feat: add cmd mire rewrite to rewrite all goldens from input again
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"mire/internal/mire"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func newRewriteCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "rewrite [path]",
|
||||
Short: "Refresh recorded CLI output fixtures",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
path := ""
|
||||
if len(args) == 1 {
|
||||
path = args[0]
|
||||
}
|
||||
|
||||
if err := mire.Rewrite(path); err != nil {
|
||||
cmd.SilenceUsage = true
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -37,7 +37,7 @@ func newRootCommand() *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
rootCmd.AddCommand(newInitCommand(), newRecordCommand(), newTestCommand())
|
||||
rootCmd.AddCommand(newInitCommand(), newRecordCommand(), newRewriteCommand(), newTestCommand())
|
||||
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ func TestRunShowsHelpWhenNoArgs(t *testing.T) {
|
||||
for _, want := range []string{
|
||||
"init Initialise mire in the current project",
|
||||
"record Record a new CLI scenario",
|
||||
"rewrite Refresh recorded CLI output fixtures",
|
||||
"test Replay recorded CLI scenarios",
|
||||
} {
|
||||
if !strings.Contains(stdout, want) {
|
||||
@@ -129,6 +130,51 @@ func TestRunRecord(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRunRewrite(t *testing.T) {
|
||||
testutil.AddFakeRecordDependencies(t, "bwrap", "bash")
|
||||
|
||||
root := t.TempDir()
|
||||
testutil.WriteScenarioFixtures(t, filepath.Join(root, "e2e", "suite", "spec"), "echo rewrite\nexit\n", "stale output\n")
|
||||
|
||||
testutil.WithWorkingDir(t, root, func() struct{} {
|
||||
initStdout, initStderr := testutil.CaptureOutput(t, func() {
|
||||
if got := Run([]string{"init"}); got != 0 {
|
||||
t.Fatalf("Run() code = %d, want %d", got, 0)
|
||||
}
|
||||
})
|
||||
if initStdout != prefixed("Done initialising...\n") {
|
||||
t.Fatalf("stdout = %q, want %q", initStdout, prefixed("Done initialising...\n"))
|
||||
}
|
||||
if initStderr != "" {
|
||||
t.Fatalf("stderr = %q, want empty", initStderr)
|
||||
}
|
||||
|
||||
stdout, stderr := testutil.CaptureOutput(t, func() {
|
||||
if got := Run([]string{"rewrite"}); got != 0 {
|
||||
t.Fatalf("Run() code = %d, want %d", got, 0)
|
||||
}
|
||||
})
|
||||
|
||||
for _, want := range []string{
|
||||
"RUN suite/spec",
|
||||
"PASS suite/spec (",
|
||||
"Summary: total=1 passed=1 failed=0",
|
||||
} {
|
||||
if !strings.Contains(stdout, want) {
|
||||
t.Fatalf("stdout = %q, want substring %q", stdout, want)
|
||||
}
|
||||
}
|
||||
if stderr != "" {
|
||||
t.Fatalf("stderr = %q, want empty", stderr)
|
||||
}
|
||||
return struct{}{}
|
||||
})
|
||||
|
||||
if got := testutil.ReadFile(t, filepath.Join(root, "e2e", "suite", "spec", "out")); got == "stale output\n" {
|
||||
t.Fatalf("rewrite left stale output unchanged")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunInitFailsWhenDependenciesMissing(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
t.Setenv("PATH", "")
|
||||
|
||||
Reference in New Issue
Block a user