|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDefaultPathInDir(t *testing.T) { |
| 13 | + t.Run("prefers config.yaml when both exist", func(t *testing.T) { |
| 14 | + dir := t.TempDir() |
| 15 | + want := filepath.Join(dir, "config.yaml") |
| 16 | + other := filepath.Join(dir, "config.yml") |
| 17 | + require.NoError(t, os.WriteFile(want, []byte("a: 1"), 0644)) |
| 18 | + require.NoError(t, os.WriteFile(other, []byte("a: 2"), 0644)) |
| 19 | + |
| 20 | + got, err := defaultPathInDir(dir) |
| 21 | + require.NoError(t, err) |
| 22 | + assert.Equal(t, want, got) |
| 23 | + }) |
| 24 | + |
| 25 | + t.Run("returns config.yaml when only it exists", func(t *testing.T) { |
| 26 | + dir := t.TempDir() |
| 27 | + want := filepath.Join(dir, "config.yaml") |
| 28 | + require.NoError(t, os.WriteFile(want, []byte("a: 1"), 0644)) |
| 29 | + |
| 30 | + got, err := defaultPathInDir(dir) |
| 31 | + require.NoError(t, err) |
| 32 | + assert.Equal(t, want, got) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("falls back to config.yml when only it exists", func(t *testing.T) { |
| 36 | + dir := t.TempDir() |
| 37 | + want := filepath.Join(dir, "config.yml") |
| 38 | + require.NoError(t, os.WriteFile(want, []byte("a: 1"), 0644)) |
| 39 | + |
| 40 | + got, err := defaultPathInDir(dir) |
| 41 | + require.NoError(t, err) |
| 42 | + assert.Equal(t, want, got) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("errors when neither exists and names both paths", func(t *testing.T) { |
| 46 | + dir := t.TempDir() |
| 47 | + got, err := defaultPathInDir(dir) |
| 48 | + assert.Empty(t, got) |
| 49 | + require.Error(t, err) |
| 50 | + assert.Contains(t, err.Error(), filepath.Join(dir, "config.yaml")) |
| 51 | + assert.Contains(t, err.Error(), filepath.Join(dir, "config.yml")) |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +func TestDefaultPath(t *testing.T) { |
| 56 | + got, err := DefaultPath() |
| 57 | + if err != nil { |
| 58 | + ex, exErr := os.Executable() |
| 59 | + require.NoError(t, exErr) |
| 60 | + assert.Contains(t, err.Error(), filepath.Dir(ex)) |
| 61 | + return |
| 62 | + } |
| 63 | + ex, err := os.Executable() |
| 64 | + require.NoError(t, err) |
| 65 | + assert.Equal(t, filepath.Dir(ex), filepath.Dir(got)) |
| 66 | + assert.Contains(t, []string{"config.yaml", "config.yml"}, filepath.Base(got)) |
| 67 | +} |
0 commit comments