Skip to content

Commit daa45e5

Browse files
philippwallersrvaroa
authored andcommitted
fix: Improve documentation and refactor tests to cover regex with special characters
1 parent 35a6fec commit daa45e5

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,15 @@ given regexs.
395395

396396
```yaml
397397
files:
398-
- "cmd/.*_tests.go"
398+
- "cmd\\/.*_tests.go"
399+
- ".*\\/subfolder\\/.*\\.md"
399400
```
400401

402+
> **NOTICE** the double backslash (`\\`) in the example above. This GitHub Action is coded in Go (Golang), which means
403+
> you need to pay special attention to regular expressions (Regex). Special characters need to be escaped with double
404+
> backslashes. This is because the backslash in Go strings is an escape character and therefore must be escaped itself
405+
> to appear as a literal in the regex.
406+
401407
### Mergeable status (PRs only) <a name="mergeable" />
402408

403409
This condition is satisfied when the [mergeable

cmd/action_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ func TestGetLabelerConfigV1(t *testing.T) {
123123
{
124124
Label: "TestFileMatch",
125125
Files: []string{
126-
"cmd/.*.go",
127-
"pkg/.*.go",
126+
"cmd\\/.*.go",
127+
"pkg\\/.*.go",
128128
},
129129
},
130130
{
@@ -262,7 +262,7 @@ func TestGetLabelerConfig2V1(t *testing.T) {
262262
},
263263
"TestFileMatch": {
264264
Label: "TestFileMatch",
265-
Files: []string{"cmd/.*.go", "pkg/.*.go"},
265+
Files: []string{"cmd\\/.*.go", "pkg\\/.*.go"},
266266
},
267267
}
268268

pkg/labeler_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ func TestHandleEvent(t *testing.T) {
334334
{
335335
Label: "L",
336336
Size: &SizeConfig{
337-
ExcludeFiles: []string{"README.md", "new_file", "dependabot.yml"},
337+
ExcludeFiles: []string{"README.md", "new_file", "dependabot.yml", "root/sub/test.md"},
338338
// our test file has a diff in four files,
339339
// including added/removed which have a
340340
// slightly trickier diff. Adding any of
@@ -479,6 +479,24 @@ func TestHandleEvent(t *testing.T) {
479479
initialLabels: []string{},
480480
expectedLabels: []string{"Files"},
481481
},
482+
{
483+
event: "pull_request",
484+
payloads: []string{"diff_pr"},
485+
name: "Test escaping in the file condition",
486+
config: LabelerConfigV1{
487+
Version: 1,
488+
Labels: []LabelMatcher{
489+
{
490+
Label: "Files",
491+
Files: []string{
492+
"(.*?)\\/(sub|test)\\/.*",
493+
},
494+
},
495+
},
496+
},
497+
initialLabels: []string{},
498+
expectedLabels: []string{"Files"},
499+
},
482500
{
483501
event: "pull_request",
484502
payloads: []string{"small_pr"},

test_data/config2_v1.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ labels:
55
title: ".*"
66
- label: "TestFileMatch"
77
files:
8-
- "cmd/.*.go"
9-
- "pkg/.*.go"
8+
- "cmd\\/.*.go"
9+
- "pkg\\/.*.go"

test_data/config_v1.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ labels:
1515
size-above: 100
1616
- label: "TestFileMatch"
1717
files:
18-
- "cmd/.*.go"
19-
- "pkg/.*.go"
18+
- "cmd\\/.*.go"
19+
- "pkg\\/.*.go"
2020
- label: "Test"
2121
authors:
2222
- "Test1"

test_data/diff_response

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,10 @@ index 0000000..ce01362
6969
+++ b/new_file
7070
@@ -0,0 +1 @@
7171
+hello
72+
diff --git a/root/sub/test.md b/root/sub/test.md
73+
index 6c61a60..85aa975 100644
74+
--- a/root/sub/test.md
75+
+++ b/root/sub/test.md
76+
@@ -1 +1 @@
77+
-# Test File
78+
+# Test File !

0 commit comments

Comments
 (0)