From aacf6dd9ebdb4d55b432ea1d4213093fe35e0ad1 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 6 Feb 2026 06:18:58 +0000 Subject: test: add comprehensive CLI test suite Add 56 test cases across 4 test suites to verify the yue command line tool functionality: - Basic options test: -h, --help, -v, --version flags - Compilation test: file/directory compilation with various options - Error handling test: syntax errors, file not found, edge cases - Execution test: -e option, script arguments, macros The test framework includes helper functions for assertions and test environment setup. All tests can be run via `bash spec/cli/run_all_tests.sh`. Co-Authored-By: Claude Sonnet 4.5 --- spec/cli/cli_test_helper.sh | 183 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100755 spec/cli/cli_test_helper.sh (limited to 'spec/cli/cli_test_helper.sh') diff --git a/spec/cli/cli_test_helper.sh b/spec/cli/cli_test_helper.sh new file mode 100755 index 0000000..ade1546 --- /dev/null +++ b/spec/cli/cli_test_helper.sh @@ -0,0 +1,183 @@ +#!/bin/bash +# CLI Test Helper for YueScript +# Provides utility functions for testing the yue command line tool + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# Test counters +TESTS_RUN=0 +TESTS_PASSED=0 +TESTS_FAILED=0 + +# Get the yue binary path +YUE_BIN="${YUE_BIN:-./bin/debug/yue}" + +# Check if yue binary exists +check_yue_binary() { + if [ ! -f "$YUE_BIN" ]; then + echo -e "${RED}Error: yue binary not found at $YUE_BIN${NC}" + echo "Please build the project first or set YUE_BIN environment variable" + exit 1 + fi + if [ ! -x "$YUE_BIN" ]; then + echo -e "${RED}Error: yue binary is not executable${NC}" + exit 1 + fi +} + +# Assert that a command succeeds +assert_success() { + local description="$1" + shift + TESTS_RUN=$((TESTS_RUN + 1)) + + if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + else + echo -e "${RED}✗${NC} $description" + echo -e " ${YELLOW}Exit code: $?${NC}" + echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}" + echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi +} + +# Assert that a command fails +assert_failure() { + local description="$1" + shift + TESTS_RUN=$((TESTS_RUN + 1)) + + if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then + echo -e "${RED}✗${NC} $description (expected failure but succeeded)" + echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}" + echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + else + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + fi +} + +# Assert that output contains expected string +assert_output_contains() { + local description="$1" + local expected="$2" + shift 2 + TESTS_RUN=$((TESTS_RUN + 1)) + + if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then + if grep -qF -- "$expected" /tmp/test_stdout.txt || grep -qF -- "$expected" /tmp/test_stderr.txt; then + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + else + echo -e "${RED}✗${NC} $description (output doesn't contain '$expected')" + echo -e " ${YELLOW}STDOUT:$(cat /tmp/test_stdout.txt)${NC}" + echo -e " ${YELLOW}STDERR:$(cat /tmp/test_stderr.txt)${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi + else + echo -e "${RED}✗${NC} $description (command failed)" + echo -e " ${YELLOW}Exit code: $?${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi +} + +# Assert that output equals expected string +assert_output_equals() { + local description="$1" + local expected="$2" + shift 2 + TESTS_RUN=$((TESTS_RUN + 1)) + + if "$@" > /tmp/test_stdout.txt 2> /tmp/test_stderr.txt; then + local actual=$(cat /tmp/test_stdout.txt) + if [ "$actual" = "$expected" ]; then + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + else + echo -e "${RED}✗${NC} $description (output mismatch)" + echo -e " ${YELLOW}Expected: '$expected'${NC}" + echo -e " ${YELLOW}Actual: '$actual'${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi + else + echo -e "${RED}✗${NC} $description (command failed)" + echo -e " ${YELLOW}Exit code: $?${NC}" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi +} + +# Assert file exists +assert_file_exists() { + local description="$1" + local filepath="$2" + TESTS_RUN=$((TESTS_RUN + 1)) + + if [ -f "$filepath" ]; then + echo -e "${GREEN}✓${NC} $description" + TESTS_PASSED=$((TESTS_PASSED + 1)) + return 0 + else + echo -e "${RED}✗${NC} $description (file not found: $filepath)" + TESTS_FAILED=$((TESTS_FAILED + 1)) + return 1 + fi +} + +# Print test summary +print_summary() { + echo "" + echo "======================================" + echo "Test Summary" + echo "======================================" + echo "Total tests: $TESTS_RUN" + echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}" + echo -e "Failed: ${RED}$TESTS_FAILED${NC}" + echo "======================================" + + if [ $TESTS_FAILED -eq 0 ]; then + echo -e "${GREEN}All tests passed!${NC}" + return 0 + else + echo -e "${RED}Some tests failed!${NC}" + return 1 + fi +} + +# Setup test environment +setup_test_env() { + # Create temporary directory for test files + TEST_TMP_DIR=$(mktemp -d) + export TEST_TMP_DIR + trap "rm -rf $TEST_TMP_DIR" EXIT + echo "Test directory: $TEST_TMP_DIR" +} + +# Get test tmp dir +get_test_tmp_dir() { + echo "$TEST_TMP_DIR" +} + +# Create a test yue file +create_test_file() { + local filepath="$1" + local content="$2" + mkdir -p "$(dirname "$filepath")" + echo "$content" > "$filepath" +} -- cgit v1.2.3-55-g6feb