diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 8358cab..8044c3c 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -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) { + 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) { + 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)