cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.16.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/discovery/glob_test.go

102lines · modecode

1package discovery_test
2
3import (
4 "fmt"
5 "io/ioutil"
6 "os"
7 "path"
8 "path/filepath"
9 "strconv"
10 "strings"
11 "testing"
12
13 "github.com/stretchr/testify/require"
14
15 "github.com/cloudflare/pint/internal/discovery"
16 "github.com/cloudflare/pint/internal/parser"
17)
18
19func TestGlobPathFinder(t *testing.T) {
20 type testCaseT struct {
21 files map[string]string
22 finder discovery.GlobFinder
23 entries []discovery.Entry
24 err error
25 }
26
27 p := parser.NewParser()
28 testRuleBody := "# pint file/owner bob\n\n- record: foo\n expr: sum(foo)\n"
29 testRules, err := p.Parse([]byte(testRuleBody))
30 require.NoError(t, err)
31
32 testCases := []testCaseT{
33 {
34 files: map[string]string{},
35 finder: discovery.NewGlobFinder("[]"),
36 err: filepath.ErrBadPattern,
37 },
38 {
39 files: map[string]string{},
40 finder: discovery.NewGlobFinder("*"),
41 err: fmt.Errorf("no matching files"),
42 },
43 {
44 files: map[string]string{},
45 finder: discovery.NewGlobFinder("*"),
46 err: fmt.Errorf("no matching files"),
47 },
48 {
49 files: map[string]string{},
50 finder: discovery.NewGlobFinder("foo/*"),
51 err: fmt.Errorf("no matching files"),
52 },
53 {
54 files: map[string]string{"bar.yml": testRuleBody},
55 finder: discovery.NewGlobFinder("foo/*"),
56 err: fmt.Errorf("no matching files"),
57 },
58 {
59 files: map[string]string{"bar.yml": testRuleBody},
60 finder: discovery.NewGlobFinder("*"),
61 entries: []discovery.Entry{
62 {
63 Path: "bar.yml",
64 Rule: testRules[0],
65 Owner: "bob",
66 },
67 },
68 },
69 {
70 files: map[string]string{"foo/bar.yml": testRuleBody + "\n\n# pint file/owner alice\n"},
71 finder: discovery.NewGlobFinder("*"),
72 entries: []discovery.Entry{
73 {
74 Path: "foo/bar.yml",
75 Rule: testRules[0],
76 Owner: "alice",
77 },
78 },
79 },
80 }
81
82 for i, tc := range testCases {
83 t.Run(strconv.Itoa(i), func(t *testing.T) {
84 workdir := t.TempDir()
85 err := os.Chdir(workdir)
86 require.NoError(t, err)
87
88 for p, content := range tc.files {
89 if strings.Contains(p, "/") {
90 err = os.MkdirAll(path.Dir(p), 0o755)
91 require.NoError(t, err)
92 }
93 err = ioutil.WriteFile(p, []byte(content), 0o644)
94 require.NoError(t, err)
95 }
96
97 entries, err := tc.finder.Find()
98 require.Equal(t, tc.err, err)
99 require.Equal(t, tc.entries, entries)
100 })
101 }
102}
103