|
| 1 | +import subprocess |
| 2 | +import sys |
| 3 | +import traceback |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from applications.ColossalChat.coati.distributed.reward.code_reward.testing_util import _load_runtime_module |
| 9 | + |
| 10 | +REPO_ROOT = Path(__file__).resolve().parents[3] |
| 11 | +MODULE_NAME = "_colossalai_test_candidate" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(autouse=True) |
| 15 | +def cleanup_runtime_module(): |
| 16 | + yield |
| 17 | + sys.modules.pop(MODULE_NAME, None) |
| 18 | + |
| 19 | + |
| 20 | +def _run_isolated(script: str): |
| 21 | + completed = subprocess.run( |
| 22 | + [sys.executable, "-c", script], |
| 23 | + cwd=REPO_ROOT, |
| 24 | + capture_output=True, |
| 25 | + text=True, |
| 26 | + timeout=30, |
| 27 | + ) |
| 28 | + assert completed.returncode == 0, completed.stdout + completed.stderr |
| 29 | + |
| 30 | + |
| 31 | +def test_load_runtime_module_supports_functions_and_solution_classes(): |
| 32 | + module = _load_runtime_module( |
| 33 | + MODULE_NAME, |
| 34 | + "def add(a, b):\n return a + b\n\nclass Solution:\n def double(self, value):\n return value * 2\n", |
| 35 | + ) |
| 36 | + |
| 37 | + assert module.__name__ == MODULE_NAME |
| 38 | + assert module.add(2, 3) == 5 |
| 39 | + assert module.Solution().double(4) == 8 |
| 40 | + |
| 41 | + |
| 42 | +def test_load_runtime_module_does_not_share_namespaces(): |
| 43 | + first = _load_runtime_module(MODULE_NAME, "value = 1") |
| 44 | + second = _load_runtime_module(MODULE_NAME, "value = 2") |
| 45 | + |
| 46 | + assert first is not second |
| 47 | + assert first.value == 1 |
| 48 | + assert second.value == 2 |
| 49 | + assert sys.modules[MODULE_NAME] is second |
| 50 | + |
| 51 | + |
| 52 | +def test_load_runtime_module_supports_dataclasses(): |
| 53 | + module = _load_runtime_module( |
| 54 | + MODULE_NAME, |
| 55 | + "from dataclasses import dataclass\n\n@dataclass\nclass Point:\n x: int\n y: int\n", |
| 56 | + ) |
| 57 | + |
| 58 | + assert module.Point(2, 3) == module.Point(x=2, y=3) |
| 59 | + |
| 60 | + |
| 61 | +def test_load_runtime_module_uses_string_filename_in_tracebacks(): |
| 62 | + previous = _load_runtime_module(MODULE_NAME, "value = 1") |
| 63 | + |
| 64 | + with pytest.raises(RuntimeError) as exc_info: |
| 65 | + _load_runtime_module(MODULE_NAME, "raise RuntimeError('boom')") |
| 66 | + |
| 67 | + frames = traceback.extract_tb(exc_info.value.__traceback__) |
| 68 | + assert frames[-1].filename == "<string>" |
| 69 | + assert sys.modules[MODULE_NAME] is previous |
| 70 | + |
| 71 | + |
| 72 | +def test_load_runtime_module_reports_syntax_errors(): |
| 73 | + with pytest.raises(SyntaxError) as exc_info: |
| 74 | + _load_runtime_module(MODULE_NAME, "def broken(:\n pass") |
| 75 | + |
| 76 | + assert exc_info.value.filename == "<string>" |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.skipif(sys.platform == "win32", reason="The code verifier uses Unix process controls") |
| 80 | +def test_run_test_executes_call_based_code_without_pyext(): |
| 81 | + _run_isolated( |
| 82 | + """ |
| 83 | +from applications.ColossalChat.coati.distributed.reward.code_reward.testing_util import run_test |
| 84 | +
|
| 85 | +result, metadata = run_test( |
| 86 | + {"fn_name": "add", "inputs": ["2\\n3"], "outputs": ["5"]}, |
| 87 | + test="def add(a, b):\\n return a + b", |
| 88 | +) |
| 89 | +assert result == [True], (result, metadata) |
| 90 | +""" |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.skipif(sys.platform == "win32", reason="The code verifier uses Unix process controls") |
| 95 | +def test_run_test_executes_standard_input_code_without_pyext(): |
| 96 | + _run_isolated( |
| 97 | + """ |
| 98 | +from applications.ColossalChat.coati.distributed.reward.code_reward.testing_util import run_test |
| 99 | +
|
| 100 | +result, metadata = run_test( |
| 101 | + {"inputs": ["4"], "outputs": ["8"]}, |
| 102 | + test="value = int(input())\\nprint(value * 2)", |
| 103 | +) |
| 104 | +assert result == [True], (result, metadata) |
| 105 | +""" |
| 106 | + ) |
0 commit comments