mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
tests: implement a better integration test framework than shellspec
(for our usecase at least)
This commit is contained in:
parent
1f03cbbedb
commit
36c1b72eff
6 changed files with 148 additions and 75 deletions
105
test.sh
Executable file
105
test.sh
Executable file
|
|
@ -0,0 +1,105 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
declare -i FAILED=0
|
||||
declare -i SUCCEDED=0
|
||||
declare -i RAN=0
|
||||
NAME=""
|
||||
|
||||
failed_report()
|
||||
{
|
||||
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
||||
echo $NAME
|
||||
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++"
|
||||
echo "++++ Failed test:"
|
||||
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:"
|
||||
echo "echo '$(cat /tmp/input.minishell)' | ./minishell"
|
||||
}
|
||||
|
||||
failed()
|
||||
{
|
||||
failed_report >> report.txt
|
||||
echo -n "F"
|
||||
FAILED+=1
|
||||
}
|
||||
|
||||
succeded()
|
||||
{
|
||||
echo -n "."
|
||||
SUCCEDED+=1
|
||||
}
|
||||
|
||||
assert()
|
||||
{
|
||||
diff -q /tmp/got.minishell /tmp/expected.minishell > /dev/null && succeded || failed
|
||||
}
|
||||
|
||||
when_run()
|
||||
{
|
||||
cat > /tmp/input.minishell
|
||||
./minishell &> /tmp/got.minishell < /tmp/input.minishell
|
||||
RAN+=1
|
||||
NAME=$1
|
||||
}
|
||||
|
||||
expecting()
|
||||
{
|
||||
cat > /tmp/expected.minishell
|
||||
assert
|
||||
}
|
||||
|
||||
setup()
|
||||
{
|
||||
rm -f report.txt
|
||||
}
|
||||
|
||||
finalize()
|
||||
{
|
||||
echo
|
||||
echo "$RAN tests ran. $SUCCEDED succeded, $FAILED failed."
|
||||
if [ $SUCCEDED -eq $RAN ];
|
||||
then
|
||||
echo "All integration tests passed!"
|
||||
else
|
||||
echo "cat report.txt"
|
||||
echo "to see details of failed tests"
|
||||
fi
|
||||
}
|
||||
|
||||
setup
|
||||
|
||||
when_run <<EOF "empty inputs returns nothing"
|
||||
|
||||
EOF
|
||||
expecting <<EOF
|
||||
EOF
|
||||
|
||||
when_run <<EOF "simple command is parsed"
|
||||
echo hello
|
||||
EOF
|
||||
expecting <<EOF
|
||||
╰─ t_cmdgroup
|
||||
├─ t_cmdlist
|
||||
│ ├─ num_cmds = 1
|
||||
│ ╰─ cmd[0]
|
||||
│ ├─ t_cmdlist_item
|
||||
│ │ ╰─ t_pipeline
|
||||
│ │ ├─ num_cmd = 1
|
||||
│ │ ╰─ cmd[0]
|
||||
│ │ ╰─ t_simple_cmd
|
||||
│ │ ├─ words = [echo][hello]
|
||||
│ │ ╰─ (no redirections)
|
||||
│ ╰─ t_operator = END
|
||||
╰─ (no redirections)
|
||||
EOF
|
||||
|
||||
finalize
|
||||
Loading…
Add table
Add a link
Reference in a new issue