Skip to content

Commit

Permalink
Added multi file test for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AryazE committed Feb 21, 2024
1 parent bd51088 commit ebda4ca
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 16 deletions.
36 changes: 20 additions & 16 deletions tests/run_single_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def correct_output(expected: str, actual: str) -> bool:


def correct_coverage(expected: str, actual: str) -> bool:
actual_lines = actual.strip().split("\n")
expected_lines = expected.strip().split("\n")
actual_lines = sorted(actual.strip().split("\n"))
expected_lines = sorted(expected.strip().split("\n"))
if len(actual_lines) != len(expected_lines):
return False
for i in range(len(actual_lines)):
Expand Down Expand Up @@ -75,20 +75,24 @@ def test_runner(directory_pair: Tuple[str, str], capsys):
coverage_dir = None

# instrument
program_file = str(Path(abs_dir) / "program.py")
orig_program_file = str(Path(abs_dir) / "program.py.orig")
# make sure to instrument the uninstrumented version
run_as_file = False
with open(program_file, "r") as file:
src = file.read()
if "DYNAPYT: DO NOT INSTRUMENT" in src:
if not exists(orig_program_file):
pytest.fail(f"Could find only the instrumented program in {rel_dir}")
copyfile(orig_program_file, program_file)
elif "# DYNAPYT: Run as file" in src:
run_as_file = True

instrument_file(program_file, selected_hooks)
program_files = [str(f) for f in Path(abs_dir).glob("program*.py")]
for program_file in program_files:
# program_file = str(Path(abs_dir) / "program.py")
orig_program_file = program_file + ".orig"
# make sure to instrument the uninstrumented version
run_as_file = False
with open(program_file, "r") as file:
src = file.read()
if "DYNAPYT: DO NOT INSTRUMENT" in src:
if not exists(orig_program_file):
pytest.fail(
f"Could find only the instrumented program in {rel_dir}"
)
copyfile(orig_program_file, program_file)
elif "# DYNAPYT: Run as file" in src:
run_as_file = True

instrument_file(program_file, selected_hooks)

if exists(join(abs_dir, "__init__.py")) and not exists(
join(abs_dir, "__init__.py.orig")
Expand Down
15 changes: 15 additions & 0 deletions tests/test_coverage/multi_file/analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from dynapyt.analyses.BaseAnalysis import BaseAnalysis


class TestAnalysis(BaseAnalysis):
def __init__(self):
super().__init__()

def begin_execution(self):
print("begin execution")

def end_execution(self):
print("end execution")

def pre_call(self, dyn_ast, iid, call, pos_args, kw_args):
print(f"pre call {call.__name__}")
2 changes: 2 additions & 0 deletions tests/test_coverage/multi_file/exp_coverage.jsonl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"test_coverage/multi_file/program.py.orig": {"8": {"TestAnalysis": 2}, "5": {"TestAnalysis": 1}}}
{"test_coverage/multi_file/program2.py.orig": {"6": {"TestAnalysis": 1}}}
7 changes: 7 additions & 0 deletions tests/test_coverage/multi_file/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
begin execution
pre call baz
pre call bar
pre call foo
pre call print
216
end execution
8 changes: 8 additions & 0 deletions tests/test_coverage/multi_file/program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .program2 import bar


def baz(x):
return bar(x) ** 3


print(baz(2))
16 changes: 16 additions & 0 deletions tests/test_coverage/multi_file/program2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# DYNAPYT: DO NOT INSTRUMENT


import dynapyt.runtime as _rt

_dynapyt_ast_ = "/home/eghbalaz/Documents/PhD/Projects/DynaPyt/tests/test_coverage/multi_file/program2.py" + ".orig"
try:
def foo(x):
return x + 1


def bar(x):
return _rt._call_(_dynapyt_ast_, 2, foo, False, [("", x)], {}) * 2
except Exception as _dynapyt_exception_:
_rt._catch_(_dynapyt_exception_)

6 changes: 6 additions & 0 deletions tests/test_coverage/multi_file/program2.py.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def foo(x):
return x + 1


def bar(x):
return foo(x) * 2

0 comments on commit ebda4ca

Please sign in to comment.