Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdstream support #168

Merged
merged 9 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions test/stdstream.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh

TRY_TOP="${TRY_TOP:-$(git rev-parse --show-toplevel --show-superproject-working-tree 2>/dev/null || echo "${0%/*}")}"
TRY="$TRY_TOP/try"

cmdfile="$(mktemp)"

cat > "$cmdfile" <<'EOF'
read x < /dev/stdin
ezrizhu marked this conversation as resolved.
Show resolved Hide resolved
echo $((x * 2)) > /dev/stdout
echo $((x * 3)) > /dev/stderr

EOF

chmod +x "$cmdfile"

try_stdout=$(mktemp)
try_stderr=$(mktemp)
sh_stdout=$(mktemp)
sh_stderr=$(mktemp)

# test stdout
echo 5 | "$TRY" "$cmdfile" >"$try_stdout" 2>"$try_stderr"
echo 5 | sh "$cmdfile" >"$sh_stdout" 2>"$sh_stderr"

diff "$try_stdout" "$sh_stdout" || exit 1

# using grep because there's try errors printed
grep -q 15 "$try_stderr"
grep -q 15 "$sh_stderr"

rm "$try_stdout" "$try_stderr" "$sh_stdout" "$sh_stderr"

cat > "$cmdfile" <<'EOF'
read x <&0
echo $((x * 2)) >&1
echo $((x * 3)) >&2

EOF

# test stdout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not test for exact output from both at once by capturing output in mktemped files?

echo 5 | "$TRY" "$cmdfile" >"$try_stdout" 2>"$try_stderr"
echo 5 | sh "$cmdfile" >"$sh_stdout" 2>"$sh_stderr"

diff "$try_stdout" "$sh_stdout" || exit 1

# using grep because there's try errors printed
grep -q 15 "$try_stderr"
grep -q 15 "$sh_stderr"

rm "$try_stdout" "$try_stderr" "$sh_stdout" "$sh_stderr"
7 changes: 7 additions & 0 deletions try
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ unshare --root="$SANDBOX_DIR/temproot" /bin/sh "$chroot_executable"
exitcode="$?"

# unmount the devices
rm "$sandbox_dir/temproot/dev/stdin"
rm "$sandbox_dir/temproot/dev/stdout"
rm "$sandbox_dir/temproot/dev/stderr"

unmount_devices "$SANDBOX_DIR"

exit $exitcode
Expand All @@ -262,6 +266,9 @@ unset START_DIR SANDBOX_DIR UNION_HELPER DIRS_AND_MOUNTS TRY_EXIT_STATUS
unset script_to_execute chroot_executable try_mount_log

mount -t proc proc /proc &&
ln -s /proc/self/fd/0 /dev/stdin &&
ezrizhu marked this conversation as resolved.
Show resolved Hide resolved
ln -s /proc/self/fd/1 /dev/stdout &&
ln -s /proc/self/fd/2 /dev/stderr &&
cd "$START_DIR" &&
ezrizhu marked this conversation as resolved.
Show resolved Hide resolved
. "$script_to_execute"
EOF
Expand Down