diff --git a/cmd/archive.go b/cmd/archive.go index bb3dfbcb5683..9c35a5358e2a 100644 --- a/cmd/archive.go +++ b/cmd/archive.go @@ -12,6 +12,7 @@ type cmdArchive struct { gs *state.GlobalState archiveOut string + archiveStdout bool excludeEnvVars bool } @@ -33,10 +34,6 @@ func (c *cmdArchive) run(cmd *cobra.Command, args []string) error { // Archive. arc := testRunState.Runner.MakeArchive() - f, err := c.gs.FS.Create(c.archiveOut) - if err != nil { - return err - } if c.excludeEnvVars { c.gs.Logger.Debug("environment variables will be excluded from the archive") @@ -44,6 +41,15 @@ func (c *cmdArchive) run(cmd *cobra.Command, args []string) error { arc.Env = nil } + if c.archiveStdout { + return arc.Write(c.gs.Stdout) + } + + f, err := c.gs.FS.Create(c.archiveOut) + if err != nil { + return err + } + err = arc.Write(f) if cerr := f.Close(); err == nil && cerr != nil { err = cerr @@ -57,6 +63,7 @@ func (c *cmdArchive) flagSet() *pflag.FlagSet { flags.AddFlagSet(optionFlagSet()) flags.AddFlagSet(runtimeOptionFlagSet(false)) flags.StringVarP(&c.archiveOut, "archive-out", "O", c.archiveOut, "archive output filename") + flags.BoolVar(&c.archiveStdout, "archive-stdout", false, "output archive to stdout") flags.BoolVarP( &c.excludeEnvVars, "exclude-env-vars", @@ -77,7 +84,7 @@ func getCmdArchive(gs *state.GlobalState) *cobra.Command { exampleText := getExampleText(gs, ` # Archive a test run. {{.}} archive -u 10 -d 10s -O myarchive.tar script.js - + # Run the resulting archive. {{.}} run myarchive.tar`[1:]) diff --git a/cmd/archive_test.go b/cmd/archive_test.go index acbe6944bdb2..cd68129a7bb2 100644 --- a/cmd/archive_test.go +++ b/cmd/archive_test.go @@ -2,6 +2,7 @@ package cmd import ( "encoding/json" + "io/fs" "os" "path/filepath" "testing" @@ -253,3 +254,22 @@ func TestArchiveNotContainsEnv(t *testing.T) { require.NoError(t, json.Unmarshal(data, &metadata)) require.Len(t, metadata.Env, 0) } + +func TestArchiveStdout(t *testing.T) { + t.Parallel() + + // given some script that will be archived + fileName := "script.js" + testScript := []byte(`export default function () {}`) + ts := tests.NewGlobalTestState(t) + require.NoError(t, fsext.WriteFile(ts.FS, filepath.Join(ts.Cwd, fileName), testScript, 0o644)) + + ts.CmdArgs = []string{"k6", "archive", "--archive-stdout", fileName} + + newRootCommand(ts.GlobalState).execute() + + _, err := ts.FS.Stat("archive.tar") + require.ErrorIs(t, err, fs.ErrNotExist) + + require.GreaterOrEqual(t, len(ts.Stdout.Bytes()), 32) +}