Skip to content

Commit a491c96

Browse files
committed
test: add additional esm sandbox assertions
1 parent ee2708b commit a491c96

25 files changed

Lines changed: 209 additions & 66 deletions

.aspect/rules/external_repository_action_cache/npm_translate_lock_LTE4Nzc1MDcwNjU=

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ examples/npm_deps/patches/[email protected]=-442666336
1616
examples/npm_package/libs/lib_a/package.json=-1377103079
1717
examples/npm_package/packages/pkg_a/package.json=1006424040
1818
examples/npm_package/packages/pkg_b/package.json=1041247977
19-
examples/npm_package/packages/pkg_d/package.json=1110895851
20-
examples/npm_package/packages/pkg_e/package.json=-2145239245
19+
examples/npm_package/packages/pkg_d/package.json=-1434007576
20+
examples/npm_package/packages/pkg_e/package.json=-336138332
2121
examples/runfiles/package.json=-1545884645
2222
examples/stack_traces/package.json=2011229626
2323
examples/webpack_cli/package.json=1911342006

examples/js_binary/BUILD.bazel

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ js_binary(
2525
name = "bin",
2626
# Reference the location where the acorn npm module was linked in the root Bazel package
2727
data = ["//:node_modules/acorn"],
28-
entry_point = "require_acorn.js",
28+
entry_point = "require_acorn.mjs",
29+
)
30+
31+
js_binary(
32+
name = "bin_cjs",
33+
# Reference the location where the acorn npm module was linked in the root Bazel package
34+
data = ["//:node_modules/acorn"],
35+
entry_point = "require_acorn.cjs",
2936
)
3037

3138
####################################################
@@ -48,6 +55,22 @@ diff_test(
4855
file2 = "out1",
4956
)
5057

58+
genrule(
59+
name = "run1_cjs",
60+
srcs = [],
61+
outs = ["out1_cjs"],
62+
# All js_binary rules need a BAZEL_BINDIR environment variable set so they can
63+
# run from that directory as the working directory.
64+
cmd = "BAZEL_BINDIR=$(BINDIR) $(location :bin_cjs) {}/out1_cjs".format(package_name()),
65+
tools = [":bin_cjs"],
66+
)
67+
68+
diff_test(
69+
name = "test_js_binary_under_genrule_cjs",
70+
file1 = "//examples:expected_one_ast.json",
71+
file2 = "out1_cjs",
72+
)
73+
5174
####################################################
5275
# Use case 2
5376
# Using js_run_binary has some nice syntax sugar vs.
@@ -73,6 +96,26 @@ diff_test(
7396
file2 = "out2",
7497
)
7598

99+
js_run_binary(
100+
name = "run2_cjs",
101+
srcs = [],
102+
outs = ["out2_cjs"],
103+
args = ["out2_cjs"],
104+
chdir = package_name(),
105+
# Request that the rules_js launcher prints extra information
106+
log_level = "debug",
107+
tool = ":bin_cjs",
108+
# Uncomment the setting below to see debug output even on a
109+
# successful run of the build action.
110+
# silent_on_success = False,
111+
)
112+
113+
diff_test(
114+
name = "test_js_binary_under_js_run_binary_cjs",
115+
file1 = "//examples:expected_one_ast.json",
116+
file2 = "out2_cjs",
117+
)
118+
76119
# Also test with local (no sandbox) execution by setting execution_requirements "local" to "1".
77120
# Bazel sets different environment variables in this case such as RUNFILES_MANIFEST_FILE.
78121
# This case tests for regression of the fix in https://github.com/aspect-build/rules_js/pull/323.
@@ -101,16 +144,16 @@ diff_test(
101144

102145
# Also test with copy_data_to_bin disabled
103146
copy_to_bin(
104-
name = "require_acorn_js",
105-
srcs = ["require_acorn.js"],
147+
name = "require_acorn_mjs",
148+
srcs = ["require_acorn.mjs"],
106149
)
107150

108151
js_binary(
109152
name = "bin_no_copy_data_to_bin",
110153
copy_data_to_bin = False,
111154
# Reference the location where the acorn npm module was linked in the root Bazel package
112155
data = ["//:node_modules/acorn"],
113-
entry_point = ":require_acorn_js",
156+
entry_point = ":require_acorn_mjs",
114157
)
115158

116159
js_run_binary(
@@ -142,7 +185,7 @@ diff_test(
142185
js_test(
143186
name = "test_test",
144187
data = ["//:node_modules/@types/node"],
145-
entry_point = "test.js",
188+
entry_point = "test.mjs",
146189
)
147190

148191
###############################
@@ -152,11 +195,12 @@ js_test(
152195

153196
write_file(
154197
name = "write4",
155-
out = "case4.js",
198+
out = "case4.mjs",
156199
content = [
157-
"""require('fs').writeFileSync(
200+
"import { writeFileSync } from 'fs'",
201+
"""writeFileSync(
158202
process.argv[2],
159-
require(process.argv[3]).toAst("1")
203+
(await import(process.argv[3])).toAst("1")
160204
)""",
161205
],
162206
)
@@ -167,7 +211,7 @@ js_binary(
167211
":node_modules/@mycorp/pkg-a",
168212
"//:node_modules/@mycorp/pkg-b",
169213
],
170-
entry_point = "case4.js",
214+
entry_point = "case4.mjs",
171215
)
172216

173217
js_run_binary(
@@ -228,12 +272,13 @@ diff_test(
228272

229273
write_file(
230274
name = "write5",
231-
out = "case5.js",
232-
content = ["""\
233-
require('fs').writeFileSync(
234-
process.argv[2],
235-
JSON.stringify(require(require('path').join(process.cwd(), "data.json")))
236-
)"""],
275+
out = "case5.mjs",
276+
content = [
277+
"import { writeFileSync } from 'fs';",
278+
"import { join } from 'path';",
279+
"const jsonData = await import(join(process.cwd(), 'data.json'), { with: { type: 'json' } });",
280+
"writeFileSync(process.argv[2], JSON.stringify(jsonData.default))",
281+
],
237282
)
238283

239284
write_file(
@@ -244,7 +289,7 @@ write_file(
244289

245290
js_binary(
246291
name = "bin5",
247-
entry_point = "case5.js",
292+
entry_point = "case5.mjs",
248293
)
249294

250295
js_run_binary(
@@ -557,11 +602,13 @@ js_test(
557602

558603
write_file(
559604
name = "write13",
560-
out = "case13.js",
605+
out = "case13.mjs",
561606
content = [
562-
"""require('fs').writeFileSync(
607+
"import { writeFileSync } from 'fs';",
608+
"""
609+
writeFileSync(
563610
process.argv[2],
564-
JSON.stringify(require(process.argv[3]))
611+
JSON.stringify((await import(process.argv[3])).default)
565612
)""",
566613
],
567614
)
@@ -572,7 +619,7 @@ js_binary(
572619
"//:node_modules/@mycorp/pkg-c1",
573620
"//:node_modules/@mycorp/pkg-c2",
574621
],
575-
entry_point = "case13.js",
622+
entry_point = "case13.mjs",
576623
)
577624

578625
write_file(
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @fileoverview minimal test program that requires a third-party package from npm
3+
*/
4+
import { writeFileSync } from 'fs'
5+
import { parse } from 'acorn'
6+
7+
writeFileSync(
8+
process.argv[2],
9+
JSON.stringify(parse('1', { ecmaVersion: 2020 })) + '\n'
10+
)

examples/js_binary/test.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/js_binary/test.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('\n\n\nThis is only a test.\n\n\n')
2+
console.log(import.meta.resolve('@types/node/package.json'))

examples/macro/mocha.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ def mocha_test(name, srcs, args = [], data = [], env = {}, **kwargs):
2222
# to the location Bazel expects.
2323
"MOCHA_FILE": "$$XML_OUTPUT_FILE",
2424
}),
25+
preserve_symlinks_main = False,
26+
copy_data_to_bin = False,
2527
**kwargs
2628
)

examples/macro/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const assert = require('assert')
2+
const { dirname } = require('node:path')
23

34
describe('mocha', () => {
45
it('integrates with Bazel', () => {
@@ -8,4 +9,13 @@ describe('mocha', () => {
89
it('is in bazel-out', () => {
910
assert.match(__dirname, /bazel-out/)
1011
})
12+
13+
it('is sandboxed', () => {
14+
assert.match(__dirname, /examples\/macro\/test_\/test\.runfiles/)
15+
assert.match(
16+
__filename,
17+
/-sandbox\/\d+\/execroot\/_main\/bazel-out\/[^/]+\/bin\/examples\/macro\/test_\/test.runfiles\/_main\/examples\/macro\/test\.js/
18+
)
19+
assert.equal(__dirname, dirname(__filename))
20+
})
1121
})

examples/npm_deps/BUILD.bazel

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,31 +265,37 @@ js_test(
265265

266266
write_file(
267267
name = "write9",
268-
out = "case9.js",
269-
content = ["require('@mycorp/pkg-d')"],
268+
out = "case9.mjs",
269+
content = [
270+
"import { sandboxAssert } from '@mycorp/pkg-d'",
271+
"sandboxAssert()",
272+
],
270273
)
271274

272275
js_test(
273276
name = "test9",
274277
data = [
275278
":node_modules/@mycorp/pkg-d",
276279
],
277-
entry_point = "case9.js",
280+
entry_point = "case9.mjs",
278281
)
279282

280283
#######################################
281284
# Case 10: use a first-party npm package a transitive dependency on a monorepo workspace from a js_library target
282285

283286
write_file(
284287
name = "write10",
285-
out = "case10.js",
286-
content = ["require('@mycorp/pkg-e')"],
288+
out = "case10.mjs",
289+
content = [
290+
"import { sandboxAssert } from '@mycorp/pkg-e'",
291+
"sandboxAssert()",
292+
],
287293
)
288294

289295
js_test(
290296
name = "test10",
291297
data = [
292298
":node_modules/@mycorp/pkg-e",
293299
],
294-
entry_point = "case10.js",
300+
entry_point = "case10.mjs",
295301
)

examples/npm_package/packages/pkg_c/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ npm_package(
99
srcs = [
1010
"data1/package.json",
1111
"data1/pkg-c.json",
12-
"src/index.js",
12+
"src/index.mjs",
1313
],
1414
package = "@mycorp/pkg-c1",
1515
root_paths = [
@@ -25,7 +25,7 @@ npm_package(
2525
srcs = [
2626
"data2/package.json",
2727
"data2/pkg-c.json",
28-
"src/index.js",
28+
"src/index.mjs",
2929
],
3030
package = "@mycorp/pkg-c2",
3131
root_paths = [

0 commit comments

Comments
 (0)