Skip to content
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
10 changes: 6 additions & 4 deletions lib/platform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ spawn_terminal_in() {
case "$os" in
darwin)
# Escape variables for AppleScript string interpolation
local safe_path safe_title safe_cmd
safe_path=$(_escape_applescript "$path")
local shell_path safe_cd safe_title safe_cmd
# Quote for the terminal shell first, then for the AppleScript string.
shell_path="${path//\'/\'\\\'\'}"
safe_cd=$(_escape_applescript "cd -- '$shell_path'")
safe_title=$(_escape_applescript "$title")
safe_cmd=$(_escape_applescript "$cmd")

Expand All @@ -109,7 +111,7 @@ spawn_terminal_in() {
tell current window
create tab with default profile
tell current session
write text "cd \"$safe_path\""
write text "$safe_cd"
set name to "$safe_title"
$iterm_cmd_line
end tell
Expand All @@ -119,7 +121,7 @@ spawn_terminal_in() {
else
osascript <<-EOF 2>/dev/null || true
tell application "Terminal"
do script "cd \"$safe_path\"; $safe_cmd"
do script "$safe_cd; $safe_cmd"
set custom title of front window to "$safe_title"
end tell
EOF
Expand Down
37 changes: 37 additions & 0 deletions tests/platform.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,40 @@ EOF
grep -Fx -- "-lc" "$BATS_TMPDIR/gnome-terminal-args"
grep -Fx -- "echo hello" "$BATS_TMPDIR/gnome-terminal-args"
}

@test "macOS terminals treat command substitutions and quotes in paths as literal data" {
export OSTYPE=darwin
local terminal
for terminal in iTerm Terminal; do
export GTR_TEST_TERMINAL="$terminal"
export GTR_TEST_SCRIPT="$_platform_mock_bin/applescript"
cat > "$_platform_mock_bin/osascript" <<'EOF'
#!/usr/bin/env bash
if [ "${1:-}" = -e ]; then
printf '%s\n' "$GTR_TEST_TERMINAL"
else
cat > "$GTR_TEST_SCRIPT"
fi
EOF
chmod +x "$_platform_mock_bin/osascript"
local path="$_platform_mock_bin/"'$(touch injected) `touch injected2` '\''quote"back\slash'
mkdir -p "$path"
local expected_path
expected_path=$(cd -P "$path" && pwd)
spawn_terminal_in "$path" 'title' ''
# Decode the AppleScript string and run the actual shell command it carries.
python3 - "$GTR_TEST_SCRIPT" "$_platform_mock_bin/command" <<'PYCODE'
import re, sys
s = open(sys.argv[1]).read()
m = re.search(r'(?:write text|do script) "((?:\\.|[^"\\])*)"', s)
assert m, s
command = re.sub(r'\\([\\"])', r'\1', m.group(1))
open(sys.argv[2], 'w').write(command + '\npwd -P\n')
PYCODE
run bash "$_platform_mock_bin/command"
[ "$status" -eq 0 ]
[ "$output" = "$expected_path" ]
[ ! -e injected ]
[ ! -e injected2 ]
done
}
Loading