|
| 1 | +"""E2E tests for pyproject.toml dependency support. |
| 2 | +
|
| 3 | +These tests verify that dependencies declared in ``pyproject.toml`` are |
| 4 | +correctly extracted, used for container building, and available in the |
| 5 | +remote environment. |
| 6 | +
|
| 7 | +A temporary ``pyproject.toml`` is written to a temp directory. Only the |
| 8 | +discovery function (``_find_requirements``) is patched to return that path; |
| 9 | +the rest of the pipeline — parsing, JAX filtering, container building, and |
| 10 | +remote execution — runs for real. |
| 11 | +
|
| 12 | +Set E2E_TESTS=1 to enable. |
| 13 | +""" |
| 14 | + |
| 15 | +import pathlib |
| 16 | +import tempfile |
| 17 | +from unittest import mock |
| 18 | + |
| 19 | +from absl.testing import absltest |
| 20 | + |
| 21 | +import keras_remote |
| 22 | +from tests.e2e.e2e_utils import skip_unless_e2e |
| 23 | + |
| 24 | + |
| 25 | +def _make_test_dir(test_case): |
| 26 | + """Create a temp directory cleaned up after the test.""" |
| 27 | + td = tempfile.TemporaryDirectory() |
| 28 | + test_case.addCleanup(td.cleanup) |
| 29 | + return pathlib.Path(td.name) |
| 30 | + |
| 31 | + |
| 32 | +@skip_unless_e2e() |
| 33 | +class TestPyprojectTomlDependencies(absltest.TestCase): |
| 34 | + """Verify that [project.dependencies] from pyproject.toml are installed.""" |
| 35 | + |
| 36 | + def _create_pyproject(self, content): |
| 37 | + """Write a pyproject.toml in a temp directory and return its path.""" |
| 38 | + tmp = _make_test_dir(self) |
| 39 | + pyproject = tmp / "pyproject.toml" |
| 40 | + pyproject.write_text(content) |
| 41 | + return str(pyproject) |
| 42 | + |
| 43 | + def test_dependency_installed_on_remote(self): |
| 44 | + """A dependency from pyproject.toml is importable in the remote function.""" |
| 45 | + path = self._create_pyproject( |
| 46 | + '[project]\nname = "test"\nversion = "0.1"\n' |
| 47 | + 'dependencies = ["humanize>=4.0"]\n' |
| 48 | + ) |
| 49 | + |
| 50 | + @keras_remote.run(accelerator="cpu") |
| 51 | + def use_humanize(): |
| 52 | + import humanize |
| 53 | + |
| 54 | + return humanize.intcomma(1_000_000) |
| 55 | + |
| 56 | + with mock.patch( |
| 57 | + "keras_remote.backend.execution._find_requirements", |
| 58 | + return_value=path, |
| 59 | + ): |
| 60 | + result = use_humanize() |
| 61 | + |
| 62 | + self.assertEqual(result, "1,000,000") |
| 63 | + |
| 64 | + def test_pyproject_without_deps_succeeds(self): |
| 65 | + """A pyproject.toml with no [project.dependencies] doesn't break the pipeline.""" |
| 66 | + path = self._create_pyproject("[tool.ruff]\nline-length = 88\n") |
| 67 | + |
| 68 | + @keras_remote.run(accelerator="cpu") |
| 69 | + def simple_add(a, b): |
| 70 | + return a + b |
| 71 | + |
| 72 | + with mock.patch( |
| 73 | + "keras_remote.backend.execution._find_requirements", |
| 74 | + return_value=path, |
| 75 | + ): |
| 76 | + result = simple_add(10, 20) |
| 77 | + |
| 78 | + self.assertEqual(result, 30) |
| 79 | + |
| 80 | + def test_jax_filtered_from_pyproject_deps(self): |
| 81 | + """JAX packages in pyproject.toml are filtered like in requirements.txt.""" |
| 82 | + path = self._create_pyproject( |
| 83 | + '[project]\nname = "test"\nversion = "0.1"\n' |
| 84 | + 'dependencies = ["jax", "humanize>=4.0"]\n' |
| 85 | + ) |
| 86 | + |
| 87 | + @keras_remote.run(accelerator="cpu") |
| 88 | + def check_humanize(): |
| 89 | + import humanize |
| 90 | + |
| 91 | + return humanize.intcomma(2_500) |
| 92 | + |
| 93 | + with mock.patch( |
| 94 | + "keras_remote.backend.execution._find_requirements", |
| 95 | + return_value=path, |
| 96 | + ): |
| 97 | + result = check_humanize() |
| 98 | + |
| 99 | + # humanize was installed (not filtered), jax was filtered silently |
| 100 | + self.assertEqual(result, "2,500") |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + absltest.main() |
0 commit comments