-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_star_destroyer.py
50 lines (42 loc) · 1.88 KB
/
test_star_destroyer.py
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
import json
import os
import pickle
import pprint
import pytest
import sys
import tempfile
CASES_PATH = 'cases'
PROGRAM_PATH = os.path.join(os.path.dirname(__file__), 'star_destroyer.py')
CASE_DIRS = []
for name in os.listdir(CASES_PATH):
path = os.path.join(CASES_PATH, name)
if (os.path.isdir(path) and
os.path.isfile(os.path.join(path, 'expected_imports')) and
os.path.isfile(os.path.join(path, 'expected_usage'))):
CASE_DIRS.append(path)
@pytest.mark.parametrize('path', CASE_DIRS)
def test_scanner(path):
print('running: %s' % path)
with tempfile.NamedTemporaryFile('rb') as imports_file:
with tempfile.NamedTemporaryFile('rb') as usage_file:
os.spawnl(os.P_WAIT, sys.executable, sys.executable, PROGRAM_PATH,
'-t', path, imports_file.name, usage_file.name)
actual_imports = pickle.load(imports_file)
actual_usage = pickle.load(usage_file)
with open(os.path.join(path, 'expected_imports')) as imports_file:
expected_imports = json.load(imports_file)
with open(os.path.join(path, 'expected_usage')) as usage_file:
expected_usage = json.load(usage_file)
for modpath in expected_imports:
module_imports = actual_imports.setdefault(modpath, {})
for name, value in module_imports.items():
module_imports[name] = sorted(value)
for modpath in expected_usage:
actual_usage[modpath] = sorted(actual_usage.get(modpath, []))
print('expected imports: %s' % json.dumps(expected_imports, sort_keys=1))
print(' actual imports: %s' % json.dumps(actual_imports, sort_keys=1))
print('expected usage: %s' % json.dumps(expected_usage, sort_keys=1))
print(' actual usage: %s' % json.dumps(actual_usage, sort_keys=1))
assert expected_imports == actual_imports
assert expected_usage == actual_usage
print('passed: %s' % path)