Expose invocation reporting mode
This commit is contained in:
70
shared_debug_external_test.go
Normal file
70
shared_debug_external_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package commandkit_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
func TestInvocationReportingMode(t *testing.T) {
|
||||
if got := (commandkit.Invocation{}).ReportingMode(); got != commandkit.ReportingModeOrdinary {
|
||||
t.Fatalf("zero invocation mode = %v", got)
|
||||
}
|
||||
var retained commandkit.Invocation
|
||||
var observed commandkit.ReportingMode
|
||||
command := commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Run: func(inv commandkit.Invocation, _ struct{}) commandkit.Outcome {
|
||||
retained = inv
|
||||
observed = inv.ReportingMode()
|
||||
return commandkit.Success()
|
||||
}})
|
||||
app, err := commandkit.New(commandkit.Identity{Name: "example"}, command)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
flag string
|
||||
want commandkit.ReportingMode
|
||||
}{{"", commandkit.ReportingModeOrdinary}, {"--quiet", commandkit.ReportingModeQuiet}, {"--verbose", commandkit.ReportingModeVerbose}, {"--debug", commandkit.ReportingModeDebug}, {"", commandkit.ReportingModeOrdinary}} {
|
||||
args := []string{"run"}
|
||||
if tc.flag != "" {
|
||||
args = append(args, tc.flag)
|
||||
}
|
||||
result := app.Execute(context.Background(), args, commandkit.Streams{Out: io.Discard, Err: io.Discard})
|
||||
if result.Kind != commandkit.OutcomeSuccess || observed != tc.want || retained.ReportingMode() != tc.want {
|
||||
t.Fatalf("%q: outcome=%#v mode=%v", tc.flag, result, retained.ReportingMode())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvocationReportingModeConcurrentReads(t *testing.T) {
|
||||
command := commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Run: func(inv commandkit.Invocation, _ struct{}) commandkit.Outcome {
|
||||
start := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 4; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
for j := 0; j < 100; j++ {
|
||||
if inv.ReportingMode() != commandkit.ReportingModeDebug {
|
||||
t.Error("mode changed")
|
||||
}
|
||||
_ = inv.Reporter().Debug("diagnostic")
|
||||
}
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
return commandkit.Success()
|
||||
}})
|
||||
app, err := commandkit.New(commandkit.Identity{Name: "example"}, command)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if outcome := app.Execute(context.Background(), []string{"run", "--debug"}, commandkit.Streams{Out: io.Discard, Err: io.Discard}); outcome.Kind != commandkit.OutcomeSuccess {
|
||||
t.Fatal(outcome)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user