forked from datacamp/pythonwhat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
170 lines (128 loc) · 4.19 KB
/
helper.py
File metadata and controls
170 lines (128 loc) · 4.19 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import re
import os
import inspect
from collections import defaultdict
from functools import wraps
from pythonwhat.local import StubProcess, run_exercise
from contextlib import contextmanager
from protowhat.Test import TestFail as TF
from pythonwhat.test_exercise import test_exercise
from pythonwhat.sct_syntax import Chain
import pytest
import tempfile
test_data = defaultdict(list)
def capture_test_data(f):
@wraps(f)
def wrapper(*args, **kwargs):
result = f(*args, **kwargs)
data = kwargs.copy()
del data["student_process"]
del data["solution_process"]
data["result"] = result
context = "other"
stack = inspect.stack()
for frame in stack:
_, filename = os.path.split(frame.filename)
if filename and filename.startswith("test_"):
context = filename
break
test_data[context].append(data.copy())
return result
return wrapper
test_exercise = capture_test_data(test_exercise)
class ChDir(object):
"""
Step into a directory temporarily.
"""
def __init__(self, path):
self.old_dir = os.getcwd()
self.new_dir = path
def __enter__(self):
os.chdir(self.new_dir)
def __exit__(self, *args):
os.chdir(self.old_dir)
def run(data, run_code=True):
pec = data.get("DC_PEC", "")
stu_code = data.get("DC_CODE", "")
sol_code = data.get("DC_SOLUTION", "")
sct = data.get("DC_SCT", "")
force_diagnose = data.get("DC_FORCE_DIAGNOSE", False)
with tempfile.TemporaryDirectory() as d:
with ChDir(d):
if run_code:
sol_process, stu_process, raw_stu_output, error = run_exercise(
pec, sol_code, stu_code
)
else:
raw_stu_output = ""
stu_process = StubProcess()
sol_process = StubProcess()
error = None
res = test_exercise(
sct=sct,
student_code=stu_code,
solution_code=sol_code,
pre_exercise_code=pec,
student_process=stu_process,
solution_process=sol_process,
raw_student_output=raw_stu_output,
ex_type="NormalExercise",
force_diagnose=force_diagnose,
error=error,
)
return res
def get_sct_payload(output):
sct_output = [out for out in output if out["type"] == "sct"]
if len(sct_output) > 0:
return sct_output[0]["payload"]
else:
print(output)
return None
def passes(st):
assert isinstance(st, Chain)
@contextmanager
def verify_sct(correct):
if correct:
yield
else:
with pytest.raises(TF):
yield
def test_lines(test, sct_payload, ls, le, cs, ce):
test.assertEqual(sct_payload["line_start"], ls)
test.assertEqual(sct_payload["line_end"], le)
test.assertEqual(sct_payload["column_start"], cs)
test.assertEqual(sct_payload["column_end"], ce)
def test_absent_lines(test, sct_payload):
test.assertFalse("line_start" in sct_payload)
test.assertFalse("line_end" in sct_payload)
test.assertFalse("column_start" in sct_payload)
test.assertFalse("column_end" in sct_payload)
def with_line_info(output, ls, le, cs, ce):
assert output["line_start"] == ls
assert output["line_end"] == le
assert output["column_start"] == cs
assert output["column_end"] == ce
def no_line_info(output):
assert "line_start" not in output
assert "line_end" not in output
assert "column_start" not in output
assert "column_end" not in output
def remove_lambdas(sct_str, count=0, with_args=False):
if with_args:
return re.sub("lambda.*?:", "", sct_str, count=count)
else:
return re.sub("lambda:", "", sct_str, count=count)
def replace_test_if(sct):
return re.sub(r"test_if_else\(", "test_if_exp(", sct)
@contextmanager
def set_v2_only_env(new):
key = "PYTHONWHAT_V2_ONLY"
old = os.environ.get(key)
try:
os.environ[key] = new
yield
finally:
if old is None:
del os.environ[key]
else:
os.environ[key] = old