cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.30.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/discovery/glob_test.go

136lines · modecode

1package discovery_test
2
3import (
4 "errors"
5 "fmt"
6 "os"
7 "path"
8 "path/filepath"
9 "regexp"
10 "strconv"
11 "strings"
12 "testing"
13
14 "github.com/prometheus/prometheus/model/rulefmt"
15 "github.com/stretchr/testify/require"
16
17 "github.com/cloudflare/pint/internal/discovery"
18 "github.com/cloudflare/pint/internal/parser"
19)
20
21func TestGlobPathFinder(t *testing.T) {
22 type testCaseT struct {
23 files map[string]string
24 finder discovery.GlobFinder
25 entries []discovery.Entry
26 err error
27 }
28
29 p := parser.NewParser()
30 testRuleBody := "# pint file/owner bob\n\n- record: foo\n expr: sum(foo)\n"
31 testRules, err := p.Parse([]byte(testRuleBody))
32 require.NoError(t, err)
33
34 _, strictErrs := rulefmt.Parse([]byte(testRuleBody))
35
36 testCases := []testCaseT{
37 {
38 files: map[string]string{},
39 finder: discovery.NewGlobFinder([]string{"[]"}, nil),
40 err: filepath.ErrBadPattern,
41 },
42 {
43 files: map[string]string{},
44 finder: discovery.NewGlobFinder([]string{"*"}, nil),
45 err: fmt.Errorf("no matching files"),
46 },
47 {
48 files: map[string]string{},
49 finder: discovery.NewGlobFinder([]string{"*"}, nil),
50 err: fmt.Errorf("no matching files"),
51 },
52 {
53 files: map[string]string{},
54 finder: discovery.NewGlobFinder([]string{"foo/*"}, nil),
55 err: fmt.Errorf("no matching files"),
56 },
57 {
58 files: map[string]string{"bar.yml": testRuleBody},
59 finder: discovery.NewGlobFinder([]string{"foo/*"}, nil),
60 err: fmt.Errorf("no matching files"),
61 },
62 {
63 files: map[string]string{"bar.yml": testRuleBody},
64 finder: discovery.NewGlobFinder([]string{"*"}, []*regexp.Regexp{regexp.MustCompile(".*")}),
65 entries: []discovery.Entry{
66 {
67 ReportedPath: "bar.yml",
68 SourcePath: "bar.yml",
69 Rule: testRules[0],
70 ModifiedLines: testRules[0].Lines(),
71 Owner: "bob",
72 },
73 },
74 },
75 {
76 files: map[string]string{"foo/bar.yml": testRuleBody + "\n\n# pint file/owner alice\n"},
77 finder: discovery.NewGlobFinder([]string{"*"}, []*regexp.Regexp{regexp.MustCompile(".*")}),
78 entries: []discovery.Entry{
79 {
80 ReportedPath: "foo/bar.yml",
81 SourcePath: "foo/bar.yml",
82 Rule: testRules[0],
83 ModifiedLines: testRules[0].Lines(),
84 Owner: "alice",
85 },
86 },
87 },
88 {
89 files: map[string]string{"bar.yml": testRuleBody},
90 finder: discovery.NewGlobFinder([]string{"*"}, nil),
91 entries: []discovery.Entry{
92 {
93 ReportedPath: "bar.yml",
94 SourcePath: "bar.yml",
95 PathError: strictErrs[0],
96 ModifiedLines: []int{1, 2, 3, 4},
97 Owner: "bob",
98 },
99 },
100 },
101 {
102 files: map[string]string{"bar.yml": "record:::{}\n expr: sum(foo)\n\n# pint file/owner bob\n"},
103 finder: discovery.NewGlobFinder([]string{"*"}, []*regexp.Regexp{regexp.MustCompile(".*")}),
104 entries: []discovery.Entry{
105 {
106 ReportedPath: "bar.yml",
107 SourcePath: "bar.yml",
108 PathError: errors.New("yaml: line 2: mapping values are not allowed in this context"),
109 ModifiedLines: []int{1, 2, 3, 4},
110 Owner: "bob",
111 },
112 },
113 },
114 }
115
116 for i, tc := range testCases {
117 t.Run(strconv.Itoa(i), func(t *testing.T) {
118 workdir := t.TempDir()
119 err := os.Chdir(workdir)
120 require.NoError(t, err)
121
122 for p, content := range tc.files {
123 if strings.Contains(p, "/") {
124 err = os.MkdirAll(path.Dir(p), 0o755)
125 require.NoError(t, err)
126 }
127 err = os.WriteFile(p, []byte(content), 0o644)
128 require.NoError(t, err)
129 }
130
131 entries, err := tc.finder.Find()
132 require.Equal(t, tc.err, err)
133 require.Equal(t, tc.entries, entries)
134 })
135 }
136}
137