From 629f38feb811a711e4f82577754a8ae671e83389 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 16 Nov 2026 18:43:20 +0800 Subject: [PATCH] test_bash_exe: what scripts written for Git Bash expect (bash-exe 0.2.2) Process substitution, /dev/stdin, /dev/stderr, /dev/urandom, /dev/zero, uname and OSTYPE as Git Bash, env and xargs finding .exe programs, sed on input not in UTF-8, #! interpreters, chmod, $BASH, a nested script's BASH_SOURCE, and a script's tools ahead of PATH. Co-Authored-By: BaoCode --- tool/test_bash_exe.ps1 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tool/test_bash_exe.ps1 b/tool/test_bash_exe.ps1 index 998b3abfd..9371a097a 100644 --- a/tool/test_bash_exe.ps1 +++ b/tool/test_bash_exe.ps1 @@ -132,6 +132,29 @@ try { Test-Bash 'SHELL' '"$SHELL" -c "echo nested"' '^nested\s*$' # `bash`, `sh` are this bash (with its tools), not WSL's or Git's. Test-Bash 'bash script.sh' 'echo "type -t rg" > nested.sh && bash nested.sh && sh -c "type -t rg"' '^builtin\nbuiltin\n$' + Test-Bash 'a script is its own BASH_SOURCE' 'echo ''[[ "${BASH_SOURCE[0]}" == "$0" ]] && echo main'' > main.sh && bash main.sh' '^main\n$' + Test-Bash 'BASH, the path of this bash' 'case "$BASH" in *[/\\]bash.exe) echo path ;; esac' '^path\n$' + + # What scripts written for Git Bash test for and use. + Test-Bash 'uname and OSTYPE, as Git Bash' 'uname -s; echo "$OSTYPE"' '^MINGW64_NT-\S+\nmsys\n$' + Test-Bash 'process substitution' 'diff <(printf "a\n") <(printf "b\n"); cat <(echo sub)' '^1c1\n< a\n---\n> b\nsub\n$' + Test-Bash '/dev/stdin and /dev/stderr' 'echo in | cat /dev/stdin; echo teed | tee /dev/stderr 2>&1 >/dev/null' '^in\nteed\n$' + Test-Bash '/dev/urandom and /dev/zero' 'head -c 16 /dev/urandom | wc -c; tr -dc a-z < /dev/urandom | head -c 5 | wc -c; head -c 3 /dev/zero | od -An -tx1' '^\s*16\n\s*5\n\s*00 00 00\n$' + Test-Bash 'env and xargs find .exe programs' 'env A=1 git --version; echo --version | xargs git' '^git version.*\ngit version' + Test-Bash 'sed, on input not in UTF-8' 'printf "caf\xe9 x\n" | sed s/x/y/ | od -An -tx1' '^\s*63 61 66 e9 20 79 0a\n$' + Test-Bash 'a #! line' 'printf "#!/usr/bin/env node\nconsole.log(process.argv[2])\n" > shebang.js && ./shebang.js arg' '^arg\n$' + Test-Bash 'chmod' 'touch ro.txt && chmod 444 ro.txt && [ ! -w ro.txt ] && chmod u+w ro.txt && [ -w ro.txt ] && echo ok' '^ok\n$' + + # A script run as `bash script.sh` has the tools ahead of PATH, as `-c` + # has (System32's `sort`, Git's `xargs` came first). + [IO.File]::WriteAllText((Join-Path $work 'tools.sh'), "printf 'b\na\n' | sort | tr '\n' ' '; printf 'x\n' | xargs -I{} echo `"got {}`"; exit 4`n") + $ran = Invoke-Bash @('tools.sh') + if ($ran.Status -ne 4 -or $ran.Out -ne "a b got x`n") { + Write-Host "FAIL a script's tools: $($ran.Status) '$($ran.Out)' $($ran.Err)" + $failed++ + } else { + Write-Host "ok a script's tools" + } # A job left in the background is not waited for, as bash's are not. $started = Get-Date