def test_running_file_with_root_check(temp_py_file): content = cf.get_file_content(temp_py_file.name) chain = setup_state("", "", pec="") with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file(temp_py_file.name, solution_code=content).run().check_object( "a").has_equal_value() with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file(temp_py_file.name, solution_code=content).run().has_no_error() with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file(temp_py_file.name, solution_code=content).run().has_printout(0) with pytest.raises(TF): with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file( temp_py_file.name, solution_code="print('Bye!')").run().has_printout(0) with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file(temp_py_file.name, solution_code=content).run().has_output("Hi")
def test_running_file(temp_py_file): content = cf.get_file_content(temp_py_file.name) chain = setup_state("", "", pec="") with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file( temp_py_file.name, solution_code=content).run().has_equal_value(expr_code="a") with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file(temp_py_file.name, solution_code=content).run().check_object( "a").has_equal_value() with pytest.raises(TF): with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file(temp_py_file.name, solution_code=content.replace( "1", "2")).run().has_equal_value(expr_code="a") with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file(temp_py_file.name).run().has_equal_value( expr_code="a", override=1) with pytest.raises(TF): with tempfile.TemporaryDirectory() as d: with ChDir(d): chain.check_file(temp_py_file.name).run().has_equal_value( expr_code="a", override=2)
def test_run_with_absolute_dir(): code = 'from pathlib import Path; c = Path("c").read_text(encoding="utf-8")' file_dir = "a/b" solution_location = "solution" workspace_location = "workspace" with in_temp_dir(): os.makedirs(file_dir) with ChDir(file_dir): abs_dir = os.path.abspath(".") abs_file_path = Path(abs_dir, "c") with open("c", "w") as f: f.write(code) write_file(solution_location, "c", code) os.makedirs(workspace_location) with ChDir(workspace_location): chain = setup_state("", "", pec="") child = chain.check_file(abs_file_path, solution_code=code) child.run(abs_dir).check_object("c") child.run().check_object("c")
def test_run_solution_dir(): code = 'from pathlib import Path; c = Path("c").read_text(encoding="utf-8")' file_dir = "a/b" file_path = "a/b/c" workspace_location = "workspace" with in_temp_dir(): write_file("solution/" + file_dir, "c", code) os.makedirs(workspace_location) with ChDir(workspace_location): write_file(file_dir, "c", code) chain = setup_state("", "", pec="") child = chain.check_file(file_path, solution_code=code) child.run(file_dir).check_object("c") child.run().check_object("c")
def test_run_custom_solution_dir(): code = 'from pathlib import Path; c = Path("c").read_text(encoding="utf-8")' file_dir = "a/b" file_path = "a/b/c" custom_solution_location = "custom/solution/folder" with in_temp_dir(): write_file(file_dir, "c", code) os.makedirs(custom_solution_location) with ChDir(custom_solution_location): write_file(file_dir, "c", code) chain = setup_state("", "", pec="") child = chain.check_file(file_path, solution_code=code) child.run(file_dir, solution_dir=custom_solution_location).check_object("c") child.run(solution_dir=custom_solution_location).check_object("c")
def in_temp_dir(): with tempfile.TemporaryDirectory() as d: with ChDir(d): yield d
def write_file(path, name, content): os.makedirs(path) with ChDir(path): with open(name, "w") as f: f.write(content)