cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.29.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/glob_test.go

132lines · 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 Path: "bar.yml",
68 Rule: testRules[0],
69 ModifiedLines: testRules[0].Lines(),
70 Owner: "bob",
71 },
72 },
73 },
74 {
75 files: map[string]string{"foo/bar.yml": testRuleBody + "\n\n# pint file/owner alice\n"},
76 finder: discovery.NewGlobFinder([]string{"*"}, []*regexp.Regexp{regexp.MustCompile(".*")}),
77 entries: []discovery.Entry{
78 {
79 Path: "foo/bar.yml",
80 Rule: testRules[0],
81 ModifiedLines: testRules[0].Lines(),
82 Owner: "alice",
83 },
84 },
85 },
86 {
87 files: map[string]string{"bar.yml": testRuleBody},
88 finder: discovery.NewGlobFinder([]string{"*"}, nil),
89 entries: []discovery.Entry{
90 {
91 Path: "bar.yml",
92 PathError: strictErrs[0],
93 ModifiedLines: []int{1, 2, 3, 4},
94 Owner: "bob",
95 },
96 },
97 },
98 {
99 files: map[string]string{"bar.yml": "record:::{}\n expr: sum(foo)\n\n# pint file/owner bob\n"},
100 finder: discovery.NewGlobFinder([]string{"*"}, []*regexp.Regexp{regexp.MustCompile(".*")}),
101 entries: []discovery.Entry{
102 {
103 Path: "bar.yml",
104 PathError: errors.New("yaml: line 2: mapping values are not allowed in this context"),
105 ModifiedLines: []int{1, 2, 3, 4},
106 Owner: "bob",
107 },
108 },
109 },
110 }
111
112 for i, tc := range testCases {
113 t.Run(strconv.Itoa(i), func(t *testing.T) {
114 workdir := t.TempDir()
115 err := os.Chdir(workdir)
116 require.NoError(t, err)
117
118 for p, content := range tc.files {
119 if strings.Contains(p, "/") {
120 err = os.MkdirAll(path.Dir(p), 0o755)
121 require.NoError(t, err)
122 }
123 err = os.WriteFile(p, []byte(content), 0o644)
124 require.NoError(t, err)
125 }
126
127 entries, err := tc.finder.Find()
128 require.Equal(t, tc.err, err)
129 require.Equal(t, tc.entries, entries)
130 })
131 }
132}
133