main zmx / test / stdin_run.bats
Eric Bower  ·  2026-05-20
 1#!/usr/bin/env bats
 2# Tests for stdin piped to `zmx run` sessions.
 3#
 4# Verifies that `zmx run` sessions always use bash (not the user's $SHELL),
 5# so piping commands via stdin works without quoting issues regardless of
 6# the user's default shell.
 7
 8load test_helper
 9
10# ============================================================================
11# Session shell is always bash
12# ============================================================================
13
14@test "run: session uses bash regardless of SHELL env" {
15  run timeout 10 env SHELL=/usr/bin/fish "$ZMX" run test-shell-check echo 'hello'
16  [ "$status" -eq 0 ]
17
18  sleep 0.3
19  run "$ZMX" history test-shell-check
20  # Task marker uses $? (bash syntax), not $status (fish syntax)
21  [[ "$output" == *'ZMX_TASK_COMPLETED:'* ]]
22  [[ "$output" == *'$?'* ]]
23}
24
25# ============================================================================
26# Stdin piped to run
27# ============================================================================
28
29@test "run: stdin pipe executes command" {
30  run bash -c 'printf "echo stdin-marker-abc123\n" | timeout 10 "$0" run test-stdin-basic' "$ZMX"
31  [ "$status" -eq 0 ]
32
33  sleep 0.3
34  run "$ZMX" history test-stdin-basic
35  [[ "$output" == *"stdin-marker-abc123"* ]]
36}
37
38@test "run: stdin with special characters passes through unmangled" {
39  run bash -c 'printf "echo '\''hello \$USER \$(whoami) \\\"double\\\" ; # comment'\''\n" | timeout 10 "$0" run test-stdin-special' "$ZMX"
40  [ "$status" -eq 0 ]
41
42  sleep 0.3
43  run "$ZMX" history test-stdin-special
44  [[ "$output" == *'$USER'* ]]
45  [[ "$output" == *'$(whoami)'* ]]
46}
47
48@test "run: multiline stdin script executes all lines" {
49  local script
50  script=$(printf 'echo line-one-aaa\necho line-two-bbb\necho line-three-ccc\n')
51  run bash -c 'printf "%s" "$1" | timeout 10 "$0" run test-stdin-multi' "$ZMX" "$script"
52  [ "$status" -eq 0 ]
53
54  sleep 0.5
55  run "$ZMX" history test-stdin-multi
56  [[ "$output" == *"line-one-aaa"* ]]
57  [[ "$output" == *"line-two-bbb"* ]]
58  [[ "$output" == *"line-three-ccc"* ]]
59}
60
61@test "run: stdin with heredoc in script" {
62  # Heredoc delimiter as the last line of stdin should work now that
63  # the task marker is sent on its own line.
64  local script
65  script=$(printf "cat <<'EOF'\nThis has \"double\" and 'single' quotes\nand \$variables that should not expand\nEOF\n")
66  run bash -c 'printf "%s" "$1" | timeout 10 "$0" run test-stdin-heredoc' "$ZMX" "$script"
67  [ "$status" -eq 0 ]
68
69  sleep 0.5
70  run "$ZMX" history test-stdin-heredoc
71  [[ "$output" == *'$variables that should not expand'* ]]
72}
73
74@test "run: args-only still works" {
75  run timeout 10 env SHELL=/bin/bash "$ZMX" run test-args-only echo args-only-marker-999
76  [ "$status" -eq 0 ]
77
78  sleep 0.3
79  run "$ZMX" history test-args-only
80  [[ "$output" == *"args-only-marker-999"* ]]
81}