cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.81.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/git/filter_test.go

143lines · modecode

1package git_test
2
3import (
4 "regexp"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8
9 "github.com/cloudflare/pint/internal/git"
10)
11
12func TestPathFilterIsPathAllowed(t *testing.T) {
13 type testCaseT struct {
14 title string
15 path string
16 include []*regexp.Regexp
17 exclude []*regexp.Regexp
18 allowed bool
19 }
20
21 testCases := []testCaseT{
22 {
23 title: "no filters - allow all",
24 include: nil,
25 exclude: nil,
26 path: "foo.txt",
27 allowed: true,
28 },
29 {
30 title: "include only - path matches",
31 include: []*regexp.Regexp{regexp.MustCompile(`\.yaml$`)},
32 exclude: nil,
33 path: "rules.yaml",
34 allowed: true,
35 },
36 {
37 title: "include only - path does not match",
38 include: []*regexp.Regexp{regexp.MustCompile(`\.yaml$`)},
39 exclude: nil,
40 path: "rules.txt",
41 allowed: false,
42 },
43 {
44 title: "exclude only - path matches exclude",
45 include: nil,
46 exclude: []*regexp.Regexp{regexp.MustCompile(`^vendor/`)},
47 path: "vendor/lib.go",
48 allowed: false,
49 },
50 {
51 title: "exclude only - path does not match exclude",
52 include: nil,
53 exclude: []*regexp.Regexp{regexp.MustCompile(`^vendor/`)},
54 path: "main.go",
55 allowed: true,
56 },
57 {
58 title: "include and exclude - path matches both",
59 include: []*regexp.Regexp{regexp.MustCompile(`\.go$`)},
60 exclude: []*regexp.Regexp{regexp.MustCompile(`^vendor/`)},
61 path: "vendor/lib.go",
62 allowed: false,
63 },
64 {
65 title: "include and exclude - path matches include only",
66 include: []*regexp.Regexp{regexp.MustCompile(`\.go$`)},
67 exclude: []*regexp.Regexp{regexp.MustCompile(`^vendor/`)},
68 path: "main.go",
69 allowed: true,
70 },
71 {
72 title: "include and exclude - path matches neither",
73 include: []*regexp.Regexp{regexp.MustCompile(`\.go$`)},
74 exclude: []*regexp.Regexp{regexp.MustCompile(`^vendor/`)},
75 path: "README.md",
76 allowed: false,
77 },
78 }
79
80 for _, tc := range testCases {
81 t.Run(tc.title, func(t *testing.T) {
82 filter := git.NewPathFilter(tc.include, tc.exclude, nil)
83 allowed := filter.IsPathAllowed(tc.path)
84 require.Equal(t, tc.allowed, allowed)
85 })
86 }
87}
88
89func TestPathFilterIsRelaxed(t *testing.T) {
90 type testCaseT struct {
91 title string
92 path string
93 relaxed []*regexp.Regexp
94 isMatch bool
95 }
96
97 testCases := []testCaseT{
98 {
99 title: "no relaxed patterns",
100 relaxed: nil,
101 path: "foo.txt",
102 isMatch: false,
103 },
104 {
105 title: "path matches relaxed pattern",
106 relaxed: []*regexp.Regexp{regexp.MustCompile(`^tests/`)},
107 path: "tests/foo.yaml",
108 isMatch: true,
109 },
110 {
111 title: "path does not match relaxed pattern",
112 relaxed: []*regexp.Regexp{regexp.MustCompile(`^tests/`)},
113 path: "rules/foo.yaml",
114 isMatch: false,
115 },
116 {
117 title: "multiple relaxed patterns - matches first",
118 relaxed: []*regexp.Regexp{regexp.MustCompile(`^tests/`), regexp.MustCompile(`^examples/`)},
119 path: "tests/foo.yaml",
120 isMatch: true,
121 },
122 {
123 title: "multiple relaxed patterns - matches second",
124 relaxed: []*regexp.Regexp{regexp.MustCompile(`^tests/`), regexp.MustCompile(`^examples/`)},
125 path: "examples/foo.yaml",
126 isMatch: true,
127 },
128 {
129 title: "multiple relaxed patterns - matches none",
130 relaxed: []*regexp.Regexp{regexp.MustCompile(`^tests/`), regexp.MustCompile(`^examples/`)},
131 path: "rules/foo.yaml",
132 isMatch: false,
133 },
134 }
135
136 for _, tc := range testCases {
137 t.Run(tc.title, func(t *testing.T) {
138 filter := git.NewPathFilter(nil, nil, tc.relaxed)
139 isMatch := filter.IsRelaxed(tc.path)
140 require.Equal(t, tc.isMatch, isMatch)
141 })
142 }
143}
144