From 890e314f2fbf652e9e6f878f13ec554a9f7892a9 Mon Sep 17 00:00:00 2001 From: Disconnect3d Date: Wed, 24 Aug 2022 00:05:57 +0200 Subject: [PATCH] tests.sh: add [filter] and --pdb (#1092) --- pytests_launcher.py | 7 +++++-- tests.sh | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/pytests_launcher.py b/pytests_launcher.py index 77b86d146..6134072fa 100644 --- a/pytests_launcher.py +++ b/pytests_launcher.py @@ -1,9 +1,10 @@ - import os import pytest import sys print(sys.argv) +use_pdb = os.environ.get('USE_PDB') == '1' + sys._pwndbg_unittest_run = True CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) @@ -12,9 +13,11 @@ test = os.environ['PWNDBG_LAUNCH_TEST'] test = os.path.join(CURRENT_DIR, test) -# If you want to debug tests locally, add '--pdb' here args = [test, '-vvv', '-s', '--showlocals', '--color=yes'] +if use_pdb: + args.append('--pdb') + print('Launching pytest with args: %s' % args) return_code = pytest.main(args) diff --git a/tests.sh b/tests.sh index d9cf06230..039a441a8 100755 --- a/tests.sh +++ b/tests.sh @@ -1,5 +1,37 @@ #!/bin/bash +help_and_exit() { + echo "Usage: ./tests.sh [-p|--pdb] []" + echo " -p, --pdb enable pdb (Python debugger) post mortem debugger on failed tests" + echo " run only tests that match the regex" + exit 1; +} + +if [[ $# -gt 2 ]]; then + help_and_exit +fi + +USE_PDB=0 +TEST_NAME_FILTER="" + +while [[ $# -gt 0 ]]; do + case $1 in + -p|--pdb) + USE_PDB=1 + echo "Will run tests with Python debugger" + shift + ;; + *) + if [[ ! -z "${TEST_NAME_FILTER}" ]]; then + help_and_exit + fi + TEST_NAME_FILTER="$1" + shift + ;; + esac +done + + if [[ -z "$ZIGPATH" ]]; then # If ZIGPATH is not set, set it to $pwd/.zig # In Docker environment this should by default be set to /opt/zig @@ -22,13 +54,13 @@ if [ $? -eq 1 ]; then exit 1; fi -TESTS_LIST=$(echo -E "$TESTS_COLLECT_OUTPUT" | grep -o "tests/.*::.*") +TESTS_LIST=$(echo -E "$TESTS_COLLECT_OUTPUT" | grep -o "tests/.*::.*" | grep "${TEST_NAME_FILTER}") tests_passed_or_skipped=0 tests_failed=0 for test_case in ${TESTS_LIST}; do - COVERAGE_PROCESS_START=.coveragerc PWNDBG_LAUNCH_TEST="${test_case}" PWNDBG_DISABLE_COLORS=1 gdb --silent --nx --nh -ex 'py import coverage;coverage.process_startup()' --command gdbinit.py --command pytests_launcher.py --eval-command quit + COVERAGE_PROCESS_START=.coveragerc USE_PDB="${USE_PDB}" PWNDBG_LAUNCH_TEST="${test_case}" PWNDBG_DISABLE_COLORS=1 gdb --silent --nx --nh -ex 'py import coverage;coverage.process_startup()' --command gdbinit.py --command pytests_launcher.py --eval-command quit exit_status=$? if [ ${exit_status} -eq 0 ]; then