mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
218 lines
3.5 KiB
Bash
Executable file
218 lines
3.5 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
set -uo pipefail
|
|
|
|
declare -i FAILED=0
|
|
declare -i SUCCEDED=0
|
|
declare -i TODTODO=0
|
|
declare -i RAN=0
|
|
NAME=""
|
|
EXTRAENV=""
|
|
MINISHELL=$PWD/minishell
|
|
|
|
report_header()
|
|
{
|
|
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
|
echo $NAME
|
|
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
|
echo "++++ test input:"
|
|
}
|
|
|
|
failed_report()
|
|
{
|
|
NAME="failed: $NAME"
|
|
report_header
|
|
cat /tmp/input.minishell
|
|
echo "++++ Got output:"
|
|
cat /tmp/got.minishell
|
|
echo "++++ But expected:"
|
|
cat /tmp/expected.minishell
|
|
echo "++++ Diff:"
|
|
echo "(red is got, green is expected)"
|
|
diff --color=always /tmp/got.minishell /tmp/expected.minishell
|
|
echo "++++ Run this to debug:"
|
|
cat < /tmp/input.minishell > /tmp/input.failed.$FAILED.minishell
|
|
echo "cat /tmp/input.failed.$FAILED.minishell | ./minishell"
|
|
}
|
|
|
|
todo_report()
|
|
{
|
|
NAME="todo: $NAME"
|
|
report_header
|
|
cat /tmp/input.minishell
|
|
echo "++++ Got output:"
|
|
cat /tmp/got.minishell
|
|
echo "++++ But test was marked as todo."
|
|
echo "++++ Run this to debug:"
|
|
cat < /tmp/input.minishell > /tmp/input.todo.$TODO.minishell
|
|
echo "cat /tmp/input.todo.$TODO.minishell | ./minishell"
|
|
}
|
|
|
|
failed()
|
|
{
|
|
echo -n "F"
|
|
FAILED+=1
|
|
failed_report >> report.txt
|
|
}
|
|
|
|
succeded()
|
|
{
|
|
echo -n "."
|
|
SUCCEDED+=1
|
|
}
|
|
|
|
assert()
|
|
{
|
|
diff -q /tmp/got.minishell /tmp/expected.minishell > /dev/null && succeded || failed
|
|
}
|
|
|
|
when_run()
|
|
{
|
|
cat > /tmp/input.minishell
|
|
rm -rf /tmp/dir.minishell
|
|
mkdir -p /tmp/dir.minishell
|
|
pushd /tmp/dir.minishell > /dev/null
|
|
env $EXTRAENV $MINISHELL &> /tmp/got.minishell < /tmp/input.minishell
|
|
popd > /dev/null
|
|
EXTRAENV=""
|
|
RAN+=1
|
|
NAME=$1
|
|
}
|
|
|
|
expecting()
|
|
{
|
|
cat > /tmp/expected.minishell
|
|
assert
|
|
}
|
|
|
|
todo()
|
|
{
|
|
echo -n "t"
|
|
TODO+=1
|
|
todo_report >> report.txt
|
|
}
|
|
|
|
setup()
|
|
{
|
|
rm -f report.txt
|
|
}
|
|
|
|
finalize()
|
|
{
|
|
echo
|
|
echo "$RAN tests ran. $SUCCEDED succeded, $FAILED failed, $TODO todo."
|
|
if [ $SUCCEDED -eq $RAN ];
|
|
then
|
|
echo "All integration tests passed!"
|
|
else
|
|
echo "cat report.txt"
|
|
echo "to see details of failed/todo tests"
|
|
fi
|
|
exit $FAILED
|
|
}
|
|
|
|
setup
|
|
|
|
when_run <<EOF "empty inputs returns nothing"
|
|
|
|
EOF
|
|
expecting <<EOF
|
|
EOF
|
|
|
|
when_run <<EOF "simple commands are run"
|
|
echo no files:
|
|
ls -a
|
|
touch t
|
|
echo single file:
|
|
ls -a
|
|
EOF
|
|
expecting <<EOF
|
|
no files:
|
|
.
|
|
..
|
|
single file:
|
|
.
|
|
..
|
|
t
|
|
EOF
|
|
|
|
when_run <<EOF "unknown commands"
|
|
qwertyuiop
|
|
poiuytrewq
|
|
EOF
|
|
expecting <<EOF
|
|
minishell: qwertyuiop: command not found
|
|
minishell: poiuytrewq: command not found
|
|
EOF
|
|
|
|
when_run <<EOF "pwd works"
|
|
pwd
|
|
pwd hello
|
|
pwd hello there
|
|
pwd -O
|
|
pwd -O hello
|
|
pwd there -O hello
|
|
EOF
|
|
expecting <<EOF
|
|
/tmp/dir.minishell
|
|
/tmp/dir.minishell
|
|
/tmp/dir.minishell
|
|
/tmp/dir.minishell
|
|
/tmp/dir.minishell
|
|
/tmp/dir.minishell
|
|
EOF
|
|
|
|
when_run <<EOF "pwd works in any directory, can cd to absolute paths"
|
|
mkdir test
|
|
cd /tmp/dir.minishell/test
|
|
pwd
|
|
mkdir hi
|
|
cd /tmp/dir.minishell/test/hi
|
|
pwd
|
|
cd /tmp/dir.minishell/test
|
|
pwd
|
|
EOF
|
|
expecting <<EOF
|
|
/tmp/dir.minishell/test
|
|
/tmp/dir.minishell/test/hi
|
|
/tmp/dir.minishell/test
|
|
EOF
|
|
|
|
when_run <<EOF "cd will print error when going into a directory with no access"
|
|
mkdir test
|
|
chmod -x test
|
|
cd test
|
|
pwd
|
|
chmod +x test
|
|
EOF
|
|
expecting <<EOF
|
|
minishell: cd: Permission denied
|
|
/tmp/dir.minishell
|
|
EOF
|
|
|
|
EXTRAENV="HOME=/tmp/dir.minishell/fakehome"
|
|
when_run <<EOF "cd with no arguments goes to \$HOME"
|
|
mkdir fakehome
|
|
cd
|
|
pwd
|
|
EOF
|
|
expecting <<EOF
|
|
/tmp/dir.minishell/fakehome
|
|
EOF
|
|
|
|
EXTRAENV="-u HOME"
|
|
when_run <<EOF "cd with no arguments, with HOME unset, prints error message and does nothing"
|
|
cd
|
|
pwd
|
|
EOF
|
|
expecting <<EOF
|
|
minishell: cd: HOME not set
|
|
/tmp/dir.minishell
|
|
EOF
|
|
|
|
when_run <<EOF "quoted parentheses are not operators"
|
|
echo unclosed '('
|
|
EOF
|
|
todo
|
|
|
|
finalize
|