|
| 1 | +import subprocess |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | +from pathlib import Path |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +import run_production |
| 8 | + |
| 9 | + |
| 10 | +class PrepareNativeTypescriptTests(unittest.TestCase): |
| 11 | + def test_compiler_resolves_pinned_official_dependencies(self) -> None: |
| 12 | + with tempfile.TemporaryDirectory() as directory: |
| 13 | + temp = Path(directory) |
| 14 | + sdk_root = temp / "sdk" |
| 15 | + source = sdk_root / "typescript" |
| 16 | + (source / "src").mkdir(parents=True) |
| 17 | + (source / "src" / "index.ts").write_text( |
| 18 | + "export { Sandbox } from 'e2b'\n", |
| 19 | + encoding="utf-8", |
| 20 | + ) |
| 21 | + (source / "package.json").write_text( |
| 22 | + '{"name":"@a3s-lab/box","version":"0.1.0"}\n', |
| 23 | + encoding="utf-8", |
| 24 | + ) |
| 25 | + (source / "tsconfig.json").write_text("{}\n", encoding="utf-8") |
| 26 | + |
| 27 | + environment = temp / "typescript" |
| 28 | + environment.mkdir() |
| 29 | + client = environment / "production_typescript_client.mjs" |
| 30 | + client.touch() |
| 31 | + modules = environment / "node_modules" |
| 32 | + (modules / ".bin").mkdir(parents=True) |
| 33 | + compiler = modules / ".bin" / "tsc" |
| 34 | + compiler.touch() |
| 35 | + |
| 36 | + calls = 0 |
| 37 | + |
| 38 | + def run(command: list[str], **kwargs: object) -> subprocess.CompletedProcess[str]: |
| 39 | + nonlocal calls |
| 40 | + calls += 1 |
| 41 | + if calls == 2: |
| 42 | + build_source = Path(str(kwargs["cwd"])) |
| 43 | + dependencies = build_source / "node_modules" |
| 44 | + self.assertTrue(dependencies.is_symlink()) |
| 45 | + self.assertEqual(dependencies.resolve(), modules.resolve()) |
| 46 | + if calls == 3: |
| 47 | + tarball = temp / "a3s-lab-box-0.1.0.tgz" |
| 48 | + tarball.touch() |
| 49 | + return subprocess.CompletedProcess( |
| 50 | + command, |
| 51 | + 0, |
| 52 | + stdout=f"{tarball.name}\n", |
| 53 | + ) |
| 54 | + return subprocess.CompletedProcess(command, 0) |
| 55 | + |
| 56 | + with ( |
| 57 | + mock.patch.object(run_production, "SDK_ROOT", sdk_root), |
| 58 | + mock.patch.object(run_production.subprocess, "run", side_effect=run), |
| 59 | + ): |
| 60 | + run_production.prepare_native_typescript(temp, client) |
| 61 | + |
| 62 | + self.assertEqual(calls, 4) |
| 63 | + self.assertFalse((temp / "a3s-typescript-sdk" / "node_modules").exists()) |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + unittest.main() |
0 commit comments