Skip to content

Commit

Permalink
test: added setup and teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline committed Aug 29, 2024
1 parent 9dc0e10 commit c833602
Showing 1 changed file with 46 additions and 15 deletions.
61 changes: 46 additions & 15 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,67 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLoadConfig(t *testing.T) {
tmpDir := t.TempDir()
var tmpDir string

// Create an empty file for testing
configFilePath := filepath.Join(tmpDir, ".sauced.yaml")

if err := os.WriteFile(configFilePath, []byte{}, 0644); err != nil {
t.Fatalf("Failed to create empty file: %v", err)
setup := func() {
tmpDir = t.TempDir()
}

// Path for a non-existent file
nonExistentPath := filepath.Join(tmpDir, "non_existent.yaml")
teardown := func() {
os.RemoveAll(tmpDir)
}

tests := []struct {
name string
path string
fallbackPath string
setup func(t *testing.T) (string, string)
expectedError bool
}{
{"Existing file", configFilePath, "", false},
{"Non-existent file", nonExistentPath, "", true},
{"Non-existent file with fallback", DefaultConfigPath, configFilePath, false},
{"Default path", DefaultConfigPath, "", true},
{
name: "Existing file",
setup: func(t *testing.T) (string, string) {
configFilePath := filepath.Join(tmpDir, ".sauced.yaml")
require.NoError(t, os.WriteFile(configFilePath, []byte("key: value"), 0644))
return configFilePath, ""
},
expectedError: false,
},
{
name: "Non-existent file",
setup: func(t *testing.T) (string, string) {

Check failure on line 39 in pkg/config/config_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
return filepath.Join(tmpDir, ".sauced.yaml"), ""
},
expectedError: true,
},
{
name: "Non-existent file with fallback",
setup: func(t *testing.T) (string, string) {
fallbackPath := filepath.Join(tmpDir, ".sauced.yaml")
require.NoError(t, os.WriteFile(fallbackPath, []byte("key: fallback"), 0644))
nonExistentPath := filepath.Join(tmpDir, ".sauced.yaml")
return nonExistentPath, fallbackPath
},
expectedError: false,
},
{
name: "Default path",
setup: func(t *testing.T) (string, string) {

Check failure on line 56 in pkg/config/config_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
return DefaultConfigPath, ""
},
expectedError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config, err := LoadConfig(tt.path, tt.fallbackPath)
setup()
defer teardown()

path, fallbackPath := tt.setup(t)
config, err := LoadConfig(path, fallbackPath)

if tt.expectedError {
assert.Error(t, err)
Expand Down

0 comments on commit c833602

Please sign in to comment.