Skip to content

Commit 8d93ff5

Browse files
committed
Show PID in output
1 parent b5f0097 commit 8d93ff5

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

cmd/toml-test/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,17 +295,17 @@ func detailed(r tomltest.Runner, t tomltest.Test, noNumber bool) string {
295295
zli.Colorize(" ", hlErr)))
296296
b.WriteByte('\n')
297297
}
298-
showStream(b, "input sent to parser-cmd", t.Input, noNumber)
298+
showStream(b, fmt.Sprintf("input sent to parser-cmd (PID %d)", t.PID), t.Input, noNumber)
299299

300300
out, err := jfmt.NewFormatter(0, "", " ").FormatString(t.Output)
301301
if err == nil {
302302
t.Output = out
303303
}
304304

305305
if t.OutputFromStderr {
306-
showStream(b, "output from parser-cmd (stderr)", t.Output, noNumber)
306+
showStream(b, fmt.Sprintf("output from parser-cmd (PID %d) (stderr)", t.PID), t.Output, noNumber)
307307
} else {
308-
showStream(b, "output from parser-cmd (stdout)", t.Output, noNumber)
308+
showStream(b, fmt.Sprintf("output from parser-cmd (PID %d) (stdout)", t.PID), t.Output, noNumber)
309309
}
310310
if t.Type() == tomltest.TypeValid {
311311
showStream(b, "want", t.Want, noNumber)

runner.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ type Parser interface {
7474
// An error return should only be used in case an unrecoverable error
7575
// occurred; failing to encode to TOML is not an error, but the encoder
7676
// unexpectedly panicking is.
77-
Encode(ctx context.Context, jsonInput string) (output string, outputIsError bool, err error)
77+
Encode(ctx context.Context, jsonInput string) (pid int, output string, outputIsError bool, err error)
7878

7979
// Decode a TOML string to JSON. The same semantics as Encode apply.
80-
Decode(ctx context.Context, tomlInput string) (output string, outputIsError bool, err error)
80+
Decode(ctx context.Context, tomlInput string) (pid int, output string, outputIsError bool, err error)
8181
}
8282

8383
// CommandParser calls an external command.
@@ -111,6 +111,7 @@ type Test struct {
111111
Output string // Output from the external program.
112112
Want string // The output we want.
113113
OutputFromStderr bool // The Output came from stderr, not stdout.
114+
PID int // PID from test run.
114115
Timeout time.Duration // Maximum time for parse.
115116
IntAsFloat bool // Int values have type=float.
116117
}
@@ -359,7 +360,7 @@ func (r Runner) hasSkip(path string) bool {
359360
return false
360361
}
361362

362-
func (c CommandParser) Encode(ctx context.Context, input string) (output string, outputIsError bool, err error) {
363+
func (c CommandParser) Encode(ctx context.Context, input string) (pid int, output string, outputIsError bool, err error) {
363364
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
364365
cmd := exec.CommandContext(ctx, c.cmd[0])
365366
cmd.Args = c.cmd
@@ -374,13 +375,16 @@ func (c CommandParser) Encode(ctx context.Context, input string) (output string,
374375
}
375376
}
376377

378+
if cmd.Process != nil {
379+
pid = cmd.Process.Pid
380+
}
377381
if stderr.Len() > 0 {
378-
return strings.TrimSpace(stderr.String()) + "\n", true, err
382+
return pid, strings.TrimSpace(stderr.String()) + "\n", true, err
379383
}
380-
return strings.TrimSpace(stdout.String()) + "\n", false, err
384+
return pid, strings.TrimSpace(stdout.String()) + "\n", false, err
381385
}
382386
func NewCommandParser(fsys fs.FS, cmd []string) CommandParser { return CommandParser{fsys, cmd} }
383-
func (c CommandParser) Decode(ctx context.Context, input string) (string, bool, error) {
387+
func (c CommandParser) Decode(ctx context.Context, input string) (int, string, bool, error) {
384388
return c.Encode(ctx, input)
385389
}
386390

@@ -403,9 +407,9 @@ func (t Test) runInvalid(p Parser, fsys fs.FS) Test {
403407
defer cancel()
404408

405409
if t.Encoder {
406-
t.Output, t.OutputFromStderr, err = p.Encode(ctx, t.Input)
410+
t.PID, t.Output, t.OutputFromStderr, err = p.Encode(ctx, t.Input)
407411
} else {
408-
t.Output, t.OutputFromStderr, err = p.Decode(ctx, t.Input)
412+
t.PID, t.Output, t.OutputFromStderr, err = p.Decode(ctx, t.Input)
409413
}
410414
if ctx.Err() != nil {
411415
err = timeoutError{t.Timeout}
@@ -430,9 +434,9 @@ func (t Test) runValid(p Parser, fsys fs.FS) Test {
430434
defer cancel()
431435

432436
if t.Encoder {
433-
t.Output, t.OutputFromStderr, err = p.Encode(ctx, t.Input)
437+
t.PID, t.Output, t.OutputFromStderr, err = p.Encode(ctx, t.Input)
434438
} else {
435-
t.Output, t.OutputFromStderr, err = p.Decode(ctx, t.Input)
439+
t.PID, t.Output, t.OutputFromStderr, err = p.Decode(ctx, t.Input)
436440
}
437441
if ctx.Err() != nil {
438442
err = timeoutError{t.Timeout}

0 commit comments

Comments
 (0)