github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/src/cmd/test2json/main.go (about)

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