Files
sausage/userland/brush/source/brush-shell/tests/cases/compat/builtins/cd.yaml
T
2026-11-11 22:04:16 +00:00

225 lines
4.9 KiB
YAML

name: "Builtins: cd"
cases:
- name: "Basic cd usage"
ignore_stderr: true
stdin: |
echo "cd /"
cd /
echo $?
echo "pwd: $PWD"
echo "cd usr"
cd usr
echo $?
echo "pwd: $PWD"
echo "cd"
cd
echo $?
echo "pwd: $PWD"
- name: "cd to file"
ignore_stderr: true
test_files:
- path: "file"
contents: "file contents"
stdin: |
ls -1
cd file
echo $?
ls -1
- name: "cd -"
ignore_stderr: true
stdin: |
cd /
echo "pwd: $PWD"
cd usr
echo "pwd: $PWD"
echo "oldpwd: $OLDPWD"
cd -
echo $?
echo "pwd: $PWD"
- name: "cd ~"
stdin: |
mkdir ./my_home
export HOME="$(realpath ./my_home)"
echo "Set HOME: $(basename $HOME)"
(
echo "Subshell 1: HOME=$(basename $HOME)"
cd ~
echo "pwd: $(basename $PWD)"
)
(
echo "Subshell 2: HOME=$(basename $HOME)"
cd -L ~
echo "pwd: $(basename $PWD)"
)
(
echo "Subshell 3: HOME=$(basename $HOME)"
cd -P ~
echo "pwd: $(basename $PWD)"
)
- name: "cd with symlink"
stdin: |
mkdir -p ./level1/level2/level3
cd level1
ln -s ./level2/level3 ./symlink
# -L by default
(
cd ./symlink
echo "$(basename $PWD)"
cd ..
echo "$(basename $PWD)"
)
- name: "cd -L"
stdin: |
mkdir -p ./level1/level2/level3
cd level1
ln -s ./level2/level3 ./symlink
echo "[Case 1]"
(
cd ./symlink
echo "$(basename $PWD)"
cd -L ..
echo "$(basename $PWD)"
)
echo "[Case 2]"
(
cd -L ./symlink
echo "$(basename $PWD)"
cd ..
echo "$(basename $PWD)"
)
# without pwd
echo "[Case 3]"
(
cd -L ./symlink
echo "$(basename $PWD)"
export PWD=
cd -L ..
echo "$(basename $PWD)"
)
echo "[Case 4]"
(
cd -L ./symlink
export PWD=
# start a shell without $PWD
(
cd .
echo "$(basename $PWD)"
)
)
- name: "cd -P"
stdin: |
mkdir -p ./level1/level2/level3
cd level1
ln -s ./level2/level3 ./symlink
echo "[Case 1]"
(
cd ./symlink
echo "cd ./symlink => $(basename $PWD)"
cd -P ..
echo "cd -P .. => $(basename $PWD)"
)
echo "[Case 2]"
(
cd -P ./symlink
echo "cd -P ./symlink => $(basename $PWD)"
cd ..
echo "cd .. => $(basename $PWD)"
)
- name: "OLDPWD"
env:
OLDPWD: "/nonexistent/path"
stdin: |
mkdir subdir
echo "[0]"
echo ${OLDPWD+set}
echo "[1]"
echo ${OLDPWD-NOTSET}
echo "[2]"
declare -p OLDPWD 2>&1 | grep -o "declare -[a-z]* OLDPWD"
echo "[3]"
env | grep "^OLDPWD=" || echo "NOT_IN_ENV"
echo
echo "[Unsetting + cd'ing...]"
unset OLDPWD
cd $PWD/subdir
echo "[4]"
declare -p OLDPWD 2>&1 | grep -o "declare -[a-z]* OLDPWD"
echo "[5]"
env | grep -o "^OLDPWD="
- name: "cd .. from a deleted directory"
known_failure: true # TODO(cd): `cd ..` fails from a working directory that's been deleted
stdin: |
start=$PWD
mkdir gone
cd gone
rmdir "${start}/gone"
cd ..; echo "status: $?"
[[ $PWD == "${start}" ]] && echo "back at the start" || echo "somewhere else"
- name: "cd --help prints help on stdout"
stdin: |
cd --help >/dev/null 2>&1; echo "rc=$?"
[[ -n $(cd --help 2>/dev/null) ]] && echo "help on stdout"
[[ -z $(cd --help 2>&1 >/dev/null) ]] && echo "nothing on stderr"
- name: "cd takes -- and both of -L and -P"
stdin: |
mkdir -- -d
cd -- -d && basename -- "$PWD"
cd -- /; pwd
- name: "cd takes the last of -L and -P"
stdin: |
mkdir real; ln -s real link
cd -LP link; basename -- "$PWD"; cd ..
cd -PL link; basename -- "$PWD"; cd ..
- name: "cd --help prints what help cd prints"
stdin: |
[[ "$(cd --help; echo .)" == "$(help cd; echo .)" ]] && echo "same"
- name: "cd -h is an invalid option and prints nothing on stdout"
stdin: |
x=$(cd -h 2>/dev/null); echo "rc=$? len=${#x}"
[[ -n $(cd -h 2>&1 >/dev/null) ]] && echo "error on stderr"
- name: "cd --help after an operand is an operand"
known_failure: true # TODO(cd): --help anywhere is taken as a request for help
ignore_stderr: true
stdin: |
cd /tmp --help; echo "rc=$?"
- name: "cd --help with stdout closed or read-only fails quietly"
stdin: |
{ cd --help >&-; echo "rc=$?"; } 2>&1
{ cd --help 1</dev/null; echo "rc=$?"; } 2>&1
- name: "cd --help with stdout full fails quietly"
incompatible_platforms: ["macos"] # no /dev/full
stdin: |
{ cd --help >/dev/full; echo "rc=$?"; } 2>&1