|
| 1 | +package goformat |
| 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 | + "github.com/golangci/golangci-lint/v2/pkg/config" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRunnerOptions_MatchAnyPattern(t *testing.T) { |
| 15 | + testCases := []struct { |
| 16 | + desc string |
| 17 | + cfg *config.Config |
| 18 | + filename string |
| 19 | + |
| 20 | + assertMatch assert.BoolAssertionFunc |
| 21 | + expectedCount int |
| 22 | + }{ |
| 23 | + { |
| 24 | + desc: "match", |
| 25 | + cfg: &config.Config{ |
| 26 | + Formatters: config.Formatters{ |
| 27 | + Exclusions: config.FormatterExclusions{ |
| 28 | + Paths: []string{`generated\.go`}, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + filename: "generated.go", |
| 33 | + assertMatch: assert.True, |
| 34 | + expectedCount: 1, |
| 35 | + }, |
| 36 | + { |
| 37 | + desc: "no match", |
| 38 | + cfg: &config.Config{ |
| 39 | + Formatters: config.Formatters{ |
| 40 | + Exclusions: config.FormatterExclusions{ |
| 41 | + Paths: []string{`excluded\.go`}, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + filename: "test.go", |
| 46 | + assertMatch: assert.False, |
| 47 | + expectedCount: 0, |
| 48 | + }, |
| 49 | + { |
| 50 | + desc: "no patterns", |
| 51 | + cfg: &config.Config{}, |
| 52 | + filename: "test.go", |
| 53 | + assertMatch: assert.False, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for _, test := range testCases { |
| 58 | + t.Run(test.desc, func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + tmpDir := t.TempDir() |
| 62 | + |
| 63 | + testFile := filepath.Join(tmpDir, test.filename) |
| 64 | + |
| 65 | + err := os.WriteFile(testFile, []byte("package main"), 0o600) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + test.cfg.SetConfigDir(tmpDir) |
| 69 | + |
| 70 | + opts, err := NewRunnerOptions(test.cfg, false, false, false) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + match, err := opts.MatchAnyPattern(testFile) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + test.assertMatch(t, match) |
| 77 | + |
| 78 | + require.Len(t, opts.patterns, len(test.cfg.Formatters.Exclusions.Paths)) |
| 79 | + |
| 80 | + if len(opts.patterns) == 0 { |
| 81 | + assert.Empty(t, opts.excludedPathCounter) |
| 82 | + } else { |
| 83 | + assert.Equal(t, test.expectedCount, opts.excludedPathCounter[opts.patterns[0]]) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// File structure: |
| 90 | +// |
| 91 | +// tmp |
| 92 | +// ├── project (`realDir`) |
| 93 | +// │ ├── .golangci.yml |
| 94 | +// │ └── test.go |
| 95 | +// └── somewhere |
| 96 | +// └── symlink (to "project") |
| 97 | +func TestRunnerOptions_MatchAnyPattern_withSymlinks(t *testing.T) { |
| 98 | + tmpDir := t.TempDir() |
| 99 | + |
| 100 | + testFile := filepath.Join(tmpDir, "project", "test.go") |
| 101 | + |
| 102 | + realDir := filepath.Dir(testFile) |
| 103 | + |
| 104 | + err := os.MkdirAll(realDir, 0o755) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + err = os.WriteFile(testFile, []byte("package main"), 0o600) |
| 108 | + require.NoError(t, err) |
| 109 | + |
| 110 | + symlink := filepath.Join(tmpDir, "somewhere", "symlink") |
| 111 | + |
| 112 | + err = os.MkdirAll(filepath.Dir(symlink), 0o755) |
| 113 | + require.NoError(t, err) |
| 114 | + |
| 115 | + err = os.Symlink(realDir, symlink) |
| 116 | + require.NoError(t, err) |
| 117 | + |
| 118 | + cfg := &config.Config{ |
| 119 | + Formatters: config.Formatters{ |
| 120 | + Exclusions: config.FormatterExclusions{ |
| 121 | + Paths: []string{`^[^/\\]+\.go$`}, |
| 122 | + }, |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + cfg.SetConfigDir(symlink) |
| 127 | + |
| 128 | + opts, err := NewRunnerOptions(cfg, false, false, false) |
| 129 | + require.NoError(t, err) |
| 130 | + |
| 131 | + match, err := opts.MatchAnyPattern(filepath.Join(symlink, "test.go")) |
| 132 | + require.NoError(t, err) |
| 133 | + |
| 134 | + assert.True(t, match) |
| 135 | + |
| 136 | + require.NotEmpty(t, opts.patterns) |
| 137 | + require.Len(t, opts.patterns, len(cfg.Formatters.Exclusions.Paths)) |
| 138 | + |
| 139 | + assert.Equal(t, 1, opts.excludedPathCounter[opts.patterns[0]]) |
| 140 | +} |
0 commit comments