Skip to content

Commit

Permalink
Merge 2e5f64e into 23d8f91
Browse files Browse the repository at this point in the history
  • Loading branch information
roobre authored Jun 17, 2024
2 parents 23d8f91 + 2e5f64e commit 59ffa06
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type cmdArchive struct {
gs *state.GlobalState

archiveOut string
archiveStdout bool
excludeEnvVars bool
}

Expand All @@ -33,17 +34,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.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
Expand All @@ -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",
Expand All @@ -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:])

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", "--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)
}

0 comments on commit 59ffa06

Please sign in to comment.