|
| 1 | +package cmd |
| 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 TestFindDefaultConfigFile_NoConfig(t *testing.T) { |
| 13 | + dir := t.TempDir() |
| 14 | + assert.Empty(t, findDefaultConfigFile(dir)) |
| 15 | +} |
| 16 | + |
| 17 | +func TestFindDefaultConfigFile_RootYml(t *testing.T) { |
| 18 | + dir := t.TempDir() |
| 19 | + path := filepath.Join(dir, ".poutine.yml") |
| 20 | + require.NoError(t, os.WriteFile(path, []byte("ignoreForks: true\n"), 0o644)) |
| 21 | + |
| 22 | + assert.Equal(t, path, findDefaultConfigFile(dir)) |
| 23 | +} |
| 24 | + |
| 25 | +func TestFindDefaultConfigFile_RootYaml(t *testing.T) { |
| 26 | + dir := t.TempDir() |
| 27 | + path := filepath.Join(dir, ".poutine.yaml") |
| 28 | + require.NoError(t, os.WriteFile(path, []byte("ignoreForks: true\n"), 0o644)) |
| 29 | + |
| 30 | + assert.Equal(t, path, findDefaultConfigFile(dir)) |
| 31 | +} |
| 32 | + |
| 33 | +func TestFindDefaultConfigFile_GithubDir(t *testing.T) { |
| 34 | + dir := t.TempDir() |
| 35 | + require.NoError(t, os.Mkdir(filepath.Join(dir, ".github"), 0o755)) |
| 36 | + path := filepath.Join(dir, ".github", "poutine.yml") |
| 37 | + require.NoError(t, os.WriteFile(path, []byte("ignoreForks: true\n"), 0o644)) |
| 38 | + |
| 39 | + assert.Equal(t, path, findDefaultConfigFile(dir)) |
| 40 | +} |
| 41 | + |
| 42 | +func TestFindDefaultConfigFile_RootTakesPrecedence(t *testing.T) { |
| 43 | + dir := t.TempDir() |
| 44 | + rootPath := filepath.Join(dir, ".poutine.yml") |
| 45 | + require.NoError(t, os.WriteFile(rootPath, []byte("ignoreForks: true\n"), 0o644)) |
| 46 | + |
| 47 | + require.NoError(t, os.Mkdir(filepath.Join(dir, ".github"), 0o755)) |
| 48 | + ghPath := filepath.Join(dir, ".github", "poutine.yml") |
| 49 | + require.NoError(t, os.WriteFile(ghPath, []byte("ignoreForks: false\n"), 0o644)) |
| 50 | + |
| 51 | + assert.Equal(t, rootPath, findDefaultConfigFile(dir), |
| 52 | + "`.poutine.yml` at repo root must take precedence over `.github/poutine.yml`") |
| 53 | +} |
| 54 | + |
| 55 | +// TestFindDefaultConfigFile_IgnoresDirectoryNamedLikeConfig ensures that a |
| 56 | +// directory (rather than a file) at a candidate path is skipped — otherwise |
| 57 | +// a stray `.poutine.yml/` dir would be wrongly reported as the config file. |
| 58 | +func TestFindDefaultConfigFile_IgnoresDirectoryNamedLikeConfig(t *testing.T) { |
| 59 | + dir := t.TempDir() |
| 60 | + require.NoError(t, os.Mkdir(filepath.Join(dir, ".poutine.yml"), 0o755)) |
| 61 | + |
| 62 | + require.NoError(t, os.Mkdir(filepath.Join(dir, ".github"), 0o755)) |
| 63 | + ghPath := filepath.Join(dir, ".github", "poutine.yml") |
| 64 | + require.NoError(t, os.WriteFile(ghPath, []byte("ignoreForks: true\n"), 0o644)) |
| 65 | + |
| 66 | + assert.Equal(t, ghPath, findDefaultConfigFile(dir)) |
| 67 | +} |
0 commit comments