diff --git a/w7/student_tests.sh b/w7/student_tests.sh new file mode 100644 index 0000000000000000000000000000000000000000..6187d2ea27414ec744d383dc8cbdff6e7c472eed --- /dev/null +++ b/w7/student_tests.sh @@ -0,0 +1,111 @@ +#!/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 +}