1// SPDX-License-Identifier: MIT
2
3package workflow
4
5import "testing"
6
7func TestPatternMatch(t *testing.T) {
8 // Cases from GitHub's "Filter pattern cheat sheet" and the examples
9 // around it.
10 tests := []struct {
11 pattern, name string
12 want bool
13 }{
14 // feature/*: the * matches any character except /.
15 {"feature/*", "feature/my-branch", true},
16 {"feature/*", "feature/your-branch", true},
17 {"feature/*", "feature", false},
18 {"feature/*", "feature/a/b", false},
19 // feature/**: matches any branch with feature/ and slashes.
20 {"feature/**", "feature/beta-a/my-branch", true},
21 {"feature/**", "feature/your-branch", true},
22 {"feature/**", "feature/mona/the/octocat", true},
23 // main, releases/mona-the-octocat: exact.
24 {"main", "main", true},
25 {"main", "main2", false},
26 {"main", "xmain", false},
27 {"releases/mona-the-octocat", "releases/mona-the-octocat", true},
28 // '*': all names that do not contain a slash.
29 {"*", "main", true},
30 {"*", "releases", true},
31 {"*", "releases/10", false},
32 // '**': all names.
33 {"**", "all/the/branches", true},
34 {"**", "every/tag", true},
35 {"**", "", true},
36 // '*feature': * is a special character in YAML, quoted here.
37 {"*feature", "mona-feature", true},
38 {"*feature", "feature", true},
39 {"*feature", "ver-10-feature", true},
40 {"*feature", "a/feature", false},
41 // v2*
42 {"v2*", "v2", true},
43 {"v2*", "v2.0", true},
44 {"v2*", "v2.9", true},
45 {"v2*", "v2/x", false},
46 // v[12].[0-9]+.[0-9]+
47 {"v[12].[0-9]+.[0-9]+", "v1.10.1", true},
48 {"v[12].[0-9]+.[0-9]+", "v2.0.0", true},
49 {"v[12].[0-9]+.[0-9]+", "v3.0.0", false},
50 {"v[12].[0-9]+.[0-9]+", "v1.x.1", false},
51 // releases/**-alpha
52 {"releases/**-alpha", "releases/beta/3-alpha", true},
53 {"releases/**-alpha", "releases/v10/3-alpha", true},
54 {"releases/**-alpha", "releases/3-alpha", true},
55 {"releases/**-alpha", "releases/3-beta", false},
56 // ? : zero or one of the preceding character.
57 {"colou?r", "color", true},
58 {"colou?r", "colour", true},
59 {"colou?r", "colouur", false},
60 // + : one or more.
61 {"a+b", "ab", true},
62 {"a+b", "aaab", true},
63 {"a+b", "b", false},
64 // paths
65 {"**.js", "app.js", true},
66 {"**.js", "src/app.js", true},
67 {"**.js", "src/app.ts", false},
68 {"*.js", "app.js", true},
69 {"*.js", "src/app.js", false},
70 {"docs/**", "docs/readme.md", true},
71 {"docs/**", "docs/a/b/c.md", true},
72 {"docs/**", "docs", false},
73 {"docs/**/*.md", "docs/a.md", true},
74 {"docs/**/*.md", "docs/x/y/a.md", true},
75 {"docs/**/*.md", "docs/a.txt", false},
76 {"**/README.md", "README.md", true},
77 {"**/README.md", "a/b/README.md", true},
78 {"**/README.md", "a/XREADME.md", false},
79 {"sub-project/**", "sub-project/src/main.go", true},
80 {"/README.md", "README.md", false},
81 // Special characters stand for themselves.
82 {"a.b", "a.b", true},
83 {"a.b", "axb", false},
84 {"a(b)", "a(b)", true},
85 {`a\*b`, "a*b", true},
86 {`a\*b`, "axb", false},
87 // Case-sensitive.
88 {"Main", "main", false},
89 }
90 for _, tc := range tests {
91 p, err := CompilePattern(tc.pattern)
92 if err != nil {
93 t.Errorf("%q: %v", tc.pattern, err)
94 continue
95 }
96 if got := p.Match(tc.name); got != tc.want {
97 t.Errorf("Match(%q, %q) = %v, want %v", tc.pattern, tc.name, got, tc.want)
98 }
99 }
100}
101
102func TestMatchListOrder(t *testing.T) {
103 // "The order that you define patterns matters": a later negative pattern
104 // excludes, a later positive one includes again.
105 tests := []struct {
106 patterns []string
107 name string
108 want bool
109 }{
110 {[]string{"releases/**", "!releases/**-alpha"}, "releases/v1", true},
111 {[]string{"releases/**", "!releases/**-alpha"}, "releases/v1-alpha", false},
112 {[]string{"releases/**", "!releases/**-alpha", "releases/**/ga"}, "releases/v1-alpha/ga", true},
113 {[]string{"!releases/**-alpha", "releases/**"}, "releases/v1-alpha", true},
114 {[]string{"!docs/**"}, "src/a.go", false}, // only negatives: matches nothing
115 {[]string{"**", "!docs/**"}, "src/a.go", true},
116 {[]string{"**", "!docs/**"}, "docs/a.md", false},
117 {nil, "x", false},
118 }
119 for _, tc := range tests {
120 if got := matchList(mustList(t, tc.patterns), tc.name); got != tc.want {
121 t.Errorf("matchList(%q, %q) = %v, want %v", tc.patterns, tc.name, got, tc.want)
122 }
123 }
124}
125
126func mustList(t testing.TB, patterns []string) []*Pattern {
127 t.Helper()
128 l, err := compileList(patterns)
129 if err != nil {
130 t.Fatal(err)
131 }
132 return l
133}
134
135func TestUnclosedBracketIsLiteral(t *testing.T) {
136 if !matchList(mustList(t, []string{"[abc"}), "[abc") {
137 t.Error("an unclosed bracket should be literal")
138 }
139}
140
141func FuzzPattern(f *testing.F) {
142 for _, s := range []string{"*", "**", "a/**/b", "[a-z]+", "!x", `\`, "?+", "[", "]]"} {
143 f.Add(s, "a/b")
144 }
145 f.Fuzz(func(t *testing.T, pattern, name string) {
146 if p, err := CompilePattern(pattern); err == nil {
147 p.Match(name)
148 }
149 })
150}