Skip to content
Snippets Groups Projects
Commit 5b6246b2 authored by Ziheng Chen's avatar Ziheng Chen
Browse files

Upload New File

parent 14afdd0a
Branches
No related tags found
No related merge requests found
#!/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
}
# Test: Append Redirection (`>>`)
@test "Append Redirection: echo Hello > test_append.txt && echo World >> test_append.txt" {
rm -f test_append.txt # Ensure file does not exist
run ./dsh <<EOF
echo Hello > test_append.txt
echo World >> test_append.txt
EOF
sleep 1 # Ensure file is written
[ "$status" -eq 0 ]
[ -f test_append.txt ] # Ensure file exists
trimmed_output=$(cat test_append.txt | tr -d '\r')
echo "Captured output: $trimmed_output" # Debugging
[[ "$trimmed_output" == *"Hello"* && "$trimmed_output" == *"World"* ]]
rm -f test_append.txt
}
# Extra Credit: Combining Pipes and Redirection
@test "Extra Credit: ls | grep .c > output.txt" {
rm -f output.txt # Ensure file does not exist
run ./dsh <<EOF
ls | grep .c > output.txt
EOF
sleep 1 # Ensure file is written
[ "$status" -eq 0 ]
[ -f output.txt ] # Ensure file exists
trimmed_output=$(cat output.txt | tr -d '\r')
echo "Captured output: $trimmed_output" # Debugging
[[ "$trimmed_output" == *"dshlib.c"* ]]
rm -f output.txt
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment