github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/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 test ... | go tool test2json [-p pkg] [-t] 10 // ./test.out 2>&1 | go tool test2json [-p pkg] [-t] 11 // 12 // Test2json expects to read go test output from 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 // Output Format 22 // 23 // The JSON stream is a newline-separated sequence of TestEvent objects 24 // corresponding to the Go struct: 25 // 26 // type TestEvent struct { 27 // Time time.Time // encodes as an RFC3339-format string 28 // Event string 29 // Package string 30 // Test string 31 // Elapsed float64 // seconds 32 // Output string 33 // } 34 // 35 // The Time field holds the time the event happened. 36 // It is conventionally omitted for cached test results. 37 // 38 // The Event field is one of a fixed set of event descriptions: 39 // 40 // run - the test has started running 41 // pause - the test has been paused 42 // cont - the test has continued running 43 // pass - the test passed 44 // fail - the test failed 45 // output - the test printed output 46 // 47 // The Package field, if present, specifies the package being tested. 48 // When the go command runs parallel tests in -json mode, events from 49 // different tests are interlaced; the Package field allows readers to 50 // separate them. 51 // 52 // The Test field, if present, specifies the test or example, or benchmark 53 // function that caused the event. Events for the overall package test 54 // do not set Test. 55 // 56 // The Elapsed field is set for "pass" and "fail" events. It gives the time 57 // elapsed for the specific test or the overall package test that passed or failed. 58 // 59 // The Output field is set for Event == "output" and is a portion of the test's output 60 // (standard output and standard error merged together). The output is 61 // unmodified except that invalid UTF-8 output from a test is coerced 62 // into valid UTF-8 by use of replacement characters. With that one exception, 63 // the concatenation of the Output fields of all output events is the exact 64 // output of the test execution. 65 // 66 package main 67 68 import ( 69 "flag" 70 "fmt" 71 "io" 72 "os" 73 74 "cmd/internal/test2json" 75 ) 76 77 var ( 78 flagP = flag.String("p", "", "report `pkg` as the package being tested in each event") 79 flagT = flag.Bool("t", false, "include timestamps in events") 80 ) 81 82 func usage() { 83 fmt.Fprintf(os.Stderr, "usage: go test ... | go tool test2json [-p pkg] [-t]\n") 84 os.Exit(2) 85 } 86 87 func main() { 88 flag.Usage = usage 89 flag.Parse() 90 if flag.NArg() > 0 { 91 usage() 92 } 93 94 var mode test2json.Mode 95 if *flagT { 96 mode |= test2json.Timestamp 97 } 98 c := test2json.NewConverter(os.Stdout, *flagP, mode) 99 defer c.Close() 100 io.Copy(c, os.Stdin) 101 }