Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All notable changes to this project will be documented here.

## [unreleased]
- Fixed scripts started by tests failing to import files from their own folder (#787)

## [v2.10.5]
- Add libuv1-dev system dependency required by fs 2.x (#732)
Expand Down
7 changes: 3 additions & 4 deletions server/autotest_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
DEFAULT_ENV_DIR = "defaultvenv"
TEST_SCRIPT_DIR = os.path.join(config["workspace"], "scripts")
STUDENT_IMPORT_TESTERS = {"py", "pyta", "jupyter"} # these testers load student code
SAFE_PATH_ENV = {"PYTHONSAFEPATH": "1"} # keeps the test folder off the import path

ResultData = Dict[str, Union[str, int, type(None), Dict]]

Expand Down Expand Up @@ -133,7 +132,7 @@ def _create_test_script_command(tester_type: str) -> str:
Return string representing a command line command to
run tests.

A safe import path stops uploaded files from hiding the tester's modules. Some testers load student
A safe import path (the -P flag) stops uploaded files from hiding the tester's modules. Some testers load student
code. They get the test folder back, last in line.
"""
import_line = f"from testers.{tester_type}.{tester_type}_tester import {tester_type.capitalize()}Tester as Tester"
Expand All @@ -149,7 +148,7 @@ def _create_test_script_command(tester_type: str) -> str:
f"Tester(resource_settings={get_resource_settings(config)}, specs=TestSpecs.from_json(sys.stdin.read())).run()",
]
python_str = "; ".join(python_lines)
return f"\"${{PYTHON}}\" -c '{python_str}'"
return f"\"${{PYTHON}}\" -P -c '{python_str}'"


def get_available_port(min_, max_, host: str = "localhost") -> str:
Expand Down Expand Up @@ -277,7 +276,7 @@ def _run_test_specs(
timeout = min(timeout, max_timeout)
try:
env = settings.get("_env", {})
env_vars = {**os.environ, **SAFE_PATH_ENV, **_get_env_vars(test_username), **env, **secret_env}
env_vars = {**os.environ, **_get_env_vars(test_username), **env, **secret_env}
env_vars = _update_env_vars(env_vars, test_env_vars)
proc = subprocess.Popen(
args,
Expand Down
2 changes: 1 addition & 1 deletion server/autotest_server/testers/ai/ai_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def call_ai_feedback(self) -> dict:
test_label = test_group.get("test_label")
timeout = test_group.get("timeout", 30)
output_mode = test_group.get("output")
cmd = [sys.executable, "-m", "ai_feedback"]
cmd = [sys.executable, "-P", "-m", "ai_feedback"]

# Restrict to the gateway-fronted models — prevent untracked cloud access.
model = config.get("model", "")
Expand Down
17 changes: 12 additions & 5 deletions server/autotest_server/tests/test_autotest_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,16 @@ def test_ai_tester_env_stays_out_of_other_testers(monkeypatch, tmp_path):
assert envs["ai"]["GATEWAY_API_KEY"] == "gw-key"


def test_every_tester_gets_safe_path(monkeypatch, tmp_path):
testers = ["ai", "py", "pyta", "jupyter", "java"]
envs, _ = _run_specs_capturing_envs(monkeypatch, tmp_path, testers)
assert {t: envs[t]["PYTHONSAFEPATH"] for t in testers} == dict.fromkeys(testers, "1")
@pytest.mark.parametrize("tester_type", ["ai", "py", "pyta", "jupyter", "java"])
def test_script_run_by_a_test_imports_its_neighbour(monkeypatch, tmp_path, tester_type):
envs, _ = _run_specs_capturing_envs(monkeypatch, tmp_path, [tester_type])
monkeypatch.undo() # restore the real subprocess.Popen
(tmp_path / "helper.py").write_text("")
(tmp_path / "main.py").write_text("import helper\n")
proc = subprocess.run(
[sys.executable, "main.py"], cwd=tmp_path, env=envs[tester_type], capture_output=True, text=True
)
assert proc.returncode == 0, proc.stderr


def test_markus_env_vars_cannot_override_ai_tester_env(monkeypatch, tmp_path):
Expand All @@ -340,7 +346,8 @@ def _run_tester_command(monkeypatch, tmp_path, tester_type, module):
# Swap the tester import for a plain import. Then the test needs no tester packages.
command = command.replace(f"from testers.{tester_type}.{tester_type}_tester import", f"import {module}; import")
command = command.split("; from testers.specs")[0] + "'"
env = {**os.environ, "PYTHON": sys.executable, "PYTHONSAFEPATH": "1"}
env = {**os.environ, "PYTHON": sys.executable}
env.pop("PYTHONSAFEPATH", None) # the -P flag alone must guard the tester
return subprocess.run(command, shell=True, cwd=tmp_path, env=env, capture_output=True, text=True)


Expand Down
6 changes: 6 additions & 0 deletions server/autotest_server/tests/testers/ai/test_ai_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ def test_annotations_output(monkeypatch):
assert tester.annotations[0]["text"] == "Good variable name"


def test_ai_feedback_runs_with_safe_path(monkeypatch):
captured = _capture_subprocess(monkeypatch)
_make_tester().call_ai_feedback()
assert captured["cmd"][:4] == [sys.executable, "-P", "-m", "ai_feedback"]


def test_message_output(monkeypatch):
tester = _make_tester(output="message")
_mock_subprocess(monkeypatch, stdout="Score: 8/10")
Expand Down
Loading