Skip to content

Commit 72bf7b8

Browse files
authored
Merge pull request #5 from moul/dev/moul/integration-tests
2 parents f514228 + 06a897c commit 72bf7b8

File tree

10 files changed

+146
-16
lines changed

10 files changed

+146
-16
lines changed

.github/workflows/go.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,28 @@ jobs:
171171
env_vars: OS,GOLANG
172172
name: codecov-umbrella
173173
fail_ci_if_error: false
174+
integration-tests:
175+
runs-on: ubuntu-latest
176+
strategy:
177+
matrix:
178+
golang:
179+
- 1.15.1
180+
env:
181+
OS: ubuntu-latest
182+
GOLANG: ${{ matrix.golang }}
183+
steps:
184+
- uses: actions/checkout@v2
185+
- name: Install Go
186+
uses: actions/setup-go@v2
187+
with:
188+
go-version: ${{ matrix.golang }}
189+
- uses: actions/cache@v1
190+
with:
191+
path: ~/go/pkg/mod
192+
key: ${{ runner.os }}-go-${{ matrix.golang }}-${{ hashFiles('**/go.sum') }}
193+
restore-keys: |
194+
${{ runner.os }}-go-${{ matrix.golang }}-
195+
- name: Compile the project
196+
run: make go.install
197+
- name: Run integration tests
198+
run: make integration

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ GOBINS ?= .
44
NPM_PACKAGES ?= .
55

66
include rules.mk
7+
8+
integration: install
9+
cd examples && make integration

examples/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
integration:
2+
# test with testman
3+
testman test -run ^TestStable ./...
4+
@# FIXME: test unstable tests
5+
@# FIXME: test broken tests
6+
7+
# test with default tools
8+
go install moul.io/retry
9+
go test -run ^TestStable -count=20 ./... >/dev/null # should always work
10+
retry -m=5 --interval=0 -- "(go test -run ^TestBroken -count=1 ./... >/dev/null)" && exit 1 || exit 0 # should always fail
11+
retry -m=50 --interval=0 -- "(go test -run ^TestUnstable -count 1 ./... >/dev/null)" # should work at least 1/50
12+
go mod tidy
13+
14+
@echo "SUCCESS."

examples/go.mod

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/testpkg/testpkg.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package testpkg
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
func AlwaysSucceed() error {
9+
return nil
10+
}
11+
12+
func AlwaysFailing() error {
13+
return fmt.Errorf("hope is the key to life")
14+
}
15+
16+
func MaySucceed() error {
17+
if rand.Intn(3) != 0 {
18+
return fmt.Errorf("oops, no luck, try again")
19+
}
20+
return nil
21+
}

examples/testpkg/testpkg_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package testpkg
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
"testing"
7+
8+
"moul.io/srand"
9+
)
10+
11+
func ExampleAlwaysSucceed() {
12+
fmt.Println(AlwaysSucceed())
13+
// Output: <nil>
14+
}
15+
16+
func TestStableAlwaysSucceed(t *testing.T) {
17+
if err := AlwaysSucceed(); err != nil {
18+
t.Errorf("expect no error, got %v", err)
19+
}
20+
}
21+
22+
func TestUnstableMaySucceed(t *testing.T) {
23+
rand.Seed(srand.Fast())
24+
if err := MaySucceed(); err != nil {
25+
t.Errorf("expect no error, got %v", err)
26+
}
27+
}
28+
29+
func TestBrokenAlwaysFailing(t *testing.T) {
30+
if err := AlwaysFailing(); err != nil {
31+
t.Errorf("expect no error, got %v", err)
32+
}
33+
}

go.mod

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.sum

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"os"
1010
"os/exec"
1111
"path/filepath"
12+
"regexp"
1213
"strings"
1314
"time"
1415

1516
"github.com/peterbourgon/ff/v3/ffcli"
17+
"moul.io/godev"
1618
"moul.io/motd"
1719
)
1820

@@ -28,15 +30,15 @@ func main() {
2830
}
2931

3032
func run(args []string) error {
31-
opts = Opts{
32-
verbose: false,
33-
}
34-
33+
// flags
3534
testFlags := flag.NewFlagSet("testman test", flag.ExitOnError)
36-
//testFlags.BoolVar(&opts.continueOnFailure, "continue-on-failure", opts.continueOnFailure, "Continue on failure")
37-
testFlags.BoolVar(&opts.verbose, "v", opts.verbose, "verbose")
35+
testFlags.BoolVar(&opts.Verbose, "v", false, "verbose")
36+
testFlags.StringVar(&opts.Run, "run", "^(Test|Example)", "regex to filter out tests and examples")
37+
//testFlags.IntVar(&opts.Retry, "retry", 0, "fail after N retries")
38+
//testFlags.DurationVar(&opts.Timeout, "timeout", opts.Timeout, "max duration allowed to run the whole suite")
3839
listFlags := flag.NewFlagSet("testman list", flag.ExitOnError)
39-
listFlags.BoolVar(&opts.verbose, "v", opts.verbose, "verbose")
40+
listFlags.BoolVar(&opts.Verbose, "v", false, "verbose")
41+
listFlags.StringVar(&opts.Run, "run", "^(Test|Example)", "regex to filter out tests and examples")
4042

4143
root := &ffcli.Command{
4244
ShortUsage: "testman <subcommand> [flags]",
@@ -85,6 +87,9 @@ func runList(ctx context.Context, args []string) error {
8587
if err != nil {
8688
return err
8789
}
90+
if len(tests) == 0 {
91+
continue
92+
}
8893

8994
fmt.Println(pkg.ImportPath)
9095
for _, test := range tests {
@@ -99,7 +104,7 @@ func runTest(ctx context.Context, args []string) error {
99104
return flag.ErrHelp
100105
}
101106
preRun()
102-
107+
log.Printf("runTest opts=%s args=%s", godev.JSON(opts), godev.JSON(args))
103108
start := time.Now()
104109

105110
// list packages
@@ -122,6 +127,9 @@ func runTest(ctx context.Context, args []string) error {
122127
if err != nil {
123128
return err
124129
}
130+
if len(tests) == 0 {
131+
continue
132+
}
125133

126134
pkgStart := time.Now()
127135
// compile test binary
@@ -138,16 +146,16 @@ func runTest(ctx context.Context, args []string) error {
138146
"-test.count=1",
139147
"-test.timeout=300s",
140148
}
141-
if opts.verbose {
149+
if opts.Verbose {
142150
args = append(args, "-test.v")
143151
}
144152
args = append(args, "-test.run", fmt.Sprintf("^%s$", test))
145153
cmd := exec.Command(bin, args...)
146154
log.Println(cmd.String())
147155
out, err := cmd.CombinedOutput()
148156
if err != nil {
149-
fmt.Printf("FAIL\t%s\t[compile error: %v]\n", pkg.ImportPath, err)
150-
if opts.verbose {
157+
fmt.Printf("FAIL\t%s.%s\t[compile error: %v]\n", pkg.ImportPath, test, err)
158+
if opts.Verbose {
151159
fmt.Println(string(out))
152160
}
153161
isPackageOK = false
@@ -167,7 +175,7 @@ func runTest(ctx context.Context, args []string) error {
167175
}
168176

169177
func preRun() {
170-
if !opts.verbose {
178+
if !opts.Verbose {
171179
log.SetOutput(ioutil.Discard)
172180
}
173181
}
@@ -204,6 +212,15 @@ func listDirTests(dir string) ([]string, error) {
204212
if strings.HasPrefix(line, "ok ") {
205213
continue
206214
}
215+
if opts.Run != "" {
216+
matched, err := regexp.MatchString(opts.Run, line)
217+
if err != nil {
218+
return nil, err
219+
}
220+
if !matched {
221+
continue
222+
}
223+
}
207224
tests = append(tests, line)
208225
}
209226
return tests, nil
@@ -239,11 +256,11 @@ type Package struct {
239256
}
240257

241258
type Opts struct {
242-
verbose bool
243-
// run
244-
// timeout
259+
Verbose bool
260+
Run string
261+
// Timeout time.Duration
262+
// Retry int
245263
// c
246264
// debug
247-
// retries
248265
// continueOnFailure vs failFast
249266
}

0 commit comments

Comments
 (0)