Skip to content

Commit c049e24

Browse files
committed
Add Unit Test
1 parent b18a58b commit c049e24

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*.out
1616

1717
# Dependency directories (remove the comment below to include it)
18-
# vendor/
18+
vendor/
1919

2020
# Go workspace file
2121
go.work

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,26 @@ Readers are kindly reminded to add `-count=1` to the CLI since there's only env
171171
to rerun. Actually the test codes are rebuilt to new temporary binary file but the objective of this package is to offload
172172
readers from golang `testing` package internal details.
173173

174-
### Troubleshooting
174+
## Limitation
175+
176+
The test running args are mutated by updating or adding a regex to matching test function names. Which means if two test
177+
functions share the same name in different packages, they are either both selected or both skipped. This is due to the
178+
go testing package regex filter mechanism. To mitigate it, the two packages with duplicated test name shall be launched
179+
separately. Reader could refer to below example to select package and its sub packages in CLI.
180+
181+
```sh
182+
❯ go test -v .{,/pkg1,/pkg2}
183+
```
184+
185+
The above CLI will run the tests in current path and its sub paths "./pkg1" and "./pkg2" only. The other sub packages
186+
are skipped.
187+
188+
Another limitation is that multiple labels are selected with logical `AND` operator. If more complicated label conditions
189+
are needed, the users can add a new label to preconfig the test cases explicitly. For example, if the conditions are
190+
`(group=demo AND env=dev) OR (group=demo AND env=sit)`, users can add a new label `group=demo-non-prod` to include the
191+
demo cases in both dev and sit envs.
192+
193+
## Troubleshooting
175194

176195
* Add `-count=1` if only env vars are changed and the go test CLI still runs the same cases. It's due to go test internal
177196
mechanism that the same built binary is used since there's no code change in between.

gotestlabels_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package gotestlabels
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestMutateTestFilterByLabels(t *testing.T) {
9+
origArgs := os.Args
10+
defer func() { os.Args = origArgs }()
11+
os.Args = []string{"theBinDoesntMatter", "-test.v", "-labels", "group=demo", "-test.list=."}
12+
origDefaultPkg := defaultPkg
13+
defer func() { defaultPkg = origDefaultPkg }()
14+
15+
defaultPkg = "./examples/simple"
16+
17+
tests := MutateTestFilterByLabels()
18+
19+
t.Logf("Filtered tests: %v\n", tests)
20+
21+
if len(tests) != 2 {
22+
t.Errorf("Expected 2 tests, got %v", len(tests))
23+
}
24+
25+
if tests[0] != "TestSimpleAlpha" {
26+
t.Errorf("Expected TestSimpleAlpha, got %v", tests[0])
27+
}
28+
29+
if tests[1] != "TestSimpleGamma" {
30+
t.Errorf("Expected TestSimpleGamma, got %v", tests[1])
31+
}
32+
}

parser.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"golang.org/x/tools/go/packages"
1212
)
1313

14+
var defaultPkg = "./..."
15+
1416
// Get go packages in "." directory since the packages and paths are actually processed earlier than
1517
// executing the test binaries internally by the go test command. The current package only needs to
1618
// take care of the current directory.
@@ -19,7 +21,7 @@ func getPackages() ([]*packages.Package, error) {
1921
Mode: packages.NeedName | packages.NeedFiles,
2022
Tests: true,
2123
}
22-
pkgs, err := packages.Load(cfg, "./...")
24+
pkgs, err := packages.Load(cfg, defaultPkg)
2325

2426
if err != nil {
2527
return nil, fmt.Errorf("Failed to load packages: %v", err)

0 commit comments

Comments
 (0)