Debian Codegraph @

src/cmd/test2json/main.go

raw
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Test2json converts go test output to a machine-readable JSON stream.
6 //
7 // Usage:
8 //
9 // go tool test2json [-p pkg] [-t] [./pkg.test -test.v=test2json]
10 //
11 // Test2json runs the given test command and converts its output to JSON;
12 // with no command specified, test2json expects test output on standard input.
13 // It writes a corresponding stream of JSON events to standard output.
14 // There is no unnecessary input or output buffering, so that
15 // the JSON stream can be read for “live updates” of test status.
16 //
17 // The -p flag sets the package reported in each test event.
18 //
19 // The -t flag requests that time stamps be added to each test event.
20 //
21 // The test should be invoked with -test.v=test2json. Using only -test.v
22 // (or -test.v=true) is permissible but produces lower fidelity results.
23 //
24 // Note that "go test -json" takes care of invoking test2json correctly,
25 // so "go tool test2json" is only needed when a test binary is being run
26 // separately from "go test". Use "go test -json" whenever possible.
27 //
28 // Note also that test2json is only intended for converting a single test
29 // binary's output. To convert the output of a "go test" command that
30 // runs multiple packages, again use "go test -json".
31 //
32 // # Output Format
33 //
34 // The JSON stream is a newline-separated sequence of TestEvent objects
35 // corresponding to the Go struct:
36 //
37 // type TestEvent struct {
38 // Time time.Time // encodes as an RFC3339-format string
39 // Action string
40 // Package string
41 // Test string
42 // Elapsed float64 // seconds
43 // Output string
44 // FailedBuild string
45 // }
46 //
47 // The Time field holds the time the event happened.
48 // It is conventionally omitted for cached test results.
49 //
50 // The Action field is one of a fixed set of action descriptions:
51 //
52 // start - the test binary is about to be executed
53 // run - the test has started running
54 // pause - the test has been paused
55 // cont - the test has continued running
56 // pass - the test passed
57 // bench - the benchmark printed log output but did not fail
58 // fail - the test or benchmark failed
59 // output - the test printed output
60 // skip - the test was skipped or the package contained no tests
61 //
62 // Every JSON stream begins with a "start" event.
63 //
64 // The Package field, if present, specifies the package being tested.
65 // When the go command runs parallel tests in -json mode, events from
66 // different tests are interlaced; the Package field allows readers to
67 // separate them.
68 //
69 // The Test field, if present, specifies the test, example, or benchmark
70 // function that caused the event. Events for the overall package test
71 // do not set Test.
72 //
73 // The Elapsed field is set for "pass" and "fail" events. It gives the time
74 // elapsed for the specific test or the overall package test that passed or failed.
75 //
76 // The Output field is set for Action == "output" and is a portion of the test's output
77 // (standard output and standard error merged together). The output is
78 // unmodified except that invalid UTF-8 output from a test is coerced
79 // into valid UTF-8 by use of replacement characters. With that one exception,
80 // the concatenation of the Output fields of all output events is the exact
81 // output of the test execution.
82 //
83 // The FailedBuild field is set for Action == "fail" if the test failure was
84 // caused by a build failure. It contains the package ID of the package that
85 // failed to build. This matches the ImportPath field of the "go list" output,
86 // as well as the BuildEvent.ImportPath field as emitted by "go build -json".
87 //
88 // When a benchmark runs, it typically produces a single line of output
89 // giving timing results. That line is reported in an event with Action == "output"
90 // and no Test field. If a benchmark logs output or reports a failure
91 // (for example, by using b.Log or b.Error), that extra output is reported
92 // as a sequence of events with Test set to the benchmark name, terminated
93 // by a final event with Action == "bench" or "fail".
94 // Benchmarks have no events with Action == "pause".
95 package main
96
97 import (
98 "flag"
99 "fmt"
100 "io"
101 "os"
102 "os/exec"
103 "os/signal"
104
105 "cmd/internal/telemetry/counter"
106 "cmd/internal/test2json"
107 )
108
109 var (
110 flagP = flag.String("p", "", "report `pkg` as the package being tested in each event")
111 flagT = flag.Bool("t", false, "include timestamps in events")
112 )
113
114 func usage() {
115 fmt.Fprintf(os.Stderr, "usage: go tool test2json [-p pkg] [-t] [./pkg.test -test.v]\n")
116 os.Exit(2)
117 }
118
119 // ignoreSignals ignore the interrupt signals.
120 func ignoreSignals() {
121 signal.Ignore(signalsToIgnore...)
122 }
123
124 func main() {
125 counter.Open()
126
127 flag.Usage = usage
128 flag.Parse()
129 counter.Inc("test2json/invocations")
130 counter.CountFlags("test2json/flag:", *flag.CommandLine)
131
132 var mode test2json.Mode
133 if *flagT {
134 mode |= test2json.Timestamp
135 }
136 c := test2json.NewConverter(os.Stdout, *flagP, mode)
137 defer c.Close()
138
139 if flag.NArg() == 0 {
140 io.Copy(c, os.Stdin)
141 } else {
142 args := flag.Args()
143 cmd := exec.Command(args[0], args[1:]...)
144 w := &countWriter{0, c}
145 cmd.Stdout = w
146 cmd.Stderr = w
147 ignoreSignals()
148 err := cmd.Run()
149 if err != nil {
150 if w.n > 0 {
151 // Assume command printed why it failed.
152 } else {
153 fmt.Fprintf(c, "test2json: %v\n", err)
154 }
155 }
156 c.Exited(err)
157 if err != nil {
158 c.Close()
159 os.Exit(1)
160 }
161 }
162 }
163
164 type countWriter struct {
165 n int64
166 w io.Writer
167 }
168
169 func (w *countWriter) Write(b []byte) (int, error) {
170 w.n += int64(len(b))
171 return w.w.Write(b)
172 }