File: handler_test.go

package info (click to toggle)
gotestsum 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,060 kB
  • sloc: sh: 89; makefile: 16
file content (122 lines) | stat: -rw-r--r-- 3,166 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cmd

import (
	"bytes"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"gotest.tools/gotestsum/testjson"
	"gotest.tools/v3/assert"
	"gotest.tools/v3/env"
	"gotest.tools/v3/fs"
	"gotest.tools/v3/golden"
)

func TestPostRunHook(t *testing.T) {
	command := &commandValue{}
	err := command.Set("go run ./testdata/postrunhook/main.go")
	assert.NilError(t, err)

	buf := new(bytes.Buffer)
	opts := &options{
		postRunHookCmd: command,
		jsonFile:       "events.json",
		junitFile:      "junit.xml",
		stdout:         buf,
	}

	env.Patch(t, "GOTESTSUM_FORMAT", "short")

	exec := newExecFromTestData(t)
	err = postRunHook(opts, exec)
	assert.NilError(t, err)
	golden.Assert(t, buf.String(), "post-run-hook-expected")
}

func newExecFromTestData(t *testing.T) *testjson.Execution {
	t.Helper()
	f, err := os.Open("../testjson/testdata/input/go-test-json.out")
	assert.NilError(t, err)
	defer f.Close() // nolint: errcheck

	exec, err := testjson.ScanTestOutput(testjson.ScanConfig{
		Stdout: f,
		Stderr: strings.NewReader(""),
	})
	assert.NilError(t, err)
	return exec
}

type bufferCloser struct {
	bytes.Buffer
}

func (bufferCloser) Close() error { return nil }

func TestEventHandler_Event_WithMissingActionFail(t *testing.T) {
	buf := new(bufferCloser)
	errBuf := new(bytes.Buffer)
	format := testjson.NewEventFormatter(errBuf, "testname")

	source := golden.Get(t, "../../testjson/testdata/input/go-test-json-missing-test-fail.out")
	cfg := testjson.ScanConfig{
		Stdout:  bytes.NewReader(source),
		Handler: &eventHandler{jsonFile: buf, formatter: format},
	}
	_, err := testjson.ScanTestOutput(cfg)
	assert.NilError(t, err)

	assert.Equal(t, buf.String(), string(source))
	// confirm the artificial event was sent to the handler by checking the output
	// of the formatter.
	golden.Assert(t, errBuf.String(), "event-handler-missing-test-fail-expected")
}

func TestEventHandler_Event_MaxFails(t *testing.T) {
	format := testjson.NewEventFormatter(ioutil.Discard, "testname")

	source := golden.Get(t, "../../testjson/testdata/input/go-test-json.out")
	cfg := testjson.ScanConfig{
		Stdout:  bytes.NewReader(source),
		Handler: &eventHandler{formatter: format, maxFails: 2},
	}

	_, err := testjson.ScanTestOutput(cfg)
	assert.Error(t, err, "ending test run because max failures was reached")
}

func TestNewEventHandler_CreatesDirectory(t *testing.T) {
	dir := fs.NewDir(t, t.Name())
	jsonFile := filepath.Join(dir.Path(), "new-path", "log.json")

	opts := &options{
		stdout:   new(bytes.Buffer),
		format:   "testname",
		jsonFile: jsonFile,
	}
	_, err := newEventHandler(opts)
	assert.NilError(t, err)

	_, err = os.Stat(jsonFile)
	assert.NilError(t, err)
}

func TestWriteJunitFile_CreatesDirectory(t *testing.T) {
	dir := fs.NewDir(t, t.Name())
	junitFile := filepath.Join(dir.Path(), "new-path", "junit.xml")

	opts := &options{
		junitFile:                    junitFile,
		junitTestCaseClassnameFormat: &junitFieldFormatValue{},
		junitTestSuiteNameFormat:     &junitFieldFormatValue{},
	}
	exec := &testjson.Execution{}
	err := writeJUnitFile(opts, exec)
	assert.NilError(t, err)

	_, err = os.Stat(junitFile)
	assert.NilError(t, err)
}