main zmx / test / stdin_run.bats
Eric Bower  ·  2026-07-17
 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# Stdin piped to run
12# ============================================================================
13
14@test "run: stdin pipe executes command" {
15  run bash -c 'printf "echo stdin-marker-abc123\n" | timeout 10 "$0" run test-stdin-basic' "$ZMX"
16  [ "$status" -eq 0 ]
17
18  wait_for_output test-stdin-basic stdin-marker-abc123
19  run "$ZMX" history test-stdin-basic
20  [[ "$output" == *"stdin-marker-abc123"* ]]
21}
22
23@test "run: stdin with special characters passes through unmangled" {
24  run bash -c 'printf "echo '\''hello \$USER \$(whoami) \\\"double\\\" ; # comment'\''\n" | timeout 10 "$0" run test-stdin-special' "$ZMX"
25  [ "$status" -eq 0 ]
26
27  wait_for_output test-stdin-special '$USER'
28  run "$ZMX" history test-stdin-special
29  [[ "$output" == *'$USER'* ]]
30  [[ "$output" == *'$(whoami)'* ]]
31}
32
33@test "run: multiline stdin script executes all lines" {
34  local script
35  script=$(printf 'echo line-one-aaa\necho line-two-bbb\necho line-three-ccc\n')
36  run bash -c 'printf "%s" "$1" | timeout 10 "$0" run test-stdin-multi' "$ZMX" "$script"
37  [ "$status" -eq 0 ]
38
39  wait_for_output test-stdin-multi line-three-ccc
40  run "$ZMX" history test-stdin-multi
41  [[ "$output" == *"line-one-aaa"* ]]
42  [[ "$output" == *"line-two-bbb"* ]]
43  [[ "$output" == *"line-three-ccc"* ]]
44}
45
46@test "run: stdin with heredoc in script" {
47  # Heredoc delimiter as the last line of stdin should work now that
48  # the task marker is sent on its own line.
49  local script
50  script=$(printf "cat <<'EOF'\nThis has \"double\" and 'single' quotes\nand \$variables that should not expand\nEOF\n")
51  run bash -c 'printf "%s" "$1" | timeout 10 "$0" run test-stdin-heredoc' "$ZMX" "$script"
52  [ "$status" -eq 0 ]
53
54  wait_for_output test-stdin-heredoc '$variables that should not expand'
55  run "$ZMX" history test-stdin-heredoc
56  [[ "$output" == *'$variables that should not expand'* ]]
57}
58
59@test "run: args-only still works" {
60  run timeout 10 env SHELL=/bin/bash "$ZMX" run test-args-only echo args-only-marker-999
61  [ "$status" -eq 0 ]
62
63  wait_for_output test-args-only args-only-marker-999
64  run "$ZMX" history test-args-only
65  [[ "$output" == *"args-only-marker-999"* ]]
66}