1+ import os
2+ from pathlib import Path
3+
14import pytest
25
6+ from pythonwhat .local import ChDir
37from pythonwhat .test_exercise import setup_state
48from tests .helper import verify_sct , in_temp_dir
59
@@ -50,7 +54,7 @@ def test_running_code_isolation_run(sol_code, stu_code):
5054 asset
5155 ),
5256 )
53- * 2 ,
57+ * 2
5458 ],
5559)
5660def test_urlretrieve_in_process (sol_code , stu_code ):
@@ -77,7 +81,7 @@ def test_urlretrieve_in_process(sol_code, stu_code):
7781 asset
7882 ),
7983 )
80- * 2 ,
84+ * 2
8185 ],
8286)
8387def test_urlopen_in_process (sol_code , stu_code ):
@@ -89,3 +93,97 @@ def test_urlopen_in_process(sol_code, stu_code):
8993
9094 with verify_sct (True ):
9195 chain .run ()
96+
97+
98+ def write_file (path , name , content ):
99+ os .makedirs (path )
100+ with ChDir (path ):
101+ with open (name , "w" ) as f :
102+ f .write (content )
103+
104+
105+ def test_run_relative_working_dir ():
106+ stu_code = 'from pathlib import Path; c = Path("c").read_text(encoding="utf-8")'
107+ sol_code = """c = 'c= Path("c").read_text(encoding="utf-8")'"""
108+
109+ file_dir = "a/b"
110+ file_path = "a/b/c"
111+
112+ with in_temp_dir ():
113+
114+ write_file (file_dir , "c" , stu_code )
115+
116+ chain = setup_state ("" , "" , pec = "" )
117+
118+ child = chain .check_file (file_path , solution_code = sol_code )
119+
120+ child .run (file_dir ).check_object ("c" )
121+ child .run ().check_object ("c" )
122+
123+
124+ def test_run_solution_dir ():
125+ code = 'from pathlib import Path; c = Path("c").read_text(encoding="utf-8")'
126+
127+ file_dir = "a/b"
128+ file_path = "a/b/c"
129+ solution_location = "solution"
130+
131+ with in_temp_dir ():
132+ write_file (file_dir , "c" , code )
133+
134+ os .makedirs (solution_location )
135+ with ChDir (solution_location ):
136+ write_file (file_dir , "c" , code )
137+
138+ chain = setup_state ("" , "" , pec = "" )
139+
140+ child = chain .check_file (file_path , solution_code = code )
141+
142+ child .run (file_dir ).check_object ("c" )
143+ child .run ().check_object ("c" )
144+
145+
146+ def test_run_with_absolute_dir ():
147+ code = 'from pathlib import Path; c = Path("c").read_text(encoding="utf-8")'
148+
149+ file_dir = "a/b"
150+ solution_location = "solution"
151+
152+ with in_temp_dir ():
153+ os .makedirs (file_dir )
154+ with ChDir (file_dir ):
155+ abs_dir = os .path .abspath ("." )
156+ abs_file_path = Path (abs_dir , "c" )
157+ with open ("c" , "w" ) as f :
158+ f .write (code )
159+
160+ write_file (solution_location , "c" , code )
161+
162+ chain = setup_state ("" , "" , pec = "" )
163+
164+ child = chain .check_file (abs_file_path , solution_code = code )
165+
166+ child .run (abs_dir ).check_object ("c" )
167+ child .run ().check_object ("c" )
168+
169+
170+ def test_run_custom_solution_dir ():
171+ code = 'from pathlib import Path; c = Path("c").read_text(encoding="utf-8")'
172+
173+ file_dir = "a/b"
174+ file_path = "a/b/c"
175+ custom_solution_location = "custom/solution/folder"
176+
177+ with in_temp_dir ():
178+ write_file (file_dir , "c" , code )
179+
180+ os .makedirs (custom_solution_location )
181+ with ChDir (custom_solution_location ):
182+ write_file (file_dir , "c" , code )
183+
184+ chain = setup_state ("" , "" , pec = "" )
185+
186+ child = chain .check_file (file_path , solution_code = code )
187+
188+ child .run (file_dir , solution_dir = custom_solution_location ).check_object ("c" )
189+ child .run (solution_dir = custom_solution_location ).check_object ("c" )
0 commit comments