Skip to content

Commit ad90eef

Browse files
dmitshurgopherbot
authored andcommitted
cmd/dist: handle -json flag in runPending (clean up)
Document work fields a bit more, and move code that synthesizes JSON-encoded skip events to testjson.go. For #37486. For #61557. Change-Id: Iffc23cf990bc39696e1e3fce8ce5a6790fc44e78 Reviewed-on: https://go-review.googlesource.com/c/go/+/512115 TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> Reviewed-by: Austin Clements <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 2ad666d commit ad90eef

File tree

2 files changed

+44
-51
lines changed

2 files changed

+44
-51
lines changed

src/cmd/dist/test.go

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -81,34 +81,21 @@ type tester struct {
8181
worklist []*work
8282
}
8383

84+
// work tracks command execution for a test.
8485
type work struct {
85-
dt *distTest
86-
cmd *exec.Cmd // Must write stdout/stderr to work.out
87-
flush func() // If non-nil, called after cmd.Run
88-
start chan bool
89-
out bytes.Buffer
90-
err error
91-
end chan bool
86+
dt *distTest // unique test name, etc.
87+
cmd *exec.Cmd // must write stdout/stderr to out
88+
flush func() // if non-nil, called after cmd.Run
89+
start chan bool // a true means to start, a false means to skip
90+
out bytes.Buffer // combined stdout/stderr from cmd
91+
err error // work result
92+
end chan struct{} // a value means cmd ended (or was skipped)
9293
}
9394

9495
// printSkip prints a skip message for all of work.
9596
func (w *work) printSkip(t *tester, msg string) {
9697
if t.json {
97-
type event struct {
98-
Time time.Time
99-
Action string
100-
Package string
101-
Output string `json:",omitempty"`
102-
}
103-
enc := json.NewEncoder(&w.out)
104-
ev := event{Time: time.Now(), Package: w.dt.name, Action: "start"}
105-
enc.Encode(ev)
106-
ev.Action = "output"
107-
ev.Output = msg
108-
enc.Encode(ev)
109-
ev.Action = "skip"
110-
ev.Output = ""
111-
enc.Encode(ev)
98+
synthesizeSkipEvent(json.NewEncoder(&w.out), w.dt.name, msg)
11299
return
113100
}
114101
fmt.Fprintln(&w.out, msg)
@@ -525,6 +512,18 @@ func (opts *goTest) packages() []string {
525512
return pkgs
526513
}
527514

515+
// printSkip prints a skip message for all of goTest.
516+
func (opts *goTest) printSkip(t *tester, msg string) {
517+
if t.json {
518+
enc := json.NewEncoder(os.Stdout)
519+
for _, pkg := range opts.packages() {
520+
synthesizeSkipEvent(enc, pkg, msg)
521+
}
522+
return
523+
}
524+
fmt.Println(msg)
525+
}
526+
528527
// ranGoTest and stdMatches are state closed over by the stdlib
529528
// testing func in registerStdTest below. The tests are run
530529
// sequentially, so there's no need for locks.
@@ -955,7 +954,7 @@ func (t *tester) registerTest(heading string, test *goTest, opts ...registerTest
955954
if skipFunc != nil {
956955
msg, skip := skipFunc(dt)
957956
if skip {
958-
t.printSkip(test, msg)
957+
test.printSkip(t, msg)
959958
return nil
960959
}
961960
}
@@ -983,30 +982,6 @@ func (t *tester) registerTest(heading string, test *goTest, opts ...registerTest
983982
}
984983
}
985984

986-
func (t *tester) printSkip(test *goTest, msg string) {
987-
if !t.json {
988-
fmt.Println(msg)
989-
return
990-
}
991-
type event struct {
992-
Time time.Time
993-
Action string
994-
Package string
995-
Output string `json:",omitempty"`
996-
}
997-
out := json.NewEncoder(os.Stdout)
998-
for _, pkg := range test.packages() {
999-
ev := event{Time: time.Now(), Package: testName(pkg, test.variant), Action: "start"}
1000-
out.Encode(ev)
1001-
ev.Action = "output"
1002-
ev.Output = msg
1003-
out.Encode(ev)
1004-
ev.Action = "skip"
1005-
ev.Output = ""
1006-
out.Encode(ev)
1007-
}
1008-
}
1009-
1010985
// dirCmd constructs a Cmd intended to be run in the foreground.
1011986
// The command will be run in dir, and Stdout and Stderr will go to os.Stdout
1012987
// and os.Stderr.
@@ -1268,8 +1243,8 @@ func (t *tester) registerCgoTests(heading string) {
12681243
}
12691244
}
12701245

1271-
// run pending test commands, in parallel, emitting headers as appropriate.
1272-
// When finished, emit header for nextTest, which is going to run after the
1246+
// runPending runs pending test commands, in parallel, emitting headers as appropriate.
1247+
// When finished, it emits header for nextTest, which is going to run after the
12731248
// pending commands are done (and runPending returns).
12741249
// A test should call runPending if it wants to make sure that it is not
12751250
// running in parallel with earlier tests, or if it has some other reason
@@ -1279,7 +1254,7 @@ func (t *tester) runPending(nextTest *distTest) {
12791254
t.worklist = nil
12801255
for _, w := range worklist {
12811256
w.start = make(chan bool)
1282-
w.end = make(chan bool)
1257+
w.end = make(chan struct{})
12831258
// w.cmd must be set up to write to w.out. We can't check that, but we
12841259
// can check for easy mistakes.
12851260
if w.cmd.Stdout == nil || w.cmd.Stdout == os.Stdout || w.cmd.Stderr == nil || w.cmd.Stderr == os.Stderr {
@@ -1305,7 +1280,7 @@ func (t *tester) runPending(nextTest *distTest) {
13051280
}
13061281
}
13071282
timelog("end", w.dt.name)
1308-
w.end <- true
1283+
w.end <- struct{}{}
13091284
}(w)
13101285
}
13111286

src/cmd/dist/testjson.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"io"
1313
"sync"
14+
"time"
1415
)
1516

1617
// lockedWriter serializes Write calls to an underlying Writer.
@@ -184,3 +185,20 @@ func (v jsonValue) MarshalJSON() ([]byte, error) {
184185
err := marshal1(v)
185186
return buf.Bytes(), err
186187
}
188+
189+
func synthesizeSkipEvent(enc *json.Encoder, pkg, msg string) {
190+
type event struct {
191+
Time time.Time
192+
Action string
193+
Package string
194+
Output string `json:",omitempty"`
195+
}
196+
ev := event{Time: time.Now(), Package: pkg, Action: "start"}
197+
enc.Encode(ev)
198+
ev.Action = "output"
199+
ev.Output = msg
200+
enc.Encode(ev)
201+
ev.Action = "skip"
202+
ev.Output = ""
203+
enc.Encode(ev)
204+
}

0 commit comments

Comments
 (0)