| 1 |
|
| 2 |
import json |
| 3 |
import pathlib |
| 4 |
import subprocess |
| 5 |
import sys |
| 6 |
import tempfile |
| 7 |
|
| 8 |
|
| 9 |
def smoketest(fname): |
| 10 |
outfile = pathlib.Path( |
| 11 |
tempfile.mkdtemp(prefix="scalene") / pathlib.Path("smoketest.json") |
| 12 |
) |
| 13 |
proc = subprocess.run( |
| 14 |
[ |
| 15 |
sys.executable, |
| 16 |
"-m", |
| 17 |
"scalene", |
| 18 |
"--cli", |
| 19 |
"--json", |
| 20 |
"--outfile", |
| 21 |
str(outfile), |
| 22 |
fname, |
| 23 |
], |
| 24 |
capture_output=True, |
| 25 |
) |
| 26 |
if proc.returncode != 0: |
| 27 |
print("Exited with a non-zero code:", proc.returncode) |
| 28 |
print("Stdout:", proc.stdout.decode("utf-8")) |
| 29 |
print("Stderr:", proc.stderr.decode("utf-8")) |
| 30 |
|
| 31 |
exit(proc.returncode) |
| 32 |
|
| 33 |
stderr = proc.stderr.decode("utf-8") |
| 34 |
try: |
| 35 |
with open(outfile, "r") as f: |
| 36 |
outfile_contents = f.read() |
| 37 |
scalene_json = json.loads(outfile_contents) |
| 38 |
except json.JSONDecodeError: |
| 39 |
print("Invalid JSON", stderr) |
| 40 |
exit(1) |
| 41 |
if len(scalene_json) == 0: |
| 42 |
print("No JSON output") |
| 43 |
exit(1) |
| 44 |
files = scalene_json["files"] |
| 45 |
if not len(files) > 0: |
| 46 |
print("No files found in output") |
| 47 |
exit(1) |
| 48 |
_fname = list(files.keys())[0] |
| 49 |
function_list = files[_fname]["functions"] |
| 50 |
exit_code = 0 |
| 51 |
|
| 52 |
|
| 53 |
expected_functions = ["doit1", "doit3"] |
| 54 |
unexpected_functions = ["doit2"] |
| 55 |
for fn_name in expected_functions: |
| 56 |
if not any(fn_name in f["line"] for f in function_list): |
| 57 |
print(f"Expected function '{fn_name}' not returned") |
| 58 |
exit_code = 1 |
| 59 |
for fn_name in unexpected_functions: |
| 60 |
if any(fn_name in f["line"] for f in function_list): |
| 61 |
print(f"Unexpected function '{fn_name}' returned") |
| 62 |
exit_code = 1 |
| 63 |
if exit_code != 0: |
| 64 |
print(function_list) |
| 65 |
exit(exit_code) |
| 66 |
|
| 67 |
|
| 68 |
if __name__ == "__main__": |
| 69 |
smoketest("test/profile_annotation_test.py") |