File: smoketest_profile_decorator.py

package info (click to toggle)
scalene 1.5.51-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,528 kB
  • sloc: cpp: 22,930; python: 13,403; javascript: 11,769; ansic: 817; makefile: 196; sh: 45
file content (53 lines) | stat: -rw-r--r-- 1,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import json
import pathlib
import subprocess
import sys
import tempfile

def smoketest(fname):
    outfile = pathlib.Path(tempfile.mkdtemp(prefix="scalene") / pathlib.Path("smoketest.json"))
    proc = subprocess.run( [sys.executable, "-m", "scalene", "--cli", "--json", "--outfile", str(outfile), fname] ,capture_output=True)
    if proc.returncode != 0:
        print("Exited with a non-zero code:", proc.returncode)
        print("Stdout:", proc.stdout.decode('utf-8'))
        print("Stderr:", proc.stderr.decode('utf-8'))

        exit(proc.returncode)

    stderr = proc.stderr.decode('utf-8')
    try:
        with open(outfile, "r") as f:
            outfile_contents = f.read()
        scalene_json = json.loads(outfile_contents)
    except json.JSONDecodeError:
        print("Invalid JSON", stderr)
        exit(1)
    if len(scalene_json) == 0:
        print("No JSON output")
        exit(1)
    files = scalene_json['files']
    if not len(files) > 0:
        print("No files found in output")
        exit(1)
    _fname = list(files.keys())[0]
    function_list = files[_fname]['functions']
    exit_code = 0

    # if 'doit1' not in function_dict:
    expected_functions = ['doit1', 'doit3']
    unexpected_functions = ['doit2']
    for fn_name in expected_functions:
        if not any(fn_name in f['line'] for f in function_list):
            print(f"Expected function '{fn_name}' not returned")
            exit_code = 1
    for fn_name in unexpected_functions:
        if any(fn_name in f['line'] for f in function_list):
            print(f"Unexpected function '{fn_name}' returned")
            exit_code = 1
    if exit_code != 0:
        print(function_list)
        exit(exit_code)

if __name__ == '__main__':
    smoketest('test/profile_annotation_test.py')