Skip to content

Commit a60b7fa

Browse files
authored
Merge pull request #387 from datacamp/fix/run-file
[CX-354] Fix running file
2 parents e7a75af + 2fc2420 commit a60b7fa

File tree

7 files changed

+48
-24
lines changed

7 files changed

+48
-24
lines changed

.readthedocs.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Read the Docs configuration file
2+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
3+
4+
# Required
5+
version: 2
6+
7+
# Build documentation in the docs/ directory with Sphinx
8+
sphinx:
9+
configuration: docs/conf.py
10+
11+
# Optionally build your docs in additional formats such as PDF and ePub
12+
formats: all
13+
14+
# Optionally set the version of Python and requirements required to build your docs
15+
python:
16+
version: 3.5
17+
install:
18+
- requirements: requirements.txt
19+
- method: pip
20+
path: .

docs/reference.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ Checking files
9090
.. autofunction:: pythonwhat.checks.check_wrappers.has_dir
9191
.. autofunction:: pythonwhat.local.run
9292

93+
Bash history checks
94+
-------------------
95+
96+
.. automodule:: protowhat.checks.check_bash_history
97+
:members:
98+
9399
Electives
94100
---------
95101

pythonwhat/State.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,14 @@ def has_different_processes(self):
181181
# play it safe (most common)
182182
return True
183183

184-
# todo: remove (deprecated)
185-
def assert_root(self, fun, extra_msg=""):
186-
if self.parent_state is not None:
187-
raise InstructorError(
188-
"`%s()` should only be called from the root state, `Ex()`. %s"
189-
% (fun, extra_msg)
190-
)
191-
192184
def assert_execution_root(self, fun, extra_msg=""):
193-
if self.parent_state is not None and not self.assert_creator_type("run"):
185+
if not (self.is_root or self.is_creator_type("run")):
194186
raise InstructorError(
195187
"`%s()` should only be called focusing on a full script, following `Ex()` or `run()`. %s"
196188
% (fun, extra_msg)
197189
)
198190

199-
def assert_creator_type(self, type):
191+
def is_creator_type(self, type):
200192
return self.creator and self.creator.get("type") == type
201193

202194
def assert_is(self, klasses, fun, prev_fun):

pythonwhat/checks/check_wrappers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from protowhat.utils import _debug
22
from protowhat.checks.check_simple import allow_errors
3+
from protowhat.checks.check_bash_history import has_command
34
from protowhat.checks.check_files import check_file, has_dir
45
from pythonwhat.checks.check_funcs import check_part, check_part_index, check_node
56
from pythonwhat.checks.has_funcs import has_equal_part
@@ -775,6 +776,8 @@ def rename_function(func, name):
775776

776777
scts["run"] = run
777778

779+
scts["has_command"] = has_command
780+
778781
scts["check_file"] = check_file
779782
scts["has_dir"] = has_dir
780783

pythonwhat/local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def run_exercise(pec, sol_code, stu_code, sol_wd=None, stu_wd=None, **kwargs):
205205
# e.g. `python -m project.run
206206
# allow setting env vars? e.g. PYTHONPATH, could help running more complex setup
207207
# allow prepending code? set_env? e.g. (automatically) setting __file__?
208-
def run(state, relative_working_dir=None, solution_dir="solution"):
208+
def run(state, relative_working_dir=None, solution_dir="../solution"):
209209
"""Run the focused student and solution code in the specified location
210210
211211
This function can be used after ``check_file`` to execute student and solution code.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pythonwhat deps
2-
protowhat~=1.10.0
2+
protowhat~=1.11.0
33
asttokens~=1.1.10
44
dill~=0.2.7.1
55
markdown2~=2.3.7

tests/test_local.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,29 @@ def test_run_solution_dir():
126126

127127
file_dir = "a/b"
128128
file_path = "a/b/c"
129-
solution_location = "solution"
129+
workspace_location = "workspace"
130130

131131
with in_temp_dir():
132-
write_file(file_dir, "c", code)
132+
write_file("solution/" + file_dir, "c", code)
133133

134-
os.makedirs(solution_location)
135-
with ChDir(solution_location):
134+
os.makedirs(workspace_location)
135+
with ChDir(workspace_location):
136136
write_file(file_dir, "c", code)
137137

138-
chain = setup_state("", "", pec="")
138+
chain = setup_state("", "", pec="")
139139

140-
child = chain.check_file(file_path, solution_code=code)
140+
child = chain.check_file(file_path, solution_code=code)
141141

142-
child.run(file_dir).check_object("c")
143-
child.run().check_object("c")
142+
child.run(file_dir).check_object("c")
143+
child.run().check_object("c")
144144

145145

146146
def test_run_with_absolute_dir():
147147
code = 'from pathlib import Path; c = Path("c").read_text(encoding="utf-8")'
148148

149149
file_dir = "a/b"
150150
solution_location = "solution"
151+
workspace_location = "workspace"
151152

152153
with in_temp_dir():
153154
os.makedirs(file_dir)
@@ -159,12 +160,14 @@ def test_run_with_absolute_dir():
159160

160161
write_file(solution_location, "c", code)
161162

162-
chain = setup_state("", "", pec="")
163+
os.makedirs(workspace_location)
164+
with ChDir(workspace_location):
165+
chain = setup_state("", "", pec="")
163166

164-
child = chain.check_file(abs_file_path, solution_code=code)
167+
child = chain.check_file(abs_file_path, solution_code=code)
165168

166-
child.run(abs_dir).check_object("c")
167-
child.run().check_object("c")
169+
child.run(abs_dir).check_object("c")
170+
child.run().check_object("c")
168171

169172

170173
def test_run_custom_solution_dir():

0 commit comments

Comments
 (0)