Skip to content

Commit 05ca79e

Browse files
committed
Use testscript for e2e
testscript is a go package to test commands with isulated evrinonment, using text based files. This PR replaces the old preimitive CLI test, with testscript text files, to get much more accurate cli test, with meaningful assertions and reacher use-cases.
1 parent b15f712 commit 05ca79e

24 files changed

+1581
-67
lines changed

.github/workflows/sanity.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ jobs:
2525
skip-pkg-cache: true
2626
args: --timeout=5m
2727

28-
- name: Build
29-
run: make
30-
31-
- name: Test
28+
- name: Unit Test and Coverage
3229
run: go test -covermode=count -coverprofile cover.out ./...
3330

3431
- uses: shogo82148/actions-goveralls@v1
3532
with:
3633
path-to-profile: cover.out
3734

38-
- name: Functional Test
39-
run: make test
35+
- name: CLI Test
36+
run: make test-cli
37+
38+
# make sure we can build the cli
39+
- name: Build
40+
run: make

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ginkgolinter
22
bin/
3+
e2e

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ HASH_FLAG := -X github.com/nunnatsa/ginkgolinter/version.gitHash=$(COMMIT_HASH)
55

66
BUILD_ARGS := -ldflags "$(VERSION_FLAG) $(HASH_FLAG)"
77

8-
build: unit-test
8+
build:
99
go build $(BUILD_ARGS) -o ginkgolinter ./cmd/ginkgolinter
1010

1111
unit-test:
@@ -23,5 +23,7 @@ build-for-linux:
2323

2424
build-all: build build-for-linux build-for-mac build-for-windows
2525

26-
test: build
27-
./tests/e2e.sh
26+
test-cli:
27+
cd tests; go test -v ./
28+
29+
test: unit-test test-cli

cmd/ginkgolinter/cli.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
package main
22

3-
import (
4-
"fmt"
5-
"github.com/nunnatsa/ginkgolinter"
6-
"github.com/nunnatsa/ginkgolinter/version"
7-
"golang.org/x/tools/go/analysis/singlechecker"
8-
"os"
9-
"runtime"
10-
)
3+
import "github.com/nunnatsa/ginkgolinter/cmd/ginkgolinter/cli"
114

125
func main() {
13-
if len(os.Args) == 2 && os.Args[1] == "version" {
14-
fmt.Printf("ginkgolinter version: %s\n", version.Version())
15-
fmt.Printf("git hash: %s\n", version.GitHash())
16-
fmt.Printf("go version: %s\n", runtime.Version())
17-
os.Exit(0)
18-
}
19-
20-
singlechecker.Main(ginkgolinter.NewAnalyzer())
6+
cli.Main()
217
}

cmd/ginkgolinter/cli/cli.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"github.com/nunnatsa/ginkgolinter"
6+
"github.com/nunnatsa/ginkgolinter/version"
7+
"golang.org/x/tools/go/analysis/singlechecker"
8+
"os"
9+
"runtime"
10+
)
11+
12+
func Main() int {
13+
if len(os.Args) == 2 && os.Args[1] == "version" {
14+
fmt.Printf("ginkgolinter version: %s\n", version.Version())
15+
fmt.Printf("git hash: %s\n", version.GitHash())
16+
fmt.Printf("go version: %s\n", runtime.Version())
17+
os.Exit(0)
18+
}
19+
20+
singlechecker.Main(ginkgolinter.NewAnalyzer())
21+
22+
return 0
23+
}

testdata/src/a/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module a
22

3-
go 1.21
3+
go 1.22
44

55
require (
66
github.com/onsi/ginkgo/v2 v2.13.2

tests/cli_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package tests_test
2+
3+
import (
4+
"os"
5+
"path"
6+
"strings"
7+
"testing"
8+
9+
"github.com/rogpeppe/go-internal/testscript"
10+
11+
"github.com/nunnatsa/ginkgolinter/cmd/ginkgolinter/cli"
12+
)
13+
14+
func TestMain(m *testing.M) {
15+
pwd, err := os.Getwd()
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
if err = os.Setenv("HOME", path.Join(pwd, "cli-test")); err != nil {
21+
panic(err)
22+
}
23+
24+
os.Exit(testscript.RunMain(m, map[string]func() int{
25+
"ginkgolinter": cli.Main,
26+
}))
27+
}
28+
29+
func TestCli(t *testing.T) {
30+
pwd, err := os.Getwd()
31+
if err != nil {
32+
panic(err)
33+
}
34+
35+
if !strings.HasSuffix(pwd, "tests") {
36+
pwd = path.Join(pwd, "tests")
37+
}
38+
39+
testscript.Run(t, testscript.Params{
40+
Dir: path.Join(pwd, "testdata"),
41+
})
42+
}

tests/e2e.sh

Lines changed: 0 additions & 41 deletions
This file was deleted.

tests/go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module github.com/nunnatsa/ginkgolinter/tests
2+
3+
go 1.22.7
4+
5+
replace github.com/nunnatsa/ginkgolinter => ..
6+
7+
require (
8+
github.com/nunnatsa/ginkgolinter v0.0.0-00010101000000-000000000000
9+
github.com/rogpeppe/go-internal v1.13.1
10+
)
11+
12+
require (
13+
github.com/go-toolsmith/astcopy v1.1.0 // indirect
14+
golang.org/x/exp/typeparams v0.0.0-20241009180824-f66d83c29e7c // indirect
15+
golang.org/x/mod v0.21.0 // indirect
16+
golang.org/x/sync v0.8.0 // indirect
17+
golang.org/x/sys v0.26.0 // indirect
18+
golang.org/x/tools v0.26.0 // indirect
19+
)

tests/go.sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
github.com/go-toolsmith/astcopy v1.1.0 h1:YGwBN0WM+ekI/6SS6+52zLDEf8Yvp3n2seZITCUBt5s=
2+
github.com/go-toolsmith/astcopy v1.1.0/go.mod h1:hXM6gan18VA1T/daUEHCFcYiW8Ai1tIwIzHY6srfEAw=
3+
github.com/go-toolsmith/astequal v1.0.3/go.mod h1:9Ai4UglvtR+4up+bAD4+hCj7iTo4m/OXVTSLnCyTAx4=
4+
github.com/go-toolsmith/astequal v1.1.0 h1:kHKm1AWqClYn15R0K1KKE4RG614D46n+nqUQ06E1dTw=
5+
github.com/go-toolsmith/astequal v1.1.0/go.mod h1:sedf7VIdCL22LD8qIvv7Nn9MuWJruQA/ysswh64lffQ=
6+
github.com/go-toolsmith/strparse v1.0.0/go.mod h1:YI2nUKP9YGZnL/L1/DLFBfixrcjslWct4wyljWhSRy8=
7+
github.com/go-toolsmith/strparse v1.1.0 h1:GAioeZUK9TGxnLS+qfdqNbA4z0SSm5zVNtCQiyP2Bvw=
8+
github.com/go-toolsmith/strparse v1.1.0/go.mod h1:7ksGy58fsaQkGQlY8WVoBFNyEPMGuJin1rfoPS4lBSQ=
9+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
10+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11+
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
12+
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
13+
golang.org/x/exp/typeparams v0.0.0-20220428152302-39d4317da171/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
14+
golang.org/x/exp/typeparams v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
15+
golang.org/x/exp/typeparams v0.0.0-20241009180824-f66d83c29e7c h1:F/15/6p7LyGUSoP0GE5CB/U9+TNEER1foNOP5sWLLnI=
16+
golang.org/x/exp/typeparams v0.0.0-20241009180824-f66d83c29e7c/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
17+
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
18+
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
19+
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
20+
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
21+
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
22+
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
23+
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
24+
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=

tests/testdata/asyncerr.txtar

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# run ginkgolinter to find wrong error assertions
2+
! exec ginkgolinter errassertion
3+
! stdout .
4+
stderr -count=1 'Success matcher only support a single error value, or function with Gomega as its first parameter'
5+
stderr -count=1 'wrong error assertion.'
6+
stderr -count=1 'Success matcher does not support multiple values'
7+
8+
# also enable the force succeed rule
9+
! exec ginkgolinter --force-succeed=true errassertion
10+
! stdout .
11+
stderr -count=1 'Success matcher only support a single error value, or function with Gomega as its first parameter'
12+
stderr -count=1 'wrong error assertion.'
13+
stderr -count=1 'Success matcher does not support multiple values'
14+
stderr -count=1 'prefer using the HaveOccurred matcher for non-function error value, instead of Succeed'
15+
stderr -count=1 'prefer using the Succeed matcher for error function, instead of HaveOccurred.'
16+
17+
# run with -fix, expect wrong error assertion errors
18+
! exec ginkgolinter --fix --force-succeed=true errassertion
19+
! stdout .
20+
stderr -count=1 'Success matcher only support a single error value, or function with Gomega as its first parameter'
21+
stderr -count=1 'wrong error assertion.'
22+
stderr -count=1 'Success matcher does not support multiple values'
23+
stderr -count=1 'prefer using the HaveOccurred matcher for non-function error value, instead of Succeed'
24+
stderr -count=1 'prefer using the Succeed matcher for error function, instead of HaveOccurred.'
25+
26+
# run again after fix, expect only non-fixable errors
27+
! exec ginkgolinter errassertion
28+
! stdout .
29+
stderr -count=1 'Success matcher only support a single error value, or function with Gomega as its first parameter'
30+
! stderr 'wrong error assertion.'
31+
stderr -count=1 'Success matcher does not support multiple values'
32+
! stderr 'prefer using the HaveOccurred matcher for non-function error value, instead of Succeed'
33+
! stderr 'prefer using the Succeed matcher for error function, instead of HaveOccurred.'
34+
35+
-- err.go --
36+
package errassertion
37+
38+
import (
39+
"errors"
40+
41+
. "github.com/onsi/ginkgo/v2"
42+
. "github.com/onsi/gomega"
43+
)
44+
45+
var _ = Describe("check error assertion", func() {
46+
It("should not comparing ee to nil", func() {
47+
err := errors.New("error")
48+
Eventually(func() string{return "not error"}).Should(Succeed())
49+
Eventually(func() error{return err}).Should(BeNil())
50+
Eventually(func() (int, error) {return 42, nil}).Should(Succeed())
51+
Eventually(func() error {return err}).Should(HaveOccurred())
52+
Expect(err).ToNot(Succeed())
53+
Expect(func() error {return err}()).To(HaveOccurred())
54+
})
55+
})
56+
57+
-- go.mod --
58+
module errassertion
59+
60+
go 1.22
61+
62+
require (
63+
github.com/onsi/ginkgo/v2 v2.13.2
64+
github.com/onsi/gomega v1.30.0
65+
)
66+
67+
require (
68+
github.com/go-logr/logr v1.3.0 // indirect
69+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
70+
github.com/google/go-cmp v0.6.0 // indirect
71+
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e // indirect
72+
golang.org/x/net v0.19.0 // indirect
73+
golang.org/x/sys v0.15.0 // indirect
74+
golang.org/x/text v0.14.0 // indirect
75+
golang.org/x/tools v0.16.1 // indirect
76+
gopkg.in/yaml.v3 v3.0.1 // indirect
77+
)
78+
79+
-- go.sum --
80+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
81+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
82+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
83+
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
84+
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
85+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
86+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
87+
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
88+
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
89+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
90+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
91+
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e h1:bwOy7hAFd0C91URzMIEBfr6BAz29yk7Qj0cy6S7DJlU=
92+
github.com/google/pprof v0.0.0-20231212022811-ec68065c825e/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
93+
github.com/onsi/ginkgo/v2 v2.13.2 h1:Bi2gGVkfn6gQcjNjZJVO8Gf0FHzMPf2phUei9tejVMs=
94+
github.com/onsi/ginkgo/v2 v2.13.2/go.mod h1:XStQ8QcGwLyF4HdfcZB8SFOS/MWCgDuXMSBe6zrvLgM=
95+
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
96+
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
97+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
98+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
99+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
100+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
101+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
102+
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
103+
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
104+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
105+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
106+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
107+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
108+
golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
109+
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
110+
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
111+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
112+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
113+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
114+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
115+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
116+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)