Skip to content

Commit 7ba7fcb

Browse files
committed
test: test fs patches via cjs and esm
1 parent 988f79e commit 7ba7fcb

File tree

13 files changed

+369
-232
lines changed

13 files changed

+369
-232
lines changed

.aspect/rules/external_repository_action_cache/npm_translate_lock_LTE4Nzc1MDcwNjU=

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ npm/private/test/package.json=322962683
3131
npm/private/test/vendored/is-odd/package.json=1041695223
3232
npm/private/test/vendored/lodash-4.17.21.tgz=-1206623349
3333
npm/private/test/vendored/semver-max/package.json=578664053
34-
package.json=1068891966
35-
pnpm-lock.yaml=1499299525
34+
package.json=234760749
35+
pnpm-lock.yaml=-2117469023
3636
pnpm-workspace.yaml=854106668

js/private/node-patches/fs.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const util = require("util");
4747
// es modules
4848
const fs = require('node:fs');
4949
const url = require('node:url');
50+
const esmModule = require('node:module');
5051
const HOP_NON_LINK = Symbol.for('HOP NON LINK');
5152
const HOP_NOT_FOUND = Symbol.for('HOP NOT FOUND');
5253
const PATCHED_FS_METHODS = [
@@ -747,12 +748,18 @@ function patcher(roots) {
747748
}
748749
}
749750
}
751+
// Sync the esm modules to use the now patched fs cjs module.
752+
// See: https://nodejs.org/api/esm.html#builtin-modules
753+
esmModule.syncBuiltinESMExports();
750754
return function revertPatch() {
751755
Object.assign(fs, fs._unpatched);
752756
delete fs._unpatched;
753757
if (unpatchPromises) {
754758
unpatchPromises();
755759
}
760+
// Sync the esm modules to use the now patched fs cjs module.
761+
// See: https://nodejs.org/api/esm.html#builtin-modules
762+
esmModule.syncBuiltinESMExports();
756763
};
757764
}
758765
// =========================================================================

js/private/node-patches/register.cjs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,11 @@ if (
3535
JS_BINARY__PATCH_NODE_FS != '0' &&
3636
JS_BINARY__FS_PATCH_ROOTS
3737
) {
38-
const module = require('node:module')
3938
const roots = JS_BINARY__FS_PATCH_ROOTS.split(':')
4039
if (JS_BINARY__LOG_DEBUG) {
4140
console.error(
4241
`DEBUG: ${JS_BINARY__LOG_PREFIX}: node fs patches will be applied with roots: ${roots}`
4342
)
4443
}
4544
patchfs(roots)
46-
47-
// Sync the esm modules to use the now patched fs cjs module.
48-
// See: https://nodejs.org/api/esm.html#builtin-modules
49-
module.syncBuiltinESMExports()
5045
}

js/private/node-patches/src/fs.cts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Dirent = any
3030
// es modules
3131
const fs = require('node:fs') as any
3232
const url = require('node:url') as typeof UrlType
33+
const esmModule = require('node:module')
3334

3435
const HOP_NON_LINK = Symbol.for('HOP NON LINK')
3536
const HOP_NOT_FOUND = Symbol.for('HOP NOT FOUND')
@@ -860,13 +861,21 @@ export function patcher(roots: string[]): () => void {
860861
}
861862
}
862863

864+
// Sync the esm modules to use the now patched fs cjs module.
865+
// See: https://nodejs.org/api/esm.html#builtin-modules
866+
esmModule.syncBuiltinESMExports()
867+
863868
return function revertPatch() {
864869
Object.assign(fs, fs._unpatched)
865870
delete fs._unpatched
866871

867872
if (unpatchPromises) {
868873
unpatchPromises()
869874
}
875+
876+
// Sync the esm modules to use the now patched fs cjs module.
877+
// See: https://nodejs.org/api/esm.html#builtin-modules
878+
esmModule.syncBuiltinESMExports()
870879
}
871880
}
872881

js/private/test/node-patches/BUILD.bazel

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")
2+
load("@npm//:@babel/cli/package_json.bzl", babel_bin = "bin")
23
load("//js:defs.bzl", "js_test")
34

45
TESTS = [
56
"escape.js",
6-
"lstat.js",
7-
"opendir.js",
8-
"readdir.js",
9-
"readlink.js",
10-
"realpath.js",
7+
"lstat.mjs",
8+
"opendir.mjs",
9+
"readdir.mjs",
10+
"readlink.mjs",
11+
"realpath.mjs",
12+
]
13+
14+
CJS_TESTS = [
15+
t.replace(".mjs", ".cjs")
16+
for t in TESTS
1117
]
1218

1319
# Multiple node toolchains for testing across versions
@@ -39,26 +45,65 @@ TOOLCHAINS_VERSIONS = [
3945
for t in TESTS
4046
]
4147

48+
babel_bin.babel(
49+
name = "babel_mjs2js",
50+
srcs = [t for t in TESTS if t.endswith(".mjs")] + [
51+
"babel.config.js",
52+
"//:node_modules/@babel/plugin-transform-modules-commonjs",
53+
],
54+
outs = [t.replace(".mjs", ".cjs") for t in TESTS if t.endswith(".mjs")],
55+
args = [
56+
"--extensions",
57+
".mjs",
58+
"--out-file-extension",
59+
".cjs",
60+
"--out-dir",
61+
".",
62+
] + [t for t in TESTS if t.endswith(".mjs")],
63+
chdir = package_name(), # to automatically pickup babel.config.js
64+
)
65+
4266
# Basic tests
4367
[
4468
[
45-
js_test(
46-
name = "{}_{}_test".format(
47-
t.replace(".js", ""),
48-
toolchain_name,
49-
),
50-
data = [
51-
"//:node_modules/inline-fixtures",
52-
"//js/private/node-patches/src:compile",
53-
],
54-
entry_point = "copy_entry_{}".format(t),
55-
node_toolchain = toolchain,
56-
patch_node_fs = False,
57-
# Without node patches on for these tests, the program is going to escape the sandbox if it
58-
# is on since the fs patches are not on for the tests as they are the code under test
59-
tags = ["no-sandbox"],
60-
)
61-
for t in TESTS
69+
[
70+
js_test(
71+
name = "{}_{}_test".format(
72+
t.replace(".mjs", "").replace(".js", ""),
73+
toolchain_name,
74+
),
75+
data = [
76+
"//:node_modules/inline-fixtures",
77+
"//js/private/node-patches/src:compile",
78+
],
79+
entry_point = "copy_entry_{}".format(t) if (t in TESTS) else t,
80+
node_toolchain = toolchain,
81+
patch_node_fs = False,
82+
# Without node patches on for these tests, the program is going to escape the sandbox if it
83+
# is on since the fs patches are not on for the tests as they are the code under test
84+
tags = ["no-sandbox"],
85+
)
86+
for t in TESTS
87+
],
88+
[
89+
js_test(
90+
name = "{}_{}_cjs_test".format(
91+
t.replace(".cjs", ""),
92+
toolchain_name,
93+
),
94+
data = [
95+
"//:node_modules/inline-fixtures",
96+
"//js/private/node-patches/src:compile",
97+
],
98+
entry_point = "copy_entry_{}".format(t) if (t in TESTS) else t,
99+
node_toolchain = toolchain,
100+
patch_node_fs = False,
101+
# Without node patches on for these tests, the program is going to escape the sandbox if it
102+
# is on since the fs patches are not on for the tests as they are the code under test
103+
tags = ["no-sandbox"],
104+
)
105+
for t in CJS_TESTS
106+
],
62107
]
63108
for toolchain_name, toolchain in zip(
64109
TOOLCHAINS_NAMES,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
plugins: [
3+
[
4+
'@babel/plugin-transform-modules-commonjs',
5+
{
6+
importInterop: 'none',
7+
},
8+
],
9+
],
10+
}

js/private/test/node-patches/lstat.js renamed to js/private/test/node-patches/lstat.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
const assert = require('assert')
18-
const fs = require('fs')
19-
const withFixtures = require('inline-fixtures').withFixtures
20-
const path = require('path')
21-
const util = require('util')
17+
import * as assert from 'node:assert'
18+
import * as fs from 'node:fs'
19+
import { withFixtures } from 'inline-fixtures'
20+
import * as path from 'node:path'
21+
import * as util from 'node:util'
2222

23-
const patcher = require('../../node-patches/src/fs.cjs').patcher
23+
import { patcher } from '../../node-patches/src/fs.cjs'
2424

2525
// We don't want to bring jest into this repo so we just fake the describe and it functions here
2626
async function describe(_, fn) {
@@ -197,7 +197,7 @@ describe('testing lstat', async () => {
197197
brokenLinkPath
198198
)
199199

200-
stat = await fs.promises.lstat(brokenLinkPath)
200+
const stat = await fs.promises.lstat(brokenLinkPath)
201201
assert.ok(
202202
stat.isSymbolicLink(),
203203
'if a symlink is broken but not escaping return it as a link.'

js/private/test/node-patches/opendir.js renamed to js/private/test/node-patches/opendir.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
const assert = require('assert')
18-
const fs = require('fs')
19-
const withFixtures = require('inline-fixtures').withFixtures
20-
const path = require('path')
21-
const util = require('util')
17+
import * as assert from 'node:assert'
18+
import * as fs from 'node:fs'
19+
import { withFixtures } from 'inline-fixtures'
20+
import * as path from 'node:path'
21+
import * as util from 'node:util'
2222

23-
const patcher = require('../../node-patches/src/fs.cjs').patcher
23+
import { patcher } from '../../node-patches/src/fs.cjs'
2424

2525
// We don't want to bring jest into this repo so we just fake the describe and it functions here
2626
async function describe(_, fn) {

js/private/test/node-patches/readdir.js renamed to js/private/test/node-patches/readdir.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
const assert = require('assert')
18-
const fs = require('fs')
19-
const withFixtures = require('inline-fixtures').withFixtures
20-
const path = require('path')
21-
const util = require('util')
17+
import * as assert from 'node:assert'
18+
import * as fs from 'node:fs'
19+
import { withFixtures } from 'inline-fixtures'
20+
import * as path from 'node:path'
21+
import * as util from 'node:util'
2222

23-
const patcher = require('../../node-patches/src/fs.cjs').patcher
23+
import { patcher } from '../../node-patches/src/fs.cjs'
2424

2525
// We don't want to bring jest into this repo so we just fake the describe and it functions here
2626
async function describe(_, fn) {

js/private/test/node-patches/readlink.js renamed to js/private/test/node-patches/readlink.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
const assert = require('assert')
18-
const fs = require('fs')
19-
const withFixtures = require('inline-fixtures').withFixtures
20-
const path = require('path')
21-
const util = require('util')
17+
import * as assert from 'node:assert'
18+
import * as fs from 'node:fs'
19+
import { withFixtures } from 'inline-fixtures'
20+
import * as path from 'node:path'
21+
import * as util from 'node:util'
2222

23-
const patcher = require('../../node-patches/src/fs.cjs').patcher
23+
import { patcher } from '../../node-patches/src/fs.cjs'
2424

2525
// We don't want to bring jest into this repo so we just fake the describe and it functions here
2626
async function describe(_, fn) {

0 commit comments

Comments
 (0)