Skip to content

Commit

Permalink
Update test.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai authored Aug 24, 2024
1 parent 70cbc14 commit 5adebcc
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,44 @@ fi
run_test() {
local test_name="$1"
local command="$2"
local expected_stdout_pattern="$3"
local expected_stderr_pattern="$4"
local expected_stdout="$3"
local expected_stderr="$4"
local expected_status="$5"
local use_regex="${6:-false}"

echo "Running test: $test_name"
output=$(./exec2json $command | jq -r '.')

actual_stdout=$(echo "$output" | jq -r '.stdout')
actual_stderr=$(echo "$output" | jq -r '.stderr')
actual_stdout=$(echo "$output" | jq -r '.stdout' | tr -d '\n')
actual_stderr=$(echo "$output" | jq -r '.stderr' | tr -d '\n')
actual_status=$(echo "$output" | jq -r '.status')
actual_command=$(echo "$output" | jq -r '.command | join(" ")')

# Remove surrounding quotes if present
actual_stdout="${actual_stdout#\'}"
actual_stdout="${actual_stdout%\'}"

if [[ ! "$actual_stdout" =~ $expected_stdout_pattern ]] ||
[[ ! "$actual_stderr" =~ $expected_stderr_pattern ]] ||
stdout_match=false
if [ "$use_regex" = true ]; then
if [[ "$actual_stdout" =~ $expected_stdout ]]; then
stdout_match=true
fi
else
if [ "$actual_stdout" = "$expected_stdout" ]; then
stdout_match=true
fi
fi

if [ "$stdout_match" = false ] ||
[ "$actual_stderr" != "$expected_stderr" ] ||
[ "$actual_status" != "$expected_status" ] ||
[ "$actual_command" != "$command" ]; then
echo "Test case '$test_name' failed"
echo "Command: $command"
echo "Expected stdout pattern: $expected_stdout_pattern"
echo "Actual stdout:"
echo "$actual_stdout"
echo "Expected stderr pattern: $expected_stderr_pattern"
echo "Actual stderr:"
echo "$actual_stderr"
echo "Expected stdout pattern: $expected_stdout"
echo "Actual stdout: $actual_stdout"
echo "Expected stderr pattern: $expected_stderr"
echo "Actual stderr: $actual_stderr"
echo "Expected status: $expected_status"
echo "Actual status: $actual_status"
echo "Actual command: $actual_command"
Expand All @@ -51,15 +61,15 @@ run_test() {
}

# Test cases
run_test "Echo command" "echo 'Hello, World!'" "^Hello, World!$" "^$" "0"
run_test "ls command" "ls -l" "^total [0-9]+\n-.*" "^$" "0"
run_test "Non-existent command" "nonexistentcommand" "^$" ".*: command not found" "127"
run_test "Exit with non-zero status" "bash -c 'exit 42'" "^$" "^$" "42"
run_test "Command with arguments" "echo arg1 arg2 arg3" "^arg1 arg2 arg3$" "^$" "0"
run_test "Command with quoted arguments" "echo 'arg with spaces'" "^arg with spaces$" "^$" "0"
run_test "Command with environment variables" "bash -c 'echo \$PATH'" ".+" "^$" "0"
run_test "Command with stdin input" "cat" "^test input$" "^$" "0" < <(echo "test input")
run_test "Command with stderr output" "bash -c 'echo error >&2; echo output'" "^output$" "^error$" "0"
run_test "Long-running command" "sleep 2" "^$" "^$" "0"
run_test "Echo command" "echo 'Hello, World!'" "Hello, World!" "" "0"
run_test "ls command" "ls -l" "^total [0-9]+\n(-[rwx-]{9} [0-9]+ [a-zA-Z0-9]+ [a-zA-Z0-9]+ [0-9]+ [A-Za-z]+ [0-9]+ [0-9:]+ .+\n?)+$" "" "0" true
run_test "Non-existent command" "nonexistentcommand" "" ".*: command not found" "127" true
run_test "Exit with non-zero status" "bash -c 'exit 42'" "" "" "42"
run_test "Command with arguments" "echo arg1 arg2 arg3" "arg1 arg2 arg3" "" "0"
run_test "Command with quoted arguments" "echo 'arg with spaces'" "arg with spaces" "" "0"
run_test "Command with environment variables" "bash -c 'echo \$PATH'" ".*" "" "0" true
run_test "Command with stdin input" "cat" "test input" "" "0" < <(echo "test input")
run_test "Command with stderr output" "bash -c 'echo error >&2; echo output'" "output" "error" "0"
run_test "Long-running command" "sleep 2" "" "" "0"

echo "All tests passed!"

0 comments on commit 5adebcc

Please sign in to comment.