Skip to content

Commit c8ee9dd

Browse files
committed
Prevent stack overflow with exported testing (:
1 parent a0e002f commit c8ee9dd

File tree

9 files changed

+83
-11
lines changed

9 files changed

+83
-11
lines changed

main.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package main
22

33
import (
4-
"log"
4+
"flag"
5+
"fmt"
56
"os"
67

78
"github.com/utilitywarehouse/patrol/patrol"
@@ -13,14 +14,38 @@ type Repo struct {
1314
}
1415

1516
func main() {
16-
if len(os.Args) < 2 {
17-
log.Fatal("please provide the path to the repository")
17+
revision := flag.String("from", "", "revision that should be used to detected "+
18+
"changes in HEAD.\nE.g.: -from=a0e002f951f56d53d552f9427b3331b11ea66e92")
19+
20+
flag.Parse()
21+
22+
args := flag.Args()
23+
24+
if len(args) < 1 {
25+
fmt.Fprintf(os.Stderr, "please provide the path to the repository\n")
26+
os.Exit(1)
27+
}
28+
29+
if *revision == "" {
30+
fmt.Fprintf(os.Stderr, "please set `from` flag:\n\tpatrol -from=a0e002f951f56d53d552f9427b3331b11ea66e92 .\n")
31+
os.Exit(1)
1832
}
1933

20-
repoPath := os.Args[1]
34+
repoPath := args[0]
2135

22-
_, err := patrol.NewRepo(repoPath)
36+
repo, err := patrol.NewRepo(repoPath)
2337
if err != nil {
24-
panic(err)
38+
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
39+
os.Exit(1)
40+
}
41+
42+
changes, err := repo.ChangesFrom(*revision)
43+
if err != nil {
44+
fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
45+
os.Exit(1)
46+
}
47+
48+
for _, c := range changes {
49+
fmt.Println(c)
2550
}
2651
}

patrol/repo.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ func NewRepo(path string) (*Repo, error) {
5555

5656
var imports []string
5757
for _, file := range pkg.Files {
58-
for _, imp := range file.Imports {
59-
imports = append(imports, strings.ReplaceAll(imp.Path.Value, `"`, ""))
58+
if !strings.HasSuffix(file.Name.Name, "_test") {
59+
for _, imp := range file.Imports {
60+
imports = append(imports, strings.ReplaceAll(imp.Path.Value, `"`, ""))
61+
}
6062
}
6163
}
6264
repo.AddPackage(strings.TrimPrefix(p, path+"/"), imports)

patrol/repo_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ 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-
TestAgainstRevision: "HEAD~1",
1312
ExpectedChangedPackages: []string{
1413
"github.com/utilitywarehouse/internalchange/internal/bar",
1514
"github.com/utilitywarehouse/internalchange/pkg/foo",
@@ -21,7 +20,6 @@ func TestRepo(t *testing.T) {
2120
Name: "change in go modules dependency",
2221
Description: "A change to a go modules dependency\n" +
2322
"should flag depending packages as changed",
24-
TestAgainstRevision: "HEAD~1",
2523
ExpectedChangedPackages: []string{
2624
"github.com/utilitywarehouse/modules",
2725
},
@@ -31,11 +29,19 @@ func TestRepo(t *testing.T) {
3129
Name: "change in vendored dependencies",
3230
Description: "A change to a vendored dependency\n" +
3331
"should flag depending packages as changed",
34-
TestAgainstRevision: "HEAD~1",
3532
ExpectedChangedPackages: []string{
3633
"github.com/utilitywarehouse/vendoring",
3734
},
3835
},
36+
RepoTest{
37+
TestdataFolder: "exportedtesting",
38+
Name: "change in a package with packagename_test test package",
39+
Description: "A change to package x that is being tested " +
40+
"using x_test package should not result in a stack overflow :D",
41+
ExpectedChangedPackages: []string{
42+
"github.com/utilitywarehouse/exportedtesting/foo",
43+
},
44+
},
3945
}
4046

4147
tests.Run(t)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package foo
2+
3+
type A struct{}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package foo_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/utilitywarehouse/exportedtesting/foo"
8+
)
9+
10+
func TestFoo(t *testing.T) {
11+
fmt.Println(&foo.A{})
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/utilitywarehouse/exportedtesting
2+
3+
go 1.17
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package foo
2+
3+
type (
4+
A struct{}
5+
B struct{}
6+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package foo_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/utilitywarehouse/exportedtesting/foo"
8+
)
9+
10+
func TestFoo(t *testing.T) {
11+
fmt.Println(&foo.A{})
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/utilitywarehouse/exportedtesting
2+
3+
go 1.17

0 commit comments

Comments
 (0)