|
| 1 | +package fs |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +// Tests for YamlFilePath in yaml.go using subtests. |
| 12 | +func TestYamlFilePath(t *testing.T) { |
| 13 | + t.Run("CustomPath", func(t *testing.T) { |
| 14 | + tmp := t.TempDir() |
| 15 | + rel := filepath.Join(tmp, "custom", "config.yaml") |
| 16 | + // Do not create the file; function should simply return Abs(customFileName). |
| 17 | + expected := Abs(rel) |
| 18 | + got := YamlFilePath("", "", rel) |
| 19 | + assert.Equal(t, expected, got) |
| 20 | + }) |
| 21 | + |
| 22 | + t.Run("PreferYmlIfExists", func(t *testing.T) { |
| 23 | + dir := t.TempDir() |
| 24 | + name := "app-config" |
| 25 | + |
| 26 | + // Create .yml file |
| 27 | + ymlPath := filepath.Join(dir, name+ExtYml) |
| 28 | + err := os.WriteFile(ymlPath, []byte("foo: bar\n"), 0o644) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("write %s: %v", ymlPath, err) |
| 31 | + } |
| 32 | + |
| 33 | + got := YamlFilePath(name, dir, "") |
| 34 | + assert.Equal(t, ymlPath, got) |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("DefaultYamlWhenYmlMissing", func(t *testing.T) { |
| 38 | + dir := t.TempDir() |
| 39 | + name := "settings" |
| 40 | + |
| 41 | + // Ensure .yml does not exist; do not create it. |
| 42 | + expected := filepath.Join(dir, name+ExtYaml) |
| 43 | + got := YamlFilePath(name, dir, "") |
| 44 | + assert.Equal(t, expected, got) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("BothExistReturnsYml", func(t *testing.T) { |
| 48 | + dir := t.TempDir() |
| 49 | + name := "prefs" |
| 50 | + |
| 51 | + // Create both files |
| 52 | + ymlPath := filepath.Join(dir, name+ExtYml) |
| 53 | + yamlPath := filepath.Join(dir, name+ExtYaml) |
| 54 | + |
| 55 | + if err := os.WriteFile(ymlPath, []byte("a: 1\n"), 0o644); err != nil { |
| 56 | + t.Fatalf("write %s: %v", ymlPath, err) |
| 57 | + } |
| 58 | + if err := os.WriteFile(yamlPath, []byte("a: 2\n"), 0o644); err != nil { |
| 59 | + t.Fatalf("write %s: %v", yamlPath, err) |
| 60 | + } |
| 61 | + |
| 62 | + got := YamlFilePath(name, dir, "") |
| 63 | + assert.Equal(t, ymlPath, got) |
| 64 | + }) |
| 65 | +} |
0 commit comments