Skip to content

Commit 4a5cf72

Browse files
committed
Replace some tests with testscripts
1 parent 81cb309 commit 4a5cf72

18 files changed

+144
-520
lines changed

cmd/gotestdox/main.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,8 @@ import (
44
"os"
55

66
"github.com/bitfield/gotestdox"
7-
"github.com/mattn/go-isatty"
87
)
98

109
func main() {
11-
td := gotestdox.NewTestDoxer()
12-
if isatty.IsTerminal(os.Stdin.Fd()) {
13-
td.ExecGoTest(os.Args[1:])
14-
} else {
15-
td.Filter()
16-
}
17-
if !td.OK {
18-
os.Exit(1)
19-
}
10+
os.Exit(gotestdox.Main())
2011
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ require (
66
github.com/fatih/color v1.13.0
77
github.com/google/go-cmp v0.5.7
88
github.com/mattn/go-isatty v0.0.14
9+
github.com/rogpeppe/go-internal v1.9.0
910
)
1011

1112
require (
1213
github.com/mattn/go-colorable v0.1.9 // indirect
14+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e // indirect
1315
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
1416
)

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
77
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
88
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
99
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
10+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
11+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
12+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
13+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
1014
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1115
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1216
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=

gotestdox.go

+14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/fatih/color"
14+
"github.com/mattn/go-isatty"
1415
)
1516

1617
// TestDoxer holds the state and config associated with a particular invocation
@@ -167,3 +168,16 @@ func ParseJSON(line string) (Event, error) {
167168
}
168169
return event, nil
169170
}
171+
172+
func Main() int {
173+
td := NewTestDoxer()
174+
if isatty.IsTerminal(os.Stdin.Fd()) {
175+
td.ExecGoTest(os.Args[1:])
176+
} else {
177+
td.Filter()
178+
}
179+
if !td.OK {
180+
return 1
181+
}
182+
return 0
183+
}

gotestdox_test.go

+15-181
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
package gotestdox_test
22

33
import (
4-
"bytes"
54
"fmt"
65
"io"
76
"log"
87
"os"
9-
"strings"
108
"testing"
119

1210
"github.com/bitfield/gotestdox"
1311
"github.com/fatih/color"
1412
"github.com/google/go-cmp/cmp"
13+
"github.com/rogpeppe/go-internal/testscript"
1514
)
1615

16+
func TestMain(m *testing.M) {
17+
os.Exit(testscript.RunMain(m, map[string]func() int{
18+
"gotestdox": gotestdox.Main,
19+
}))
20+
}
21+
22+
func TestGotestdoxProducesCorrectOutputWhen(t *testing.T) {
23+
t.Parallel()
24+
testscript.Run(t, testscript.Params{
25+
Dir: "testdata/script",
26+
})
27+
}
28+
1729
func TestParseJSON_ReturnsValidDataForValidJSON(t *testing.T) {
1830
t.Parallel()
1931
input := `{"Time":"2022-02-28T15:53:43.532326Z","Action":"pass","Package":"github.com/bitfield/script","Test":"TestFindFilesInNonexistentPathReturnsError","Elapsed":0.12}`
@@ -165,163 +177,6 @@ func TestNewTestDoxer_ReturnsTestdoxerWithStandardIOStreams(t *testing.T) {
165177
}
166178
}
167179

168-
func TestFilterSetsOKToTrueIfThereAreNoTestFailures(t *testing.T) {
169-
t.Parallel()
170-
data, err := os.Open("testdata/passing_tests.txt")
171-
if err != nil {
172-
t.Fatal(err)
173-
}
174-
defer data.Close()
175-
td := gotestdox.TestDoxer{
176-
Stdin: data,
177-
Stdout: io.Discard,
178-
Stderr: io.Discard,
179-
}
180-
td.Filter()
181-
if !td.OK {
182-
t.Error("not OK")
183-
}
184-
}
185-
186-
func TestFilterSetsOKToFalseIfAnyTestFails(t *testing.T) {
187-
t.Parallel()
188-
data, err := os.Open("testdata/failing_tests.txt")
189-
if err != nil {
190-
t.Fatal(err)
191-
}
192-
defer data.Close()
193-
td := gotestdox.TestDoxer{
194-
Stdin: data,
195-
Stdout: io.Discard,
196-
Stderr: io.Discard,
197-
}
198-
td.Filter()
199-
if td.OK {
200-
t.Error("got OK")
201-
}
202-
}
203-
204-
func TestFilterSkipsIrrelevantEvents(t *testing.T) {
205-
t.Parallel()
206-
data, err := os.Open("testdata/passing_tests.txt")
207-
if err != nil {
208-
t.Fatal(err)
209-
}
210-
defer data.Close()
211-
buf := &bytes.Buffer{}
212-
td := gotestdox.TestDoxer{
213-
Stdin: data,
214-
Stdout: buf,
215-
Stderr: io.Discard,
216-
}
217-
td.Filter()
218-
if strings.Contains(buf.String(), "Example") {
219-
t.Error("irrelevant event (Example)")
220-
}
221-
}
222-
223-
func TestFilterKeepsTrackOfCurrentPackage(t *testing.T) {
224-
t.Parallel()
225-
data, err := os.Open("testdata/packages.txt")
226-
if err != nil {
227-
t.Fatal(err)
228-
}
229-
defer data.Close()
230-
buf := &bytes.Buffer{}
231-
td := gotestdox.TestDoxer{
232-
Stdin: data,
233-
Stdout: buf,
234-
Stderr: io.Discard,
235-
}
236-
td.Filter()
237-
if strings.Count(buf.String(), "a:") > 1 {
238-
t.Error("want package a to be shown only once")
239-
}
240-
}
241-
242-
func TestFilterOrdersTestsByPrettifiedName(t *testing.T) {
243-
data, err := os.Open("testdata/unordered_tests.txt")
244-
if err != nil {
245-
t.Fatal(err)
246-
}
247-
defer data.Close()
248-
buf := &bytes.Buffer{}
249-
td := gotestdox.TestDoxer{
250-
Stdin: data,
251-
Stdout: buf,
252-
Stderr: io.Discard,
253-
}
254-
color.NoColor = true
255-
td.Filter()
256-
want := "p:\n ✔ A (0.00s)\n x B (0.00s)\n ✔ C (0.00s)\n\n"
257-
got := buf.String()
258-
if !cmp.Equal(want, got) {
259-
t.Error(cmp.Diff(want, got))
260-
}
261-
}
262-
263-
func TestFilterHandlesOutOfOrderPackageEvents(t *testing.T) {
264-
data, err := os.Open("testdata/multi_packages.txt")
265-
if err != nil {
266-
t.Fatal(err)
267-
}
268-
defer data.Close()
269-
buf := &bytes.Buffer{}
270-
td := gotestdox.TestDoxer{
271-
Stdin: data,
272-
Stdout: buf,
273-
Stderr: io.Discard,
274-
}
275-
color.NoColor = true
276-
td.Filter()
277-
want := "p:\n ✔ A (0.00s)\n x B (0.00s)\n ✔ C (0.00s)\n\nq:\n ✔ A (0.00s)\n ✔ B (0.00s)\n\n"
278-
got := buf.String()
279-
if !cmp.Equal(want, got) {
280-
t.Error(cmp.Diff(want, got))
281-
}
282-
}
283-
284-
func TestFilterSetsOKToFalseOnParsingError(t *testing.T) {
285-
t.Parallel()
286-
td := gotestdox.TestDoxer{
287-
Stdin: strings.NewReader("invalid"),
288-
Stdout: io.Discard,
289-
Stderr: io.Discard,
290-
}
291-
td.Filter()
292-
if td.OK {
293-
t.Error("got OK")
294-
}
295-
}
296-
297-
func TestExecGoTest_SetsOKToTrueWhenTestsPass(t *testing.T) {
298-
t.Parallel()
299-
path := newTempTestPath(t, passingTest)
300-
td := gotestdox.TestDoxer{
301-
Stdin: strings.NewReader("invalid"),
302-
Stdout: io.Discard,
303-
Stderr: io.Discard,
304-
}
305-
td.ExecGoTest([]string{path})
306-
if !td.OK {
307-
t.Error("want ok")
308-
}
309-
}
310-
311-
func TestExecGoTest_SetsOKToFalseWhenTestsFail(t *testing.T) {
312-
t.Parallel()
313-
path := newTempTestPath(t, failingTest)
314-
td := gotestdox.TestDoxer{
315-
Stdin: strings.NewReader("invalid"),
316-
Stdout: io.Discard,
317-
Stderr: io.Discard,
318-
}
319-
td.ExecGoTest([]string{path})
320-
if td.OK {
321-
t.Error("want not ok")
322-
}
323-
}
324-
325180
func TestExecGoTest_SetsOKToFalseWhenCommandErrors(t *testing.T) {
326181
t.Parallel()
327182
td := gotestdox.TestDoxer{
@@ -334,30 +189,9 @@ func TestExecGoTest_SetsOKToFalseWhenCommandErrors(t *testing.T) {
334189
}
335190
}
336191

337-
var (
338-
preamble = "package dummy\nimport \"testing\"\nfunc TestDummy(t *testing.T)"
339-
passingTest = preamble + "{}"
340-
failingTest = preamble + "{t.FailNow()}"
341-
)
342-
343-
func newTempTestPath(t *testing.T, data string) (path string) {
344-
t.Helper()
345-
testDir := t.TempDir()
346-
err := os.WriteFile(testDir+"/go.mod", []byte("module dummy"), os.ModePerm)
347-
if err != nil {
348-
t.Fatal(err)
349-
}
350-
path = testDir + "/dummy_test.go"
351-
err = os.WriteFile(path, []byte(data), os.ModePerm)
352-
if err != nil {
353-
t.Fatal(err)
354-
}
355-
return path
356-
}
357-
358192
func ExampleTestDoxer_Filter() {
359193
td := gotestdox.NewTestDoxer()
360-
data, err := os.Open("testdata/small_passing_tests.txt")
194+
data, err := os.Open("testdata/example.json")
361195
if err != nil {
362196
panic(err)
363197
}
File renamed without changes.

0 commit comments

Comments
 (0)