Skip to content

Commit

Permalink
Updated tests to use assert
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaddalena committed Jul 14, 2022
1 parent 5b18cb3 commit 12219bb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 48 deletions.
17 changes: 5 additions & 12 deletions cmd/internal/certs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
)
Expand All @@ -18,18 +19,10 @@ func TestGenerateCertificatePackage(t *testing.T) {
crt_path := filepath.Join(GetCwdFromExe(), "ssl", "ghostwriter.crt")

// Test if the `ssl` folder exists
if !DirExists(ssl_dir) {
t.Error("Could not find the `ssl` directory")
}
assert.True(t, DirExists(ssl_dir), "Expected `ssl` folder to exist")

// Test if all certificate package files exist
if !FileExists(dh_path) {
t.Error("Failed to find the generated `dhparam.pem` file")
}
if !FileExists(key_path) {
t.Error("Failed to find the generated `ghostwriter.key` file")
}
if !FileExists(crt_path) {
t.Error("Failed to find the generated `ghostwriter.crt` file")
}
assert.True(t, FileExists(dh_path), "Expected `dhparam.pem` file to exist")
assert.True(t, FileExists(key_path), "Expected `ghostwriter.key` file to exist")
assert.True(t, FileExists(crt_path), "Expected `ghostwriter.crt` file to exist")
}
59 changes: 23 additions & 36 deletions cmd/internal/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"strings"
Expand All @@ -9,74 +10,60 @@ import (

func TestGetCwdFromExe(t *testing.T) {
cwd := GetCwdFromExe()
if cwd == "" {
t.Error("Could not get current working directory")
}
assert.False(t, cwd == "", "Expected `GetCwdFromExe()` to return a non-empty string")
}

func TestCheckPath(t *testing.T) {
if !CheckPath("docker-compose") {
t.Error("Did not find `docker-compose` in PATH")
}
assert.True(t, CheckPath("docker-compose"), "Expected `CheckPath()` to find `docker-compose` in `$PATH`")
}

func TestRunBasicCmd(t *testing.T) {
defer quietTests()()
_, err := RunBasicCmd("docker-compose", []string{"--version"})
if err != nil {
t.Errorf("Could not run `docker-compose --version` with `RunBasicCmd()`: %s", err)
}
assert.Equal(t, nil, err, "Expected `RunBasicCmd()` to return no error")
}

func TestRunCmd(t *testing.T) {
defer quietTests()()
err := RunCmd("docker-compose", []string{"--version"})
if err != nil {
t.Errorf("Could not run `docker-compose --version` with `RunCmd()`: %s", err)
}
assert.Equal(t, nil, err, "Expected `RunCmd()` to return no error")
}

func TestContains(t *testing.T) {
if !Contains([]string{"a", "b", "c"}, "b") {
t.Error("Expected `Contains()` to return true for `b`")
}
if Contains([]string{"a", "b", "c"}, "d") {
t.Error("Expected `Contains()` to return false for `d`")
}
assert.True(t, Contains([]string{"a", "b", "c"}, "b"), "Expected `Contains()` to return true")
assert.False(t, Contains([]string{"a", "b", "c"}, "d"), "Expected `Contains()` to return false")
}

func TestGetLocalGhostwriterVersion(t *testing.T) {

// Mock the Ghostwriter VERSION file
versionFile := filepath.Join(GetCwdFromExe(), "VERSION")
f, err := os.Create(versionFile)
if err != nil {
t.Error(err)
}
assert.Equal(t, nil, err, "Expected `os.Create()` to return no error")

defer f.Close()

_, writeErr := f.WriteString("v3.0.0\n7 June 2022")
if writeErr != nil {
t.Error(writeErr)
}
_, writeErr := f.WriteString("v3.0.0\n22 June 2022")
assert.Equal(t, nil, writeErr, "Expected `f.WriteString()` to return no error")

// Test reading the version data from the file
version, err := GetLocalGhostwriterVersion()
if err != nil {
t.Error(err)
} else if version != "Ghostwriter v3.0.0 ( 7 June 2022 )\n" {
t.Errorf("Expected `GetLocalGhostwriterVersion()` to return `Ghostwriter v3.0.0 ( 7 June 2022 )`, got `%s`", version)
}
assert.Equal(t, nil, err, "Expected `GetLocalGhostwriterVersion()` to return no error")
assert.Equal(
t,
"Ghostwriter v3.0.0 ( 22 June 2022 )\n",
version,
"Expected `GetLocalGhostwriterVersion()` to return `Ghostwriter v3.0.0 ( 22 June 2022 )\n`",
)
}

func TestGetRemoteGhostwriterVersion(t *testing.T) {
// Test reading the version data from GitHub's API
version, err := GetRemoteGhostwriterVersion()
if err != nil {
t.Errorf("Error getting remote Ghostwriter version: %s", err)
}
if !strings.Contains(version, "Ghostwriter v") {
t.Errorf("Expected `GetRemoteGhostwriterVersion()` to return a string containing `Ghostwriter v...`, got `%s`", version)
}
assert.Equal(t, nil, err, "Expected `GetRemoteGhostwriterVersion()` to return no error")
assert.True(
t,
strings.Contains(version, "Ghostwriter v"),
"Expected `GetRemoteGhostwriterVersion()` to return a string containing `Ghostwriter v...`",
)
}

0 comments on commit 12219bb

Please sign in to comment.