Skip to content

Commit a575c21

Browse files
committed
Introduce changes.patrol files for more flexible tests
1 parent 16da970 commit a575c21

File tree

8 files changed

+35
-23
lines changed

8 files changed

+35
-23
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,9 @@ like Patrol. You can take a look at any of the case defined in
104104
trying to test
105105
- add as many folders as you'd like to `patrol/testdata/{your-test}/commits`.
106106
Each folder represents an actual commit, and they will be applied on top of
107-
each other as real commits when tests are run.
107+
each other as real commits when tests are run. Each commit following the first
108+
one should contain a `changes.patrol` file that lists the output you expect
109+
from Patrol for that test (don't worry, order doesn't matter here). You can
110+
check this
111+
[example](patrol/testdata/internalchanges/commits/2/changes.patrol).
108112
- add a new test case in [patrol/repo\_test.go](patrol/repo_test.go)

patrol/patrol_test.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package patrol_test
22

33
import (
4+
"bufio"
45
"errors"
56
"fmt"
67
"io/ioutil"
@@ -27,13 +28,6 @@ type RepoTest struct {
2728

2829
// description of the test, what are trying to assess?
2930
Description string
30-
31-
// the git revision against which changes should be detected
32-
TestAgainstRevision string
33-
34-
// the list of expected packages that should be flagged as changed between
35-
// HEAD and TestAgainstRevision
36-
ExpectedChangedPackages []string
3731
}
3832

3933
func (test *RepoTest) Run(t *testing.T) {
@@ -75,19 +69,38 @@ func (test *RepoTest) Run(t *testing.T) {
7569
})
7670
require.NoError(t, err)
7771

72+
// avoid running patrol on first commit, there was nothing before
7873
if previousCommit != "" {
74+
expected := expectedChanges(t, tmp)
75+
7976
r, err := patrol.NewRepo(tmp)
8077
require.NoError(t, err)
8178

8279
changes, err := r.ChangesFrom(previousCommit)
8380
require.NoError(t, err)
84-
assert.ElementsMatch(t, test.ExpectedChangedPackages, changes, test.Name+": expected changes do not match")
81+
assert.ElementsMatch(t, expected, changes, test.Name+": expected changes do not match")
8582
}
8683

8784
previousCommit = commit.String()
8885
}
8986
}
9087

88+
func expectedChanges(t *testing.T, dir string) []string {
89+
file, err := os.Open(filepath.Join(dir, "changes.patrol"))
90+
require.NoError(t, err)
91+
defer file.Close()
92+
93+
var changes []string
94+
scanner := bufio.NewScanner(file)
95+
for scanner.Scan() {
96+
changes = append(changes, scanner.Text())
97+
}
98+
99+
require.NoError(t, scanner.Err())
100+
101+
return changes
102+
}
103+
91104
type RepoTests []RepoTest
92105

93106
func (tests RepoTests) Run(t *testing.T) {

patrol/repo_test.go

-14
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,24 @@ func TestRepo(t *testing.T) {
99
Name: "change within module",
1010
Description: "A change to a package within the same module\n" +
1111
"should flag depending packages as changed",
12-
ExpectedChangedPackages: []string{
13-
"github.com/utilitywarehouse/internalchange/internal/bar",
14-
"github.com/utilitywarehouse/internalchange/pkg/foo",
15-
"github.com/utilitywarehouse/internalchange/pkg/cat",
16-
},
1712
},
1813
RepoTest{
1914
TestdataFolder: "modules",
2015
Name: "change in go modules dependency",
2116
Description: "A change to a go modules dependency\n" +
2217
"should flag depending packages as changed",
23-
ExpectedChangedPackages: []string{
24-
"github.com/utilitywarehouse/modules",
25-
},
2618
},
2719
RepoTest{
2820
TestdataFolder: "vendoring",
2921
Name: "change in vendored dependencies",
3022
Description: "A change to a vendored dependency\n" +
3123
"should flag depending packages as changed",
32-
ExpectedChangedPackages: []string{
33-
"github.com/utilitywarehouse/vendoring",
34-
},
3524
},
3625
RepoTest{
3726
TestdataFolder: "exportedtesting",
3827
Name: "change in a package with packagename_test test package",
3928
Description: "A change to package x that is being tested " +
4029
"using x_test package should not result in a stack overflow :D",
41-
ExpectedChangedPackages: []string{
42-
"github.com/utilitywarehouse/exportedtesting/foo",
43-
},
4430
},
4531
}
4632

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github.com/utilitywarehouse/exportedtesting/foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github.com/utilitywarehouse/internalchange/internal/bar
2+
github.com/utilitywarehouse/internalchange/pkg/foo
3+
github.com/utilitywarehouse/internalchange/pkg/cat
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github.com/utilitywarehouse/internalchange/internal/bar
2+
github.com/utilitywarehouse/internalchange/pkg/foo
3+
github.com/utilitywarehouse/internalchange/pkg/cat
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github.com/utilitywarehouse/modules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github.com/utilitywarehouse/vendoring

0 commit comments

Comments
 (0)