Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
feat: Capture and assert no bash job control messages
Maximilian Roos5149596d2d28
diff --git a/templates/posix_directives.sh b/templates/posix_directives.shindex de75fa830..bb1e4aba7 100644--- a/templates/posix_directives.sh+++ b/templates/posix_directives.sh@@ -8,9 +8,7 @@# systems may capture functions but not environment variables.wt_exec() {# Disable job control notifications in zsh (prevents "[1] 12345" / "[1] + done" messages)- # Zsh prints these when background jobs start/complete in interactive shells- # Note: Bash also prints these, but `set +m` doesn't work within functions to suppress them- # Bash notifications are less intrusive (completion message only at next prompt vs inline)+ # For bash, this is handled at the backgrounding site using fd redirectionif [[ -n "${ZSH_VERSION:-}" ]]; thensetopt LOCAL_OPTIONS NO_MONITORfi@@ -46,8 +44,17 @@ wt_exec() {# Run worktrunk in background, piping stdout to FIFO# (stderr stays attached to TTY for child process colors/progress)- command "${_WORKTRUNK_CMD:-{{ cmd_prefix }}}" "$@" >"$fifo_path" &- runner_pid=$!+ # For bash, redirect stderr for the { & } to suppress job notifications,+ # but keep command's stderr on the original fd+ if [[ -n "${BASH_VERSION:-}" ]]; then+ exec 9>&2+ { command "${_WORKTRUNK_CMD:-{{ cmd_prefix }}}" "$@" >"$fifo_path" 2>&9 & } 2>/dev/null+ runner_pid=$!+ exec 9>&-+ else+ command "${_WORKTRUNK_CMD:-{{ cmd_prefix }}}" "$@" >"$fifo_path" &+ runner_pid=$!+ fi# Parse directives as they stream inwhile IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; dodiff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rsindex e800daea8..9b06724a2 100644--- a/tests/integration_tests/shell_wrapper.rs+++ b/tests/integration_tests/shell_wrapper.rs@@ -77,6 +77,12 @@ struct ShellOutput {exit_code: i32,}+/// Regex for detecting bash job control messages+/// Matches patterns like "[1] 12345" (job start) and "[1]+ Done" (job completion)+static JOB_CONTROL_REGEX: LazyLock<regex::Regex> = LazyLock::new(|| {+ regex::Regex::new(r"\[\d+\][+-]?\s+(Done|\d+)").expect("Invalid job control regex pattern")+});+impl ShellOutput {/// Check if output contains no directive leaksfn assert_no_directive_leaks(&self) {@@ -92,6 +98,19 @@ impl ShellOutput {);}+ /// Check if output contains no bash job control messages+ ///+ /// Job control messages like "[1] 12345" (job start) and "[1]+ Done ..." (job completion)+ /// should not appear in user-facing output. These are internal shell artifacts from+ /// background process management that leak implementation details.+ fn assert_no_job_control_messages(&self) {+ assert!(+ !JOB_CONTROL_REGEX.is_match(&self.combined),+ "Output contains job control messages (e.g., '[1] 12345' or '[1]+ Done'):\n{}",+ self.combined+ );+ }+/// Normalize paths and ANSI codes in output for snapshot testingfn normalized(&self) -> String {// First normalize temporary directory paths@@ -232,8 +251,15 @@ fn build_shell_script(shell: &str, repo: &TestRepo, subcommand: &str, args: &[&s// (see templates/fish.fish - psub causes buffering). Tests document current behavior.format!("begin\n{}\nend 2>&1", script)}+ "bash" => {+ // For bash, we don't use a subshell wrapper because it would isolate job control messages.+ // Instead, we use exec to redirect stderr to stdout, then run the script.+ // This ensures job control messages (like "[1] 12345" and "[1]+ Done") are captured,+ // allowing tests to catch these leaks.+ format!("exec 2>&1\n{}", script)+ }_ => {- // bash/zsh use parentheses for subshell grouping+ // zsh uses parentheses for subshell groupingformat!("( {} ) 2>&1", script)}}@@ -307,12 +333,13 @@ fn exec_in_pty_interactive(cmd.env("USER", "testuser");cmd.env("SHELL", shell);- // For zsh, isolate from user rc files- // Note: We no longer pass NO_MONITOR here - job control notifications are now- // suppressed by the shell integration code itself (setopt LOCAL_OPTIONS NO_MONITOR- // in wt_exec). This ensures tests match real user experience.+ // Run in interactive mode to simulate real user environment.+ // This ensures tests catch job control message leaks like "[1] 12345" and "[1]+ Done".+ // Interactive shells have job control enabled by default.if shell == "zsh" {+ // Isolate from user rc filescmd.env("ZDOTDIR", "/dev/null");+ cmd.arg("-i");cmd.arg("--no-rcs");cmd.arg("-o");cmd.arg("NO_GLOBAL_RCS");@@ -320,6 +347,10 @@ fn exec_in_pty_interactive(cmd.arg("NO_RCS");}+ if shell == "bash" {+ cmd.arg("-i");+ }+cmd.arg("-c");cmd.arg(script);cmd.cwd(working_dir);@@ -560,6 +591,7 @@ mod tests {// Shell-agnostic assertionsassert_eq!(output.exit_code, 0, "{}: Command should succeed", shell);output.assert_no_directive_leaks();+ output.assert_no_job_control_messages();assert!(output.combined.contains("Created new worktree"),@@ -1056,6 +1088,7 @@ approved-commands = ["echo 'test command executed'"]// This is where the bug occurs - "🔄 Starting (background):" is printed with println!// which causes it to concatenate with the directiveoutput.assert_no_directive_leaks();+ output.assert_no_job_control_messages();assert_eq!(output.exit_code, 0, "Command should succeed");diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snapindex 5d0c3050a..715b263d0 100644--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap@@ -41,9 +41,7 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then# systems may capture functions but not environment variables.wt_exec() {# Disable job control notifications in zsh (prevents "[1] 12345" / "[1] + done" messages)- # Zsh prints these when background jobs start/complete in interactive shells- # Note: Bash also prints these, but `set +m` doesn't work within functions to suppress them- # Bash notifications are less intrusive (completion message only at next prompt vs inline)+ # For bash, this is handled at the backgrounding site using fd redirectionif [[ -n "${ZSH_VERSION:-}" ]]; thensetopt LOCAL_OPTIONS NO_MONITORfi@@ -79,8 +77,17 @@ wt_exec() {# Run worktrunk in background, piping stdout to FIFO# (stderr stays attached to TTY for child process colors/progress)- command "${_WORKTRUNK_CMD:-wt}" "$@" >"$fifo_path" &- runner_pid=$!+ # For bash, redirect stderr for the { & } to suppress job notifications,+ # but keep command's stderr on the original fd+ if [[ -n "${BASH_VERSION:-}" ]]; then+ exec 9>&2+ { command "${_WORKTRUNK_CMD:-wt}" "$@" >"$fifo_path" 2>&9 & } 2>/dev/null+ runner_pid=$!+ exec 9>&-+ else+ command "${_WORKTRUNK_CMD:-wt}" "$@" >"$fifo_path" &+ runner_pid=$!+ fi# Parse directives as they stream inwhile IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; dodiff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snapindex 03e035d54..c9bcc9753 100644--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap@@ -41,9 +41,7 @@ if command -v wt >/dev/null 2>&1 || [[ -n "${WORKTRUNK_BIN:-}" ]]; then# systems may capture functions but not environment variables.wt_exec() {# Disable job control notifications in zsh (prevents "[1] 12345" / "[1] + done" messages)- # Zsh prints these when background jobs start/complete in interactive shells- # Note: Bash also prints these, but `set +m` doesn't work within functions to suppress them- # Bash notifications are less intrusive (completion message only at next prompt vs inline)+ # For bash, this is handled at the backgrounding site using fd redirectionif [[ -n "${ZSH_VERSION:-}" ]]; thensetopt LOCAL_OPTIONS NO_MONITORfi@@ -79,8 +77,17 @@ wt_exec() {# Run worktrunk in background, piping stdout to FIFO# (stderr stays attached to TTY for child process colors/progress)- command "${_WORKTRUNK_CMD:-wt}" "$@" >"$fifo_path" &- runner_pid=$!+ # For bash, redirect stderr for the { & } to suppress job notifications,+ # but keep command's stderr on the original fd+ if [[ -n "${BASH_VERSION:-}" ]]; then+ exec 9>&2+ { command "${_WORKTRUNK_CMD:-wt}" "$@" >"$fifo_path" 2>&9 & } 2>/dev/null+ runner_pid=$!+ exec 9>&-+ else+ command "${_WORKTRUNK_CMD:-wt}" "$@" >"$fifo_path" &+ runner_pid=$!+ fi# Parse directives as they stream inwhile IFS= read -r -d '' chunk || [[ -n "$chunk" ]]; do