fix: test slowness on usage of exit

This commit is contained in:
2026-03-21 08:01:31 +00:00
parent ff533753ef
commit f50a6b6447
2 changed files with 23 additions and 3 deletions
+8 -3
View File
@@ -26,7 +26,7 @@ func loadRecordedInput(path string) ([]byte, error) {
}
}
return trimTrailingNewlineAfterEOF(data), nil
return trimTrailingReplayNewline(data), nil
}
func loadRecordedOutput(path string) ([]byte, error) {
@@ -108,8 +108,13 @@ func findScriptFooter(data []byte) (int, footerMatchState) {
return candidate, footerFound
}
func trimTrailingNewlineAfterEOF(data []byte) []byte {
if len(data) >= 2 && data[len(data)-2] == eofByte && data[len(data)-1] == '\n' {
func trimTrailingReplayNewline(data []byte) []byte {
// util-linux script records the final Enter as "\r\n", but replay feeds the
// bytes from a file rather than a tty. If the shell exits after consuming the
// trailing '\r', the leftover '\n' can trigger script's ~2s non-tty stdin
// delay before it shuts down. Keep the '\r' that bash consumed, drop only the
// synthetic final '\n'. The same cleanup is needed after a terminal EOF byte.
if len(data) >= 2 && data[len(data)-1] == '\n' && (data[len(data)-2] == '\r' || data[len(data)-2] == eofByte) {
return data[:len(data)-1]
}
+15
View File
@@ -20,6 +20,21 @@ func TestLoadRecordedInputTrimsTrailingNewlineAfterEOF(t *testing.T) {
}
}
func TestLoadRecordedInputTrimsTrailingNewlineAfterCarriageReturn(t *testing.T) {
path := filepath.Join(t.TempDir(), "in")
writeFile(t, path, "echo hi\rexit\r\n")
got, err := loadRecordedInput(path)
if err != nil {
t.Fatalf("loadRecordedInput() error = %v", err)
}
want := "echo hi\rexit\r"
if string(got) != want {
t.Fatalf("loadRecordedInput() = %q, want %q", string(got), want)
}
}
func TestLoadRecordedInputLeavesNormalTrailingNewline(t *testing.T) {
path := filepath.Join(t.TempDir(), "in")
writeFile(t, path, "echo hi\n")