Source file src/cmd/test2json/main.go
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 // } 45 // 46 // The Time field holds the time the event happened. 47 // It is conventionally omitted for cached test results. 48 // 49 // The Action field is one of a fixed set of action descriptions: 50 // 51 // start - the test binary is about to be executed 52 // run - the test has started running 53 // pause - the test has been paused 54 // cont - the test has continued running 55 // pass - the test passed 56 // bench - the benchmark printed log output but did not fail 57 // fail - the test or benchmark failed 58 // output - the test printed output 59 // skip - the test was skipped or the package contained no tests 60 // 61 // Every JSON stream begins with a "start" event. 62 // 63 // The Package field, if present, specifies the package being tested. 64 // When the go command runs parallel tests in -json mode, events from 65 // different tests are interlaced; the Package field allows readers to 66 // separate them. 67 // 68 // The Test field, if present, specifies the test, example, or benchmark 69 // function that caused the event. Events for the overall package test 70 // do not set Test. 71 // 72 // The Elapsed field is set for "pass" and "fail" events. It gives the time 73 // elapsed for the specific test or the overall package test that passed or failed. 74 // 75 // The Output field is set for Action == "output" and is a portion of the test's output 76 // (standard output and standard error merged together). The output is 77 // unmodified except that invalid UTF-8 output from a test is coerced 78 // into valid UTF-8 by use of replacement characters. With that one exception, 79 // the concatenation of the Output fields of all output events is the exact 80 // output of the test execution. 81 // 82 // When a benchmark runs, it typically produces a single line of output 83 // giving timing results. That line is reported in an event with Action == "output" 84 // and no Test field. If a benchmark logs output or reports a failure 85 // (for example, by using b.Log or b.Error), that extra output is reported 86 // as a sequence of events with Test set to the benchmark name, terminated 87 // by a final event with Action == "bench" or "fail". 88 // Benchmarks have no events with Action == "pause". 89 package main 90 91 import ( 92 "flag" 93 "fmt" 94 "io" 95 "os" 96 "os/exec" 97 "os/signal" 98 99 "cmd/internal/telemetry/counter" 100 "cmd/internal/test2json" 101 ) 102 103 var ( 104 flagP = flag.String("p", "", "report `pkg` as the package being tested in each event") 105 flagT = flag.Bool("t", false, "include timestamps in events") 106 ) 107 108 func usage() { 109 fmt.Fprintf(os.Stderr, "usage: go tool test2json [-p pkg] [-t] [./pkg.test -test.v]\n") 110 os.Exit(2) 111 } 112 113 // ignoreSignals ignore the interrupt signals. 114 func ignoreSignals() { 115 signal.Ignore(signalsToIgnore...) 116 } 117 118 func main() { 119 counter.Open() 120 121 flag.Usage = usage 122 flag.Parse() 123 counter.Inc("test2json/invocations") 124 counter.CountFlags("test2json/flag:", *flag.CommandLine) 125 126 var mode test2json.Mode 127 if *flagT { 128 mode |= test2json.Timestamp 129 } 130 c := test2json.NewConverter(os.Stdout, *flagP, mode) 131 defer c.Close() 132 133 if flag.NArg() == 0 { 134 io.Copy(c, os.Stdin) 135 } else { 136 args := flag.Args() 137 cmd := exec.Command(args[0], args[1:]...) 138 w := &countWriter{0, c} 139 cmd.Stdout = w 140 cmd.Stderr = w 141 ignoreSignals() 142 err := cmd.Run() 143 if err != nil { 144 if w.n > 0 { 145 // Assume command printed why it failed. 146 } else { 147 fmt.Fprintf(c, "test2json: %v\n", err) 148 } 149 } 150 c.Exited(err) 151 if err != nil { 152 c.Close() 153 os.Exit(1) 154 } 155 } 156 } 157 158 type countWriter struct { 159 n int64 160 w io.Writer 161 } 162 163 func (w *countWriter) Write(b []byte) (int, error) { 164 w.n += int64(len(b)) 165 return w.w.Write(b) 166 } 167