Skip to content

Commit

Permalink
Added test for multi file analysis coverage (#50)
Browse files Browse the repository at this point in the history
* Added multi file test for coverage

* Fixed cleanup
  • Loading branch information
AryazE authored Feb 21, 2024
1 parent bd51088 commit 830f9a7
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 18 deletions.
42 changes: 24 additions & 18 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 Expand Up @@ -143,8 +147,10 @@ def test_runner(directory_pair: Tuple[str, str], capsys):

# restore uninstrumented program and remove temporary files
try:
move(orig_program_file, program_file)
remove(join(abs_dir, "program-dynapyt.json"))
for program_file in program_files:
orig_program_file = program_file + ".orig"
move(orig_program_file, program_file)
remove(join(abs_dir, f"{program_file[:-3]}-dynapyt.json"))
if cov:
remove(join(abs_dir, "covered.jsonl"))
remove(join(abs_dir, "covered.jsonl.lock"))
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))
6 changes: 6 additions & 0 deletions tests/test_coverage/multi_file/program2.py
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 830f9a7

Please sign in to comment.