Skip to content

Commit 91d04fa

Browse files
author
Roy Lin
committed
fix(sdk): resolve pinned TypeScript dependencies
1 parent cb292a4 commit 91d04fa

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

compat/e2b/fixtures/official-clients/run_production.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ def prepare_native_typescript(temp: Path, client: Path) -> None:
5454
check=True,
5555
)
5656
compiler = environment / "node_modules" / ".bin" / "tsc"
57-
subprocess.run([str(compiler), "-p", "tsconfig.json"], cwd=build_source, check=True)
57+
dependencies = build_source / "node_modules"
58+
dependencies.symlink_to(environment / "node_modules", target_is_directory=True)
59+
try:
60+
subprocess.run(
61+
[str(compiler), "-p", "tsconfig.json"],
62+
cwd=build_source,
63+
check=True,
64+
)
65+
finally:
66+
dependencies.unlink(missing_ok=True)
5867
packed = subprocess.run(
5968
["npm", "pack", "--ignore-scripts", "--pack-destination", str(temp)],
6069
cwd=build_source,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)