From f7b92da54d0c35e68364b0f3b83e6090cc8d28a8 Mon Sep 17 00:00:00 2001 From: Aryaz Eghbali Date: Tue, 20 Feb 2024 15:09:06 +0100 Subject: [PATCH 1/2] Fixed tests to run run_analysis --- src/dynapyt/run_analysis.py | 23 ++++++++++++++++++----- src/dynapyt/runtime.py | 35 ++++++++++++++++++++++------------- tests/run_single_test.py | 19 ++++++++----------- 3 files changed, 48 insertions(+), 29 deletions(-) diff --git a/src/dynapyt/run_analysis.py b/src/dynapyt/run_analysis.py index e63db94..9e596a3 100644 --- a/src/dynapyt/run_analysis.py +++ b/src/dynapyt/run_analysis.py @@ -3,20 +3,31 @@ import importlib from os.path import abspath from tempfile import gettempdir -from shutil import rmtree import sys from pathlib import Path from . import runtime as _rt def run_analysis( - entry: str, analyses: List[str], name: str = None, coverage: bool = False + entry: str, + analyses: List[str], + name: str = None, + coverage: bool = False, + coverage_dir: str = None, ): - coverage_dir = Path(gettempdir()) / "dynapyt_coverage" + global _rt + _rt = importlib.reload(_rt) + if coverage: - coverage_dir.mkdir(exist_ok=True) + if coverage_dir is None: + coverage_path = Path(gettempdir()) / "dynapyt_coverage" + coverage_path.mkdir(exist_ok=True) + else: + coverage_path = Path(coverage) + coverage_path.mkdir(exist_ok=True) + _rt.set_coverage(coverage_path) else: - rmtree(str(coverage_dir), ignore_errors=True) + _rt.set_coverage(None) analyses_file = Path(gettempdir()) / "dynapyt_analyses.txt" if analyses_file.exists(): @@ -38,6 +49,8 @@ def run_analysis( globals_dict["__file__"] = entry_full_path exec(open(entry_full_path).read(), globals_dict) else: + if importlib.util.find_spec(entry) is None: + raise ValueError(f"Could not find entry {entry}") importlib.import_module(entry) _rt.end_execution() diff --git a/src/dynapyt/runtime.py b/src/dynapyt/runtime.py index 89d87c9..844391d 100644 --- a/src/dynapyt/runtime.py +++ b/src/dynapyt/runtime.py @@ -15,20 +15,19 @@ analyses = None covered = None +coverage_path = None current_file = None end_execution_called = False def end_execution(): - global covered, end_execution_called + global covered, coverage_path, end_execution_called if end_execution_called: return end_execution_called = True call_if_exists("end_execution") if covered is not None: - coverage_file = ( - Path(tempfile.gettempdir()) / "dynapyt_coverage" / "covered.jsonl" - ) + coverage_file = coverage_path / "covered.jsonl" with FileLock(f"{str(coverage_file)}.lock"): if coverage_file.exists(): existing_coverage = {} @@ -59,15 +58,25 @@ def end_execution(): def set_analysis(new_analyses: List[Any]): global analyses, covered - if analyses is None: - analyses = [] - coverage_dir = Path(tempfile.gettempdir()) / "dynapyt_coverage" - if coverage_dir.exists(): - covered = {} - signal.signal(signal.SIGINT, end_execution) - signal.signal(signal.SIGTERM, end_execution) - atexit.register(end_execution) - analyses = load_analyses(new_analyses) + analyses = [] + signal.signal(signal.SIGINT, end_execution) + signal.signal(signal.SIGTERM, end_execution) + atexit.register(end_execution) + analyses = load_analyses(new_analyses) + + +def set_coverage(coverage_path: Path): + if coverage_path is not None: + global covered + covered = {} + coverage_path.mkdir(exist_ok=True) + coverage_file = coverage_path / "covered.jsonl" + if coverage_file.exists(): + with open(str(coverage_file), "r") as f: + content = f.read().splitlines() + for c in content: + tmp = json.loads(c) + covered.update(tmp) def filtered(func, f, args): diff --git a/tests/run_single_test.py b/tests/run_single_test.py index 1e3ed08..4baceee 100644 --- a/tests/run_single_test.py +++ b/tests/run_single_test.py @@ -68,22 +68,19 @@ def test_runner(directory_pair: Tuple[str, str], capsys): instrument_file(join(abs_dir, "__init__.py"), selected_hooks) # analyze - # class_ = getattr(module, "TestAnalysis") - analysis_instances = [class_[1]() for class_ in analysis_classes] - rt.analyses = None - rt.set_analysis(analysis_instances) captured = capsys.readouterr() # clear stdout # print(f"Before analysis: {captured.out}") # for debugging purposes - for analysis_instance in analysis_instances: - if hasattr(analysis_instance, "begin_execution"): - analysis_instance.begin_execution() if run_as_file: run_analysis(program_file, [f"{module_prefix}.analysis.TestAnalysis"]) else: - import_module(f"{module_prefix}.program") - for analysis_instance in analysis_instances: - if hasattr(analysis_instance, "end_execution"): - analysis_instance.end_execution() + run_analysis( + f"{module_prefix}.program", + [ + f"{module_prefix}.analysis.{ac[0]}" + for ac in analysis_classes + if not ac[0].endswith("BaseAnalysis") + ], + ) # check output expected_file = join(abs_dir, "expected.txt") From 934429e1d87bdf83b2c3b27a7e9653662c18cb37 Mon Sep 17 00:00:00 2001 From: Aryaz Eghbali Date: Tue, 20 Feb 2024 15:23:26 +0100 Subject: [PATCH 2/2] Updated github actions versions --- .github/workflows/docs.yml | 6 +++--- .github/workflows/python-publish.yml | 4 ++-- .github/workflows/tests.yml | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 8238194..dbe4dd3 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -18,8 +18,8 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 with: python-version: '3.10' @@ -31,7 +31,7 @@ jobs: - run: python docs/hierarchy2md.py - run: pdoc src/dynapyt/analyses/TraceAll.py -o docs/ - - uses: actions/upload-pages-artifact@v1 + - uses: actions/upload-pages-artifact@v3 with: path: docs/ diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index c6909ea..460c116 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -22,9 +22,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python - uses: actions/setup-python@v3 + uses: actions/setup-python@v5 with: python-version: '3.x' - name: Install dependencies diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e2b05e9..6f6cd17 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,9 +13,9 @@ jobs: runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - name: Install dependencies