Skip to content

Commit

Permalink
Print the expected char with the default output
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Feb 24, 2023
1 parent 06ccc07 commit 2305316
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
35 changes: 35 additions & 0 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestSimpleTestStdin(t *testing.T) {
cmd.ExecuteWithGlobalState(ts.GlobalState)

stdout := ts.Stdout.String()
assert.Contains(t, stdout, "output: -")
assert.Contains(t, stdout, "default: 1 iterations for each of 1 VUs")
assert.Contains(t, stdout, "1 complete and 0 interrupted iterations")
assert.Empty(t, ts.Stderr.Bytes())
Expand Down Expand Up @@ -1745,3 +1746,37 @@ func TestBrowserPermissions(t *testing.T) {
})
}
}

func TestUIRenderOutput(t *testing.T) {
t.Parallel()

tests := []struct {
outputs []string
expRender string
}{
{outputs: []string{}, expRender: "output: -\n"},
{outputs: []string{"json"}, expRender: "output: json(stdout)\n\n"},
{outputs: []string{"json", "csv"}, expRender: "output: json(stdout), csv (file.csv)\n\n"},
}

for _, tc := range tests {
tc := tc

t.Run(tc.expRender, func(t *testing.T) {
t.Parallel()

ts := NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "run"}
for _, o := range tc.outputs {
ts.CmdArgs = append(ts.CmdArgs, "-o")
ts.CmdArgs = append(ts.CmdArgs, o)
}
ts.CmdArgs = append(ts.CmdArgs, "-")
ts.Stdin = bytes.NewBufferString(`export default function() {};`)
cmd.ExecuteWithGlobalState(ts.GlobalState)

stdout := ts.Stdout.String()
assert.Contains(t, stdout, tc.expRender)
})
}
}
12 changes: 9 additions & 3 deletions cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/metrics/engine"
"go.k6.io/k6/output"
"go.k6.io/k6/ui/pb"
)
Expand Down Expand Up @@ -109,11 +110,16 @@ func printExecutionDescription(
switch {
case outputOverride != "":
outputDescriptions = []string{outputOverride}
case len(outputs) == 0:
outputDescriptions = []string{"-"}
default:
for _, out := range outputs {
outputDescriptions = append(outputDescriptions, out.Description())
desc := out.Description()
if desc == engine.IngesterDescription {
if len(outputs) != 1 {
continue
}
desc = "-"
}
outputDescriptions = append(outputDescriptions, desc)
}
}

Expand Down
7 changes: 6 additions & 1 deletion metrics/engine/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const collectRate = 50 * time.Millisecond

var _ output.Output = &outputIngester{}

// IngesterDescription is a short description for ingester.
// This variable is used from a function in cmd/ui file for matching this output
// and print a special text.
const IngesterDescription = "Internal Metrics Ingester"

// outputIngester implements the output.Output interface and can be used to
// "feed" the MetricsEngine data from a `k6 run` test run.
type outputIngester struct {
Expand All @@ -23,7 +28,7 @@ type outputIngester struct {

// Description returns a human-readable description of the output.
func (oi *outputIngester) Description() string {
return "engine"
return IngesterDescription
}

// Start the engine by initializing a new output.PeriodicFlusher
Expand Down

0 comments on commit 2305316

Please sign in to comment.