fuzzyworld

template / commit

Getting build passing irrespective of shell, getting tests working, not leaving behind lots of test files.

748e3056ede7314fb37e0f408cf56e17252c5a4f · Pavel Safronov · 2022-11-18 23:49:48 -0800 · parents 9b0177beeb3a

Getting build passing irrespective of shell, getting tests working, not leaving behind lots of test files.
 .gitignore      |  2 ++
 config_test.go  | 24 +++++++++++++++---------
 profile_test.go |  3 ++-
 3 files changed, 19 insertions(+), 10 deletions(-)

diff --git a/.gitignore b/.gitignore
index 8582cfa..0ed2877 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,3 +5,5 @@ template
 test
 test-coverage.out
 test-coverage.html
+invalid-config.yaml
+invalid-yaml.yaml
diff --git a/config_test.go b/config_test.go
index 1fe2e31..cf673a8 100644
--- a/config_test.go
+++ b/config_test.go
@@ -3,13 +3,12 @@ package main
 import (
 	"errors"
 	"os"
+	"path/filepath"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
 )
 
-const configPath string = "./test/test-config.yaml"
-
 // Compares YAML representation of two objects
 func compareObjectYaml(t *testing.T, expected, actual interface{}) {
 	expectedYaml, err := getYaml(expected)
@@ -21,11 +20,18 @@ func compareObjectYaml(t *testing.T, expected, actual interface{}) {
 }
 
 func TestSaveLoadConfig(t *testing.T) {
+	const configPath string = "./test/test-config.yaml"
+	configDir := filepath.Dir(configPath)
+	err := os.Mkdir(configDir, 0755)
+	if err != nil && !errors.Is(err, os.ErrExist) {
+		assert.Nil(t, err, "Error creating config directory")
+	}
+	defer os.Remove(configDir)
 
 	config := defaultConfig()
 
 	// save config
-	err := saveConfig(configPath, config)
+	err = saveConfig(configPath, config)
 	assert.Nil(t, err, "Error saving config")
 
 	// load config
@@ -40,9 +46,9 @@ func TestSaveLoadConfig(t *testing.T) {
 	assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
 
 	// test load invalid yaml
-	err = os.WriteFile("./test/invalid-yaml.yaml", []byte("invalid yaml"), 0644)
+	err = os.WriteFile("./invalid-yaml.yaml", []byte("invalid yaml"), 0644)
 	assert.Nil(t, err, "Error writing invalid yaml file")
-	_, err = loadConfig("./test/invalid-yaml.yaml")
+	_, err = loadConfig("./invalid-yaml.yaml")
 	assert.NotNil(t, err, "Expected to fail to parse config, but succeeded")
 
 	// test saving config to invalid path
@@ -164,18 +170,18 @@ func TestLoadConfig(t *testing.T) {
 profiles:
     - name:
 `)
-	err := os.WriteFile("./test/invalid-config.yaml", bytes, 0644)
+	err := os.WriteFile("./invalid-config.yaml", bytes, 0644)
 	assert.Nil(t, err, "Error writing invalid config file")
 
 	// load config
-	_, err = loadConfig("./test/invalid-config.yaml")
+	_, err = loadConfig("./invalid-config.yaml")
 	assert.NotNil(t, err, "Expected to fail to load config, but succeeded")
 	assert.Contains(t, err.Error(), "error validating profile : Profile name is required")
 }
 
 func TestSaveConfig(t *testing.T) {
 	config := Config{Profiles: []Profile{}}
-	err := saveConfig("./test/invalid-config.yaml", config)
+	err := saveConfig("./invalid-config.yaml", config)
 	assert.NotNil(t, err, "Expected to fail to save config, but succeeded")
 	assert.Contains(t, err.Error(), "no profiles found")
 }
@@ -194,7 +200,7 @@ func TestMergeConfigs(t *testing.T) {
 		assert.Contains(t, err.Error(), errorText)
 	}
 	testFailure(Config{}, Config{}, "no profiles found")
-	testFailure(Config{Profiles: []Profile{Profile{}}}, Config{}, "error validating config a: error validating profile : Profile name is required")
+	testFailure(Config{Profiles: []Profile{{}}}, Config{}, "error validating config a: error validating profile : Profile name is required")
 	testFailure(Config{Profiles: []Profile{defaultConfig.Profiles[0]}}, Config{}, "error validating config b: no profiles found")
 
 	profile1 := defaultConfig.Profiles[0]
diff --git a/profile_test.go b/profile_test.go
index 5245457..e661e64 100644
--- a/profile_test.go
+++ b/profile_test.go
@@ -255,7 +255,8 @@ func TestProcessProfile(t *testing.T) {
 	configureMonkeyPatching := func() {
 		monkeyPatchCalls = applyTestMonkeyPatch(t, monkeyPatch{
 			ExecCommand: func(command string, args ...string) *exec.Cmd {
-				return exec.Command("echo")
+				shell, shellArgs := shellCommandAndArgs("echo")
+				return exec.Command(shell, shellArgs...)
 			},
 			OSChdir: func(dir string) error { return nil },
 			OSGetwd: func() (dir string, err error) { return cwd, nil },