Skip to content

Commit bf05103

Browse files
committed
Update to use latest protowhat functionality
1 parent 86f7bd9 commit bf05103

5 files changed

Lines changed: 83 additions & 11 deletions

File tree

shellwhat/State.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44

55

66
class State(BaseState):
7-
def __init__(self, *args, **kwargs):
8-
super().__init__(*args, **kwargs)
9-
107
def get_dispatcher(self):
118
return Dispatcher.from_module(DEFAULT_PARSER)

shellwhat/test_exercise.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ def test_exercise(
3030
solution_conn=solution_conn,
3131
student_result=student_result,
3232
solution_result=solution_result,
33-
reporter=Reporter(error),
33+
reporter=Reporter(errors=error),
3434
force_diagnose=force_diagnose,
3535
)
3636

37+
State.root_state = state
3738
SCT_CTX["Ex"].root_state = state
3839

3940
try:

tests/test_basic.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from tempfile import NamedTemporaryFile
2+
13
from shellwhat.test_exercise import test_exercise as te
24
from functools import partial
35

@@ -37,11 +39,19 @@ def test_sct(te_sct, code, is_correct):
3739
assert sct_payload.get("student_code") == "ls -t"
3840

3941

40-
def test_sct_check_file():
42+
@pytest.fixture(scope="function")
43+
def tf():
44+
with NamedTemporaryFile() as tmp:
45+
tmp.file.write(b"hey")
46+
tmp.file.flush()
47+
yield tmp
48+
49+
50+
def test_sct_check_file(tf):
4151
sct_payload = te(
42-
"Ex().check_file('file1.sh', use_fs=False).has_code('hey')",
43-
student_code={"file1.sh": "echo hey"},
44-
solution_code={"file1.sh": "echo ya"},
52+
"Ex().check_file('{}').has_code('hey')".format(tf.name),
53+
student_code="NA",
54+
solution_code="NA",
4555
pre_exercise_code="",
4656
student_conn=None,
4757
solution_conn=None,

tests/test_has_funcs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
@pytest.fixture
1212
def state():
13-
return State(
13+
state = State(
1414
student_code="some code\x1b[39;49m",
1515
solution_code="some code",
1616
pre_exercise_code="",
@@ -20,6 +20,8 @@ def state():
2020
solution_result=None,
2121
reporter=Reporter(),
2222
)
23+
state.root_state = state
24+
return state
2325

2426

2527
def test_strip_ansi(state):

tests/test_selectors.py

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import pytest
2+
13
from shellwhat.State import State
24
from protowhat.Reporter import Reporter
35
from protowhat.Test import TestFail as TF
46
from protowhat.checks.check_funcs import check_node, check_edge, has_equal_ast
7+
from shellwhat.parsers import OshParser
58

6-
import pytest
9+
10+
@pytest.fixture(autouse=True)
11+
def run_around_tests():
12+
yield
13+
OshParser.nodes = {}
714

815

916
@pytest.fixture("function")
1017
def state():
11-
return State(
18+
state = State(
1219
student_code="echo a $b ${c}",
1320
solution_code="echo a $b ${c} unique",
1421
pre_exercise_code="",
@@ -18,16 +25,65 @@ def state():
1825
solution_result="",
1926
reporter=Reporter(),
2027
)
28+
state.root_state = state
29+
return state
2130

2231

2332
@pytest.fixture("function")
2433
def d():
2534
return state().get_dispatcher()
2635

2736

37+
@pytest.fixture("function")
38+
def shell_script():
39+
return """# Use curl, download file from URL and rename
40+
curl -o Spotify201812.zip -L https://tinyurl.com/Zipped201812Spotify
41+
42+
# Unzip file then delete original zipped file
43+
unzip Spotify201812.zip && rm Spotify201812.zip
44+
45+
# View url_list.txt to verify content
46+
cat url_list.txt
47+
48+
# Use wget, download all files in url_list.txt
49+
wget -i url_list.txt
50+
51+
# Take a look at all files downloaded
52+
ls"""
53+
54+
2855
@pytest.mark.osh
2956
class TestOsh:
3057
# TODO: check top node?
58+
def test_osh_parsing(self, d, shell_script):
59+
d.ast_mod.parse(shell_script)
60+
61+
def test_sct(self, shell_script):
62+
from shellwhat.test_exercise import test_exercise as te
63+
64+
sct_payload = te(
65+
"""
66+
Ex().check_node('SimpleCommand', 0).multi(
67+
check_edge('words', 0).has_equal_ast(),
68+
check_edge('words', 1).has_equal_ast(),
69+
check_edge('words', 2).has_equal_ast(),
70+
check_edge('words', 3).has_equal_ast(),
71+
check_edge('words', 4).has_equal_ast()
72+
)""",
73+
student_code=shell_script,
74+
solution_code=shell_script,
75+
pre_exercise_code="""from urllib.request import urlretrieve
76+
url = 'https://assets.datacamp.com/production/repositories/4180/datasets/b4e48732f25e87864f6ce23066b8c0d14c7c6430/Chp1Capstone_urlList.txt'
77+
urlretrieve(url, 'url_list.txt')""",
78+
student_conn=None,
79+
solution_conn=None,
80+
student_result="",
81+
solution_result="",
82+
ex_type="NormalExercise",
83+
error=[],
84+
)
85+
assert sct_payload.get("correct")
86+
3187
def test_osh_dispatcher_ast_fails_hard(self, d):
3288
with pytest.raises(d.ParseError):
3389
d.ast_mod.parse("for ii")
@@ -54,6 +110,12 @@ def test_osh_selector_high_priority(self, state):
54110
child = check_node(state, "BracedVarSub", priority=99)
55111
assert isinstance(child.student_ast, target)
56112
assert child.student_ast.token.val == "c"
113+
with pytest.raises(TF):
114+
from protowhat.Feedback import Feedback
115+
116+
fb = Feedback("test")
117+
child.report(fb)
118+
assert fb.highlight is not None
57119

58120
def test_osh_selector_fail(self, state):
59121
with pytest.raises(TF):

0 commit comments

Comments
 (0)