|
| 1 | +import os |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | + |
| 7 | +def test_local_test_runs_host_pytest_with_entry(tmp_path, monkeypatch): |
| 8 | + project = tmp_path / "proj" |
| 9 | + project.mkdir() |
| 10 | + monkeypatch.chdir(project) |
| 11 | + |
| 12 | + # Create a dummy test file |
| 13 | + test_file = project / "metric" / "test_one.py" |
| 14 | + test_file.parent.mkdir(parents=True, exist_ok=True) |
| 15 | + test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8") |
| 16 | + |
| 17 | + # Import module under test |
| 18 | + from eval_protocol.cli_commands import local_test as lt |
| 19 | + |
| 20 | + # Avoid Docker path |
| 21 | + monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: []) |
| 22 | + |
| 23 | + captured = {"target": ""} |
| 24 | + |
| 25 | + def _fake_host(target: str) -> int: |
| 26 | + captured["target"] = target |
| 27 | + return 0 |
| 28 | + |
| 29 | + monkeypatch.setattr(lt, "_run_pytest_host", _fake_host) |
| 30 | + |
| 31 | + args = SimpleNamespace(entry=str(test_file), ignore_docker=False, yes=True) |
| 32 | + rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType] |
| 33 | + assert rc == 0 |
| 34 | + # Expect relative path target |
| 35 | + assert captured["target"] == os.path.relpath(str(test_file), str(project)) |
| 36 | + |
| 37 | + |
| 38 | +def test_local_test_ignores_docker_when_flag_set(tmp_path, monkeypatch): |
| 39 | + project = tmp_path / "proj" |
| 40 | + project.mkdir() |
| 41 | + monkeypatch.chdir(project) |
| 42 | + |
| 43 | + test_file = project / "metric" / "test_two.py" |
| 44 | + test_file.parent.mkdir(parents=True, exist_ok=True) |
| 45 | + test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8") |
| 46 | + |
| 47 | + from eval_protocol.cli_commands import local_test as lt |
| 48 | + |
| 49 | + # Pretend we have Dockerfile(s), but ignore_docker=True should skip |
| 50 | + monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [str(project / "Dockerfile")]) |
| 51 | + |
| 52 | + called = {"host": False} |
| 53 | + |
| 54 | + def _fake_host(target: str) -> int: |
| 55 | + called["host"] = True |
| 56 | + return 0 |
| 57 | + |
| 58 | + monkeypatch.setattr(lt, "_run_pytest_host", _fake_host) |
| 59 | + |
| 60 | + args = SimpleNamespace(entry=str(test_file), ignore_docker=True, yes=True) |
| 61 | + rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType] |
| 62 | + assert rc == 0 |
| 63 | + assert called["host"] is True |
| 64 | + |
| 65 | + |
| 66 | +def test_local_test_errors_on_multiple_dockerfiles(tmp_path, monkeypatch): |
| 67 | + project = tmp_path / "proj" |
| 68 | + project.mkdir() |
| 69 | + monkeypatch.chdir(project) |
| 70 | + |
| 71 | + test_file = project / "metric" / "test_three.py" |
| 72 | + test_file.parent.mkdir(parents=True, exist_ok=True) |
| 73 | + test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8") |
| 74 | + |
| 75 | + from eval_protocol.cli_commands import local_test as lt |
| 76 | + |
| 77 | + monkeypatch.setattr( |
| 78 | + lt, "_find_dockerfiles", lambda root: [str(project / "Dockerfile"), str(project / "another" / "Dockerfile")] |
| 79 | + ) |
| 80 | + |
| 81 | + args = SimpleNamespace(entry=str(test_file), ignore_docker=False, yes=True) |
| 82 | + rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType] |
| 83 | + assert rc == 1 |
| 84 | + |
| 85 | + |
| 86 | +def test_local_test_builds_and_runs_in_docker(tmp_path, monkeypatch): |
| 87 | + project = tmp_path / "proj" |
| 88 | + project.mkdir() |
| 89 | + monkeypatch.chdir(project) |
| 90 | + |
| 91 | + test_file = project / "metric" / "test_four.py" |
| 92 | + test_file.parent.mkdir(parents=True, exist_ok=True) |
| 93 | + test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8") |
| 94 | + |
| 95 | + from eval_protocol.cli_commands import local_test as lt |
| 96 | + |
| 97 | + monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: [str(project / "Dockerfile")]) |
| 98 | + monkeypatch.setattr(lt, "_build_docker_image", lambda dockerfile, tag: True) |
| 99 | + |
| 100 | + captured = {"target": "", "image": ""} |
| 101 | + |
| 102 | + def _fake_run_docker(root: str, image_tag: str, pytest_target: str) -> int: |
| 103 | + captured["target"] = pytest_target |
| 104 | + captured["image"] = image_tag |
| 105 | + return 0 |
| 106 | + |
| 107 | + monkeypatch.setattr(lt, "_run_pytest_in_docker", _fake_run_docker) |
| 108 | + |
| 109 | + args = SimpleNamespace(entry=str(test_file), ignore_docker=False, yes=True) |
| 110 | + rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType] |
| 111 | + assert rc == 0 |
| 112 | + assert captured["image"] == "ep-evaluator:local" |
| 113 | + assert captured["target"] == os.path.relpath(str(test_file), str(project)) |
| 114 | + |
| 115 | + |
| 116 | +def test_local_test_selector_single_test(tmp_path, monkeypatch): |
| 117 | + project = tmp_path / "proj" |
| 118 | + project.mkdir() |
| 119 | + monkeypatch.chdir(project) |
| 120 | + |
| 121 | + test_file = project / "metric" / "test_sel.py" |
| 122 | + test_file.parent.mkdir(parents=True, exist_ok=True) |
| 123 | + test_file.write_text("def test_dummy():\n assert True\n", encoding="utf-8") |
| 124 | + |
| 125 | + from eval_protocol.cli_commands import local_test as lt |
| 126 | + from eval_protocol.cli_commands import upload as up |
| 127 | + |
| 128 | + # No entry; force discover + selector |
| 129 | + disc = SimpleNamespace(qualname="metric.test_sel", file_path=str(test_file)) |
| 130 | + monkeypatch.setattr(lt, "_discover_tests", lambda root: [disc]) |
| 131 | + monkeypatch.setattr(up, "_prompt_select", lambda tests, non_interactive=False: tests[:1]) |
| 132 | + monkeypatch.setattr(lt, "_find_dockerfiles", lambda root: []) |
| 133 | + |
| 134 | + called = {"host": False} |
| 135 | + |
| 136 | + def _fake_host(target: str) -> int: |
| 137 | + called["host"] = True |
| 138 | + return 0 |
| 139 | + |
| 140 | + monkeypatch.setattr(lt, "_run_pytest_host", _fake_host) |
| 141 | + |
| 142 | + args = SimpleNamespace(entry=None, ignore_docker=False, yes=True) |
| 143 | + rc = lt.local_test_command(args) # pyright: ignore[reportArgumentType] |
| 144 | + assert rc == 0 |
| 145 | + assert called["host"] is True |
0 commit comments