fix: strip script headers and footers in saved output

This commit is contained in:
2026-03-18 11:49:41 +00:00
parent 081e338744
commit 959d2fb569
4 changed files with 122 additions and 8 deletions
+23 -1
View File
@@ -33,6 +33,13 @@ func TestRecordCreatesRelativePath(t *testing.T) {
t.Fatalf("Stat(%q) error = %v", filepath.Join(want, name), err)
}
}
if got := readFile(t, filepath.Join(want, "in")); got != "fake recorded input\n" {
t.Fatalf("saved in = %q, want %q", got, "fake recorded input\n")
}
if got := readFile(t, filepath.Join(want, "out")); got != "fake recorded output\n" {
t.Fatalf("saved out = %q, want %q", got, "fake recorded output\n")
}
}
func TestRecordStripsExplicitTestDirPrefix(t *testing.T) {
@@ -60,6 +67,10 @@ func TestRecordStripsExplicitTestDirPrefix(t *testing.T) {
t.Fatalf("Stat(%q) error = %v", filepath.Join(want, name), err)
}
}
if got := readFile(t, filepath.Join(want, "out")); got != "fake recorded output\n" {
t.Fatalf("saved out = %q, want %q", got, "fake recorded output\n")
}
}
func TestRecordRejectsAbsolutePathOutsideTestDir(t *testing.T) {
@@ -193,6 +204,17 @@ func withStdin(t *testing.T, input string, fn func()) {
fn()
}
func readFile(t *testing.T, path string) string {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile(%q) error = %v", path, err)
}
return string(data)
}
type ioDiscard struct{}
func (ioDiscard) Write(p []byte) (int, error) {
@@ -207,7 +229,7 @@ func addFakeRecordDependencies(t *testing.T, names ...string) {
path := filepath.Join(binDir, name)
body := "#!/bin/sh\nexit 0\n"
if name == "script" {
body = "#!/bin/sh\nin=''\nout=''\nwhile [ \"$#\" -gt 0 ]; do\n case \"$1\" in\n -I)\n in=\"$2\"\n shift 2\n ;;\n -O)\n out=\"$2\"\n shift 2\n ;;\n *)\n shift\n ;;\n esac\ndone\nprintf 'fake recorded input\\n' > \"$in\"\nprintf 'fake recorded output\\n' > \"$out\"\nexit 0\n"
body = "#!/bin/sh\nin=''\nout=''\nwhile [ \"$#\" -gt 0 ]; do\n case \"$1\" in\n -I)\n in=\"$2\"\n shift 2\n ;;\n -O)\n out=\"$2\"\n shift 2\n ;;\n *)\n shift\n ;;\n esac\ndone\nif [ -n \"${FAKE_SCRIPT_LOG_IN+x}\" ]; then\n printf '%s' \"$FAKE_SCRIPT_LOG_IN\" > \"$in\"\nelse\n cat <<'EOF' > \"$in\"\nfake recorded input\nEOF\nfi\nif [ -n \"${FAKE_SCRIPT_LOG_OUT+x}\" ]; then\n printf '%s' \"$FAKE_SCRIPT_LOG_OUT\" > \"$out\"\nelse\n cat <<'EOF' > \"$out\"\nScript started on 2026-03-18 11:13:38+00:00 [TERM=\"xterm-256color\"]\nfake recorded output\nScript done on 2026-03-18 11:13:44+00:00 [COMMAND_EXIT_CODE=\"0\"]\nEOF\nfi\nexit 0\n"
}
if err := os.WriteFile(path, []byte(body), 0o755); err != nil {
t.Fatalf("WriteFile(%q) error = %v", path, err)