Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/archive: output archive to stdout if - is passed as --archive-out #3797

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ 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")

arc.Env = nil
}

if c.archiveOut == "-" {
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
Expand All @@ -56,7 +61,10 @@ func (c *cmdArchive) flagSet() *pflag.FlagSet {
flags.SortFlags = false
flags.AddFlagSet(optionFlagSet())
flags.AddFlagSet(runtimeOptionFlagSet(false))
flags.StringVarP(&c.archiveOut, "archive-out", "O", c.archiveOut, "archive output filename")
flags.StringVarP(
&c.archiveOut, "archive-out", "O", c.archiveOut,
"archive output filename. Dash (-) is a reserved value that causes the archive to be output to stdout.",
)
flags.BoolVarP(
&c.excludeEnvVars,
"exclude-env-vars",
Expand All @@ -77,7 +85,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:])

Expand Down
20 changes: 20 additions & 0 deletions cmd/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"encoding/json"
"io/fs"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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", "-O", "-", 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)
}
Loading