Skip to content

Commit

Permalink
test: added tests for LoadConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nickytonline committed Aug 29, 2024
1 parent a0298b1 commit 2a5f85d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/posthog/posthog-go v1.2.19
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
golang.org/x/term v0.23.0
gopkg.in/yaml.v3 v3.0.1
)
Expand All @@ -29,6 +30,7 @@ require (
github.com/charmbracelet/x/windows v0.1.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/go-chi/chi/v5 v5.1.0
Expand All @@ -46,6 +48,7 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
Expand Down
54 changes: 54 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package config

import (
"os"
"path/filepath"
"testing"

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

func TestLoadConfig(t *testing.T) {
// Create a temporary directory for test files
tmpDir, err := os.MkdirTemp("", "config_test")
if err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
defer os.RemoveAll(tmpDir)

// 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)
}

// Path for a non-existent file
nonExistentPath := filepath.Join(tmpDir, "non_existent.yaml")

tests := []struct {
name string
path string
fallbackPath string
expectedError bool
}{
{"Existing file", configFilePath, "", false},
{"Non-existent file", nonExistentPath, "", true},
{"Non-existent file with fallback", DefaultConfigPath, configFilePath, false},
{"Default path", DefaultConfigPath, "", true},
}

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

if tt.expectedError {
assert.Error(t, err)
assert.Nil(t, config)
} else {
assert.NoError(t, err)
assert.NotNil(t, config)
}
})
}
}

0 comments on commit 2a5f85d

Please sign in to comment.