/
content_analyzer_test.go
116 lines (109 loc) · 3.29 KB
/
content_analyzer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package yamlfmt_test
import (
"path/filepath"
"testing"
"github.com/google/yamlfmt"
"github.com/google/yamlfmt/internal/collections"
"github.com/google/yamlfmt/internal/tempfile"
)
const testdataBase = "testdata/content_analyzer"
func TestBasicContentAnalyzer(t *testing.T) {
testCases := []struct {
name string
testdataDir string
excludePatterns []string
expectedPaths collections.Set[string]
expectedExcluded collections.Set[string]
}{
{
name: "has ignore metadata",
testdataDir: "has_ignore",
excludePatterns: []string{},
expectedPaths: collections.Set[string]{
"y.yaml": {},
},
expectedExcluded: collections.Set[string]{
"x.yaml": {},
},
},
{
name: "matches regex pattern",
testdataDir: "regex_ignore",
excludePatterns: []string{
".*generated by.*",
},
expectedPaths: collections.Set[string]{
"y.yaml": {},
},
expectedExcluded: collections.Set[string]{
"x.yaml": {},
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tempPath := t.TempDir()
testdataDir := filepath.Join(testdataBase, tc.testdataDir)
paths, err := tempfile.ReplicateDirectory(testdataDir, tempPath)
if err != nil {
t.Fatalf("could not replicate testdata directory %s: %v", tc.testdataDir, err)
}
err = paths.CreateAll()
if err != nil {
t.Fatalf("could not create full test directory: %v", err)
}
contentAnalyzer, err := yamlfmt.NewBasicContentAnalyzer(tc.excludePatterns)
if err != nil {
t.Fatalf("could not create content analyzer: %v", err)
}
collector := &yamlfmt.FilepathCollector{
Include: []string{tempPath},
Exclude: []string{},
Extensions: []string{"yaml", "yml"},
}
collectedPaths, err := collector.CollectPaths()
if err != nil {
t.Fatalf("CollectPaths failed: %v", err)
}
resultPaths, excludedPaths, err := contentAnalyzer.ExcludePathsByContent(collectedPaths)
if err != nil {
t.Fatalf("expected content analyzer to work, got error: %v", err)
}
resultPathsTrimmed, err := pathsTempdirTrimmed(resultPaths, tempPath)
if err != nil {
t.Fatalf("expected trimming tempdir from result not to have error: %v", err)
}
if !tc.expectedPaths.Equals(collections.SliceToSet(resultPathsTrimmed)) {
t.Fatalf("expected files:\n%v\ngot:\n%v", tc.expectedPaths, resultPaths)
}
excludePathsTrimmed, err := pathsTempdirTrimmed(excludedPaths, tempPath)
if err != nil {
t.Fatalf("expected trimming tempdir from excluded not to have error: %v", err)
}
if !tc.expectedExcluded.Equals(collections.SliceToSet(excludePathsTrimmed)) {
t.Fatalf("expected excludsions:\n%v\ngot:\n%v", tc.expectedExcluded, excludedPaths)
}
})
}
}
func TestBadNewContentAnalyzer(t *testing.T) {
// Illegal because no closing )
badPattern := "%^3412098(]fj"
_, err := yamlfmt.NewBasicContentAnalyzer([]string{badPattern})
if err == nil {
t.Fatalf("expected there to be an error")
}
}
func pathsTempdirTrimmed(paths []string, tempDir string) ([]string, error) {
trimmedPaths := []string{}
for _, path := range paths {
trimmedPath, err := filepath.Rel(tempDir, path)
if err != nil {
return nil, err
}
trimmedPaths = append(trimmedPaths, trimmedPath)
}
return trimmedPaths, nil
}