cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.22.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/glob_test.go

133lines · modecode

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