Skip to content

Commit

Permalink
ci: return non-zero if run lldb tests failed
Browse files Browse the repository at this point in the history
  • Loading branch information
cpunion committed Sep 30, 2024
1 parent 88cb607 commit 96c33a1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
31 changes: 14 additions & 17 deletions _lldb/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,18 @@ done
# Build the project
build_project "$package_path" || exit 1

# Prepare LLDB commands
lldb_commands=(
"command script import _lldb/test.py"
"script test.run_tests(\\\"${package_path}/debug.out\\\", [\\\"${package_path}/in.go\\\"], ${verbose}, ${interactive}, ${plugin_path})"
)

# Add quit command if not in interactive mode
if [ "$interactive" = False ]; then
lldb_commands+=("quit")
# Set up the result file path
result_file="/tmp/lldb_exit_code"

# Run LLDB with the test script
"$LLDB_PATH" -o "command script import _lldb/test.py" -o "script test.run_tests_with_result('${package_path}/debug.out', ['${package_path}/in.go'], $verbose, $interactive, $plugin_path, '$result_file')" -o "quit"

# Read the exit code from the result file
if [ -f "$result_file" ]; then
exit_code=$(cat "$result_file")
rm "$result_file"
exit "$exit_code"
else
echo "Error: Could not find exit code file"
exit 1
fi

# Run LLDB with prepared commands
lldb_command_string=""
for cmd in "${lldb_commands[@]}"; do
lldb_command_string+=" -O \"$cmd\""
done

eval "$LLDB_PATH $lldb_command_string" || exit 1
40 changes: 35 additions & 5 deletions _lldb/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def execute_tests(executable_path: str, test_cases: List[TestCase], verbose: boo
return results


def run_tests(executable_path: str, source_files: List[str], verbose: bool, interactive: bool, plugin_path: Optional[str]) -> None:
def run_tests(executable_path: str, source_files: List[str], verbose: bool, interactive: bool, plugin_path: Optional[str]) -> int:
test_cases = parse_expected_values(source_files)
if verbose:
log(f"Running tests for {', '.join(source_files)} with {executable_path}")
Expand All @@ -251,8 +251,8 @@ def run_tests(executable_path: str, source_files: List[str], verbose: bool, inte
verbose, interactive, plugin_path)
print_test_results(results)

if results.total != results.passed:
sys.exit(1)
# Return 0 if all tests passed, 1 otherwise
return 0 if results.failed == 0 else 1


def execute_test_case(debugger: LLDBDebugger, test_case: TestCase, all_variable_names: Set[str]) -> CaseResult:
Expand Down Expand Up @@ -349,6 +349,24 @@ def print_test_result(result: TestResult, verbose: bool) -> None:
log(f" Actual: {result.actual}")


def run_tests_with_result(executable_path: str, source_files: List[str], verbose: bool, interactive: bool, plugin_path: Optional[str], result_path: str) -> int:
try:
exit_code = run_tests(executable_path, source_files,
verbose, interactive, plugin_path)
except Exception as e:
log(f"An error occurred during test execution: {str(e)}")
exit_code = 2 # Use a different exit code for unexpected errors

try:
with open(result_path, 'w', encoding='utf-8') as f:
f.write(str(exit_code))
except IOError as e:
log(f"Error writing result to file {result_path}: {str(e)}")
# If we can't write to the file, we should still return the exit code

return exit_code


def main() -> None:
log(sys.argv)
parser = argparse.ArgumentParser(
Expand All @@ -360,12 +378,24 @@ def main() -> None:
parser.add_argument("-i", "--interactive", action="store_true",
help="Enable interactive mode on test failure")
parser.add_argument("--plugin", help="Path to the LLDB plugin")
parser.add_argument("--result-path", help="Path to write the result")
args = parser.parse_args()

plugin_path = args.plugin or os.path.join(os.path.dirname(
os.path.realpath(__file__)), "go_lldb_plugin.py")
run_tests(args.executable, args.sources,
args.verbose, args.interactive, plugin_path)

try:
if args.result_path:
exit_code = run_tests_with_result(args.executable, args.sources,
args.verbose, args.interactive, plugin_path, args.result_path)
else:
exit_code = run_tests(args.executable, args.sources,
args.verbose, args.interactive, plugin_path)
except Exception as e:
log(f"An unexpected error occurred: {str(e)}")
exit_code = 2 # Use a different exit code for unexpected errors

sys.exit(exit_code)


if __name__ == "__main__":
Expand Down

0 comments on commit 96c33a1

Please sign in to comment.