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/test2json"
   100  )
   101  
   102  var (
   103  	flagP = flag.String("p", "", "report `pkg` as the package being tested in each event")
   104  	flagT = flag.Bool("t", false, "include timestamps in events")
   105  )
   106  
   107  func usage() {
   108  	fmt.Fprintf(os.Stderr, "usage: go tool test2json [-p pkg] [-t] [./pkg.test -test.v]\n")
   109  	os.Exit(2)
   110  }
   111  
   112  // ignoreSignals ignore the interrupt signals.
   113  func ignoreSignals() {
   114  	signal.Ignore(signalsToIgnore...)
   115  }
   116  
   117  func main() {
   118  	flag.Usage = usage
   119  	flag.Parse()
   120  
   121  	var mode test2json.Mode
   122  	if *flagT {
   123  		mode |= test2json.Timestamp
   124  	}
   125  	c := test2json.NewConverter(os.Stdout, *flagP, mode)
   126  	defer c.Close()
   127  
   128  	if flag.NArg() == 0 {
   129  		io.Copy(c, os.Stdin)
   130  	} else {
   131  		args := flag.Args()
   132  		cmd := exec.Command(args[0], args[1:]...)
   133  		w := &countWriter{0, c}
   134  		cmd.Stdout = w
   135  		cmd.Stderr = w
   136  		ignoreSignals()
   137  		err := cmd.Run()
   138  		if err != nil {
   139  			if w.n > 0 {
   140  				// Assume command printed why it failed.
   141  			} else {
   142  				fmt.Fprintf(c, "test2json: %v\n", err)
   143  			}
   144  		}
   145  		c.Exited(err)
   146  		if err != nil {
   147  			c.Close()
   148  			os.Exit(1)
   149  		}
   150  	}
   151  }
   152  
   153  type countWriter struct {
   154  	n int64
   155  	w io.Writer
   156  }
   157  
   158  func (w *countWriter) Write(b []byte) (int, error) {
   159  	w.n += int64(len(b))
   160  	return w.w.Write(b)
   161  }
   162  

View as plain text