Select Git revision
-
Peter Mangelsdorf authoredPeter Mangelsdorf authored
To learn more about this project, read the wiki.
student_tests.sh 1.72 KiB
#!/usr/bin/env bats
# File: student_tests.sh
#
# Create your unit tests suit in this file
@test "Example: check ls runs without errors" {
run ./dsh <<EOF
ls
EOF
# Assertions
[ "$status" -eq 0 ]
}
# Test: Basic command execution
@test "Basic command execution: echo" {
run ./dsh <<EOF
echo Hello World
EOF
output=$(echo "$output" | tr -d '\r')
echo "Captured output: $output" # Debugging
[ "$status" -eq 0 ]
[[ "$output" == *"Hello World"* ]]
}
# Test: Input Redirection (`<`)
@test "Input Redirection: wc -w < test_input.txt" {
echo "Hello World" > test_input.txt
run ./dsh <<EOF
wc -w < test_input.txt
EOF
output=$(echo "$output" | tr -d '\r')
echo "Captured output: $output" # Debugging
[ "$status" -eq 0 ]
[[ "$output" == *"2"* ]]
rm -f test_input.txt
}
# Test: Built-in Commands (`cd`)
@test "Built-in command: cd" {
run ./dsh <<EOF
cd /tmp
pwd
EOF
[ "$status" -eq 0 ]
[[ "$output" =~ "/tmp" ]]
}
# Test: Built-in Commands (`exit`)
@test "Built-in command: exit" {
run ./dsh <<EOF
exit
EOF
[ "$status" -eq 0 ]
}
@test "Output Redirection: echo Hello > test_output.txt" {
rm -f test_output.txt # Ensure file does not exist before test
run ./dsh <<EOF
echo Hello > test_output.txt
EOF
sleep 1 # Give time for file creation
echo "Debug: Checking test_output.txt after execution..."
ls -l test_output.txt
[ "$status" -eq 0 ]
if [ ! -f test_output.txt ]; then
echo "Test Failed: Output file test_output.txt does not exist!"
exit 1
fi
trimmed_output=$(cat test_output.txt | tr -d '\r')
echo "Captured output: $trimmed_output"
[[ "$trimmed_output" == *"Hello"* ]]
rm -f test_output.txt
}