libstdc++: Qualify calls in <bits/stl_uninitialized.h> to prevent ADL
[official-gcc.git] / libgo / go / cmd / test2json / main.go
blobfdf681a8ffe564cc07715b7c218b789345446d27
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.
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 [-test.paniconexit0]]
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.
17 // The -p flag sets the package reported in each test event.
19 // The -t flag requests that time stamps be added to each test event.
21 // The test must be invoked with -test.v. Additionally passing
22 // -test.paniconexit0 will cause test2json to exit with a non-zero
23 // status if one of the tests being run calls os.Exit(0).
25 // Note that test2json is only intended for converting a single test
26 // binary's output. To convert the output of a "go test" command,
27 // use "go test -json" instead of invoking test2json directly.
29 // Output Format
31 // The JSON stream is a newline-separated sequence of TestEvent objects
32 // corresponding to the Go struct:
34 // type TestEvent struct {
35 // Time time.Time // encodes as an RFC3339-format string
36 // Action string
37 // Package string
38 // Test string
39 // Elapsed float64 // seconds
40 // Output string
41 // }
43 // The Time field holds the time the event happened.
44 // It is conventionally omitted for cached test results.
46 // The Action field is one of a fixed set of action descriptions:
48 // run - the test has started running
49 // pause - the test has been paused
50 // cont - the test has continued running
51 // pass - the test passed
52 // bench - the benchmark printed log output but did not fail
53 // fail - the test or benchmark failed
54 // output - the test printed output
55 // skip - the test was skipped or the package contained no tests
57 // The Package field, if present, specifies the package being tested.
58 // When the go command runs parallel tests in -json mode, events from
59 // different tests are interlaced; the Package field allows readers to
60 // separate them.
62 // The Test field, if present, specifies the test, example, or benchmark
63 // function that caused the event. Events for the overall package test
64 // do not set Test.
66 // The Elapsed field is set for "pass" and "fail" events. It gives the time
67 // elapsed for the specific test or the overall package test that passed or failed.
69 // The Output field is set for Action == "output" and is a portion of the test's output
70 // (standard output and standard error merged together). The output is
71 // unmodified except that invalid UTF-8 output from a test is coerced
72 // into valid UTF-8 by use of replacement characters. With that one exception,
73 // the concatenation of the Output fields of all output events is the exact
74 // output of the test execution.
76 // When a benchmark runs, it typically produces a single line of output
77 // giving timing results. That line is reported in an event with Action == "output"
78 // and no Test field. If a benchmark logs output or reports a failure
79 // (for example, by using b.Log or b.Error), that extra output is reported
80 // as a sequence of events with Test set to the benchmark name, terminated
81 // by a final event with Action == "bench" or "fail".
82 // Benchmarks have no events with Action == "run", "pause", or "cont".
84 package main
86 import (
87 "flag"
88 "fmt"
89 exec "internal/execabs"
90 "io"
91 "os"
93 "cmd/internal/test2json"
96 var (
97 flagP = flag.String("p", "", "report `pkg` as the package being tested in each event")
98 flagT = flag.Bool("t", false, "include timestamps in events")
101 func usage() {
102 fmt.Fprintf(os.Stderr, "usage: go tool test2json [-p pkg] [-t] [./pkg.test -test.v]\n")
103 os.Exit(2)
106 func main() {
107 flag.Usage = usage
108 flag.Parse()
110 var mode test2json.Mode
111 if *flagT {
112 mode |= test2json.Timestamp
114 c := test2json.NewConverter(os.Stdout, *flagP, mode)
115 defer c.Close()
117 if flag.NArg() == 0 {
118 io.Copy(c, os.Stdin)
119 } else {
120 args := flag.Args()
121 cmd := exec.Command(args[0], args[1:]...)
122 w := &countWriter{0, c}
123 cmd.Stdout = w
124 cmd.Stderr = w
125 err := cmd.Run()
126 if err != nil {
127 if w.n > 0 {
128 // Assume command printed why it failed.
129 } else {
130 fmt.Fprintf(c, "test2json: %v\n", err)
133 c.Exited(err)
134 if err != nil {
135 c.Close()
136 os.Exit(1)
141 type countWriter struct {
142 n int64
143 w io.Writer
146 func (w *countWriter) Write(b []byte) (int, error) {
147 w.n += int64(len(b))
148 return w.w.Write(b)