Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add acceptance tests #6

Draft
wants to merge 16 commits into
base: trunk
Choose a base branch
from
12 changes: 7 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
Expand All @@ -27,8 +24,13 @@ jobs:
with:
go-version: ${{ matrix.go-version }}

- name: Build
- name: Build all packages
run: go build -v ./...

- name: Test
- name: Run Go tests
run: go test -v ./...

- name: Run bash tests
run: |
chmod +x test.sh
./test.sh
62 changes: 62 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

set -e
sanmai marked this conversation as resolved.
Show resolved Hide resolved

# Check if exec2json executable exists
if [ ! -f "./exec2json" ]; then
echo "Error: exec2json executable not found. Make sure to build it before running tests."
exit 1
fi

# Function to run a test case
run_test() {
local test_name="$1"
local command="$2"
local expected_stdout="$3"
local expected_stderr="$4"
local expected_status="$5"

echo "Running test: $test_name"
output=$(./exec2json $command)

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%\'}"
actual_stderr="${actual_stderr#\'}"
actual_stderr="${actual_stderr%\'}"

if [ "$actual_stdout" != "$expected_stdout" ] ||
[ "$actual_stderr" != "$expected_stderr" ] ||
[ "$actual_status" != "$expected_status" ] ||
[ "$actual_command" != "$command" ]; then
echo "Test case '$test_name' failed"
echo "Command: $command"
echo "Expected stdout: $expected_stdout"
echo "Actual stdout: $actual_stdout"
echo "Expected stderr: $expected_stderr"
echo "Actual stderr: $actual_stderr"
echo "Expected status: $expected_status"
echo "Actual status: $actual_status"
echo "Actual command: $actual_command"
exit 1
fi

echo "Test case '$test_name' passed"
echo
}

# Test cases
run_test "Echo command" "echo 'Hello, World!'" "Hello, World!" "" "0"
run_test "Echo multiple words" "echo word1 word2 word3" "word1 word2 word3" "" "0"
run_test "Echo with quotes" "echo 'quoted string'" "quoted string" "" "0"
run_test "Command with arguments" "printf '%s %s' arg1 arg2" "arg1 arg2" "" "0"
run_test "Exit with non-zero status" "bash -c 'exit 42'" "" "" "42"
run_test "Command with stderr output" "bash -c 'echo error >&2; echo output'" "output" "error" "0"
run_test "Non-existent command" "nonexistentcommand" "" "nonexistentcommand: command not found" "127"

echo "All tests passed!"
Loading