diff --git a/w6/student_tests.sh b/w6/student_tests.sh new file mode 100644 index 0000000000000000000000000000000000000000..8a754f420b8535181c541a2efd59cd48fb6520ea --- /dev/null +++ b/w6/student_tests.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bats + +# File: student_tests.sh +# +# Create your unit tests suit in this file + +@test "Check if shell starts" { + run ./dsh <<< "exit" + [ "$status" -eq 0 ] + # These echo commands will help with debugging and will only print + #if the test fails + echo "Captured stdout:" + echo "Output: $output" + echo "Exit Status: $status" + echo "${stripped_output} -> ${expected_output}" + + # Check exact match + [ "$stripped_output" = "$expected_output" ] +} + +@test "Example: check ls runs without errors" { + run ./dsh <<EOF +ls +EOF + + # Assertions + [ "$status" -eq 0 ] + # These echo commands will help with debugging and will only print + #if the test fails + echo "Captured stdout:" + echo "Output: $output" + echo "Exit Status: $status" + echo "${stripped_output} -> ${expected_output}" + + # Check exact match + [ "$stripped_output" = "$expected_output" ] +} + +@test "Create and navigate into new directory" { + run ./dsh <<EOF +mkdir -p /tmp/student_test_dir +cd /tmp/student_test_dir +pwd +EOF + + # Remove extra spaces but **preserve newlines** + stripped_output=$(echo "$output" | tr -s '[:space:]' | tr '\n' '|' ) + + # Adjust expected output to match actual shell behavior + expected_output="/tmp/student_test_dir|dsh2> dsh2> dsh2> dsh2> |cmd loop returned 0|" + + # Debugging output + echo "Captured stdout:" + echo "Output: $output" + echo "Exit Status: $status" + echo "${stripped_output} -> ${expected_output}" + + # Assertion: Check exact match + [ "$stripped_output" = "$expected_output" ] +} + +@test "Handle command execution failures gracefully" { + run ./dsh <<< "command_that_does_not_exist" + + # Expected error message + expected_error="execvp failed: No such file or directory" + + + + # Check if output contains the expected error message + [[ "$output" == *"$expected_error"* ]] + + # Debugging output + echo "Captured stdout:" + echo "Output: $output" + echo "Exit Status: $status" + echo "${stripped_output} -> ${expected_output}" + + # Assertion: Check exact match + [ "$stripped_output" = "$expected_output" ] +} + +@test "Detect too many arguments for built-in cd" { + run ./dsh <<< "cd /tmp extra_argument" + + # Expected error message + expected_error="error: cd command accepts at most 1 argument" + + + + # Check if output contains the expected error message + [[ "$output" == *"$expected_error"* ]] + + # Debugging output + echo "Captured stdout:" + echo "Output: $output" + echo "Exit Status: $status" + # Assertion: Check exact match + [ "$stripped_output" = "$expected_output" ] +}