test(javascript): share configuration key expectations (#17446)

This commit is contained in:
Vincent Koc
2026-12-06 15:54:49 +08:00
committed by GitHub
parent 075a6a04a5
commit 46b9617629
@@ -58,55 +58,58 @@ it.each([
expect(ir.configurationOperations.map(({ key }) => key)).toEqual(["MODE"]);
});
const noConfigurationKeys = (source: string, why: string) => ({
source,
keys: [],
why,
});
const onlyKeptConfigurationKey = (source: string, why: string) => ({
source,
keys: ["kept"],
why,
});
// Whether `process` still names the original global process object uses
// activation timing, like the require loader. Each row states why the
// replacement is proven to run after the read or write, or why it may not.
it.each([
{
source: `const value = process.env.kept; for (const x of [1]) process = other;`,
keys: ["kept"],
why: "the loop is a later top-level statement, so it starts only after the read",
},
{
source: `const value = process.env.kept; for (const x of []) process = other;`,
keys: ["kept"],
why: "an empty array literal with unchanged array iteration never runs the body",
},
{
source: `process = wrap(process.env.kept);`,
keys: ["kept"],
why: "the right-hand side is evaluated before the assignment stores its value",
},
{
source: `process.env = {}; for (const x of [1]) process = other; const proc = require('process'); const mode = proc.env.wrong;`,
keys: [],
why: "the env write runs before the loop, so it reaches the original object",
},
{
source: `globalThis.process.env = {}; for (const x of [1]) globalThis.process = other; const proc = require('process'); const mode = proc.env.wrong;`,
keys: [],
why: "a qualified write before the loop reaches the original object",
},
{
source: `Array.prototype[Symbol.iterator] = gen; const value = process.env.maybe; for (const x of []) process = other;`,
keys: [],
why: "a replaced array iterator can yield from an empty literal",
},
{
source: `for (const x of [1]) process = other; const value = process.env.wrong;`,
keys: [],
why: "the loop body runs before the read",
},
{
source: `class Box { value = process.env.wrong; } for (const x of [1]) process = other; new Box();`,
keys: [],
why: "an instance field runs on construction, after the loop",
},
{
source: `process = class { value = process.env.wrong; }; new process();`,
keys: [],
why: "a field of the replacement class runs on construction, after the replacement",
},
onlyKeptConfigurationKey(
`const value = process.env.kept; for (const x of [1]) process = other;`,
"the loop is a later top-level statement, so it starts only after the read",
),
onlyKeptConfigurationKey(
`const value = process.env.kept; for (const x of []) process = other;`,
"an empty array literal with unchanged array iteration never runs the body",
),
onlyKeptConfigurationKey(
`process = wrap(process.env.kept);`,
"the right-hand side is evaluated before the assignment stores its value",
),
noConfigurationKeys(
`process.env = {}; for (const x of [1]) process = other; const proc = require('process'); const mode = proc.env.wrong;`,
"the env write runs before the loop, so it reaches the original object",
),
noConfigurationKeys(
`globalThis.process.env = {}; for (const x of [1]) globalThis.process = other; const proc = require('process'); const mode = proc.env.wrong;`,
"a qualified write before the loop reaches the original object",
),
noConfigurationKeys(
`Array.prototype[Symbol.iterator] = gen; const value = process.env.maybe; for (const x of []) process = other;`,
"a replaced array iterator can yield from an empty literal",
),
noConfigurationKeys(
`for (const x of [1]) process = other; const value = process.env.wrong;`,
"the loop body runs before the read",
),
noConfigurationKeys(
`class Box { value = process.env.wrong; } for (const x of [1]) process = other; new Box();`,
"an instance field runs on construction, after the loop",
),
noConfigurationKeys(
`process = class { value = process.env.wrong; }; new process();`,
"a field of the replacement class runs on construction, after the replacement",
),
])(
"orders process replacement by activation: $source ($why)",
({ source, keys }) => {
@@ -121,81 +124,66 @@ it.each([
// A write through `process` reaches the original object unless a replacement
// is proven to run before it. A replacement that may run first is not enough.
it.each([
{
source: `process.env = {}; while (ready) process = other;`,
keys: [],
why: "the loop body may never run, and runs only after the write",
},
{
source: `if (ready) process = other; process.env = {};`,
keys: [],
why: "the replacement may be skipped, so the write may reach the original",
},
{
source: `process.env = {}; if (ready) process = other;`,
keys: [],
why: "the write runs before the replacement",
},
{
source: `if (ready) globalThis.process = other; globalThis.process.env = {};`,
keys: [],
why: "a skipped qualified replacement leaves the original in place",
},
{
source: `function swap() { process = other; } swap(); process.env = {};`,
keys: [],
why: "a replacement inside a function body is not proven to run",
},
{
source: `function write() { process.env = {}; } write(); process = other;`,
keys: [],
why: "the write runs in a call made before the replacement",
},
{
source: `process = other; process.env = {};`,
keys: [],
why: "the replacement may be the original object itself",
},
{
source: `if (ready) process = other; patch(process);`,
keys: [],
why: "an escape that may reach the original is kept like a write",
},
{
source: `if (ready) globalThis.process = other; patch(globalThis.process);`,
keys: [],
why: "a qualified escape that may reach the original is kept",
},
{
source: `if (ready) process = other; process.env.kept = "";`,
keys: [],
why: "a nested write that may reach the original is kept",
},
{
source: `process = {}; patch(process);`,
keys: ["kept"],
why: "a fresh replacement always runs before the escape",
},
{
source: `process = {}; process.env = {};`,
keys: ["kept"],
why: "a fresh object always replaces the original before the write",
},
{
source: `globalThis.process = {}; process.env = {};`,
keys: ["kept"],
why: "a fresh qualified replacement always runs before the write",
},
{
source: `const original = process; process = {}; if (ready) process = original; process.env = {};`,
keys: [],
why: "a later conditional write may restore the original before the write",
},
{
source: `const host = ready ? globalThis : {}; host.process = {}; process.env = {};`,
keys: [],
why: "a write through a possible alias of the global object may not replace process",
},
noConfigurationKeys(
`process.env = {}; while (ready) process = other;`,
"the loop body may never run, and runs only after the write",
),
noConfigurationKeys(
`if (ready) process = other; process.env = {};`,
"the replacement may be skipped, so the write may reach the original",
),
noConfigurationKeys(
`process.env = {}; if (ready) process = other;`,
"the write runs before the replacement",
),
noConfigurationKeys(
`if (ready) globalThis.process = other; globalThis.process.env = {};`,
"a skipped qualified replacement leaves the original in place",
),
noConfigurationKeys(
`function swap() { process = other; } swap(); process.env = {};`,
"a replacement inside a function body is not proven to run",
),
noConfigurationKeys(
`function write() { process.env = {}; } write(); process = other;`,
"the write runs in a call made before the replacement",
),
noConfigurationKeys(
`process = other; process.env = {};`,
"the replacement may be the original object itself",
),
noConfigurationKeys(
`if (ready) process = other; patch(process);`,
"an escape that may reach the original is kept like a write",
),
noConfigurationKeys(
`if (ready) globalThis.process = other; patch(globalThis.process);`,
"a qualified escape that may reach the original is kept",
),
noConfigurationKeys(
`if (ready) process = other; process.env.kept = "";`,
"a nested write that may reach the original is kept",
),
onlyKeptConfigurationKey(
`process = {}; patch(process);`,
"a fresh replacement always runs before the escape",
),
onlyKeptConfigurationKey(
`process = {}; process.env = {};`,
"a fresh object always replaces the original before the write",
),
onlyKeptConfigurationKey(
`globalThis.process = {}; process.env = {};`,
"a fresh qualified replacement always runs before the write",
),
noConfigurationKeys(
`const original = process; process = {}; if (ready) process = original; process.env = {};`,
"a later conditional write may restore the original before the write",
),
noConfigurationKeys(
`const host = ready ? globalThis : {}; host.process = {}; process.env = {};`,
"a write through a possible alias of the global object may not replace process",
),
])(
"attributes process writes that may reach the original: $source ($why)",
({ source, keys }) => {
@@ -890,116 +878,94 @@ it.each([
// parameters, whose effects are indexed like any other source effect. Each
// row states why `fs.readFileSync` is or is not still the original method.
it.each([
{
source: `(function(p){})(fs);`,
keys: ["kept"],
why: "the body never uses the argument",
},
{
source: `(function(p){ p.readFileSync = custom })({}, fs);`,
keys: ["kept"],
why: "no parameter binds the second argument and the body never reads arguments",
},
{
source: `(function(p, p){ p.readFileSync = custom })(fs, other);`,
keys: ["kept"],
why: "the later duplicate parameter binds the other argument",
},
{
source: `(function(p){ p.readFileSync = custom })(fs);`,
keys: [],
why: "a write through the parameter reaches the argument",
},
{
source: `(function(p){ var q = p; q.readFileSync = custom })(fs);`,
keys: [],
why: "a local alias of the parameter reaches the argument",
},
{
source: `(function(p){ later(() => { p.readFileSync = custom }) })(fs);`,
keys: [],
why: "a closure over the parameter reaches the argument",
},
{
source: `(function(p){ return p })(fs).readFileSync = custom;`,
keys: [],
why: "the returned parameter is the argument",
},
{
source: `(function(p){ patch(p) })(fs);`,
keys: [],
why: "the parameter escapes to unknown code",
},
{
source: `(function(p){ store.v = p })(fs);`,
keys: [],
why: "the parameter is stored in an unknown object",
},
{
source: `(function(p){ arguments[0].readFileSync = custom })(fs);`,
keys: [],
why: "the arguments object reaches the argument",
},
{
source: `(function(...r){ r[0].readFileSync = custom })(fs);`,
keys: [],
why: "a rest parameter is a new array holding the argument",
},
{
source: `(function(p){ eval(src) })(fs);`,
keys: [],
why: "direct eval may reach the parameter",
},
{
source: `const box = {fs}; (function(b){ b.fs.readFileSync = custom })(box);`,
keys: [],
why: "a write below the parameter reaches the argument's child",
},
{
source: `unknown(fs);`,
keys: [],
why: "an unknown callee still receives an escape",
},
{
source: `(function(get){ patch(get()) })(() => fs);`,
keys: [],
why: "a callable argument's result reaches the parameter's callers",
},
{
source: `(function(get){ get().readFileSync = custom })(() => fs);`,
keys: [],
why: "a write through a callable argument's result reaches its value",
},
{
source: `(function f(p){ patch(f["arguments"][0]) })(fs);`,
keys: [],
why: "a sloppy function's arguments property reaches the argument",
},
{
source: `(function f(p){ patch(f.arguments[0]) })(fs);`,
keys: [],
why: "a sloppy function's arguments property reaches the argument",
},
{
source: `function g(){ patch(g.caller.arguments[0]) } (function(p){ g() })(fs);`,
keys: [],
why: "a called sloppy function reaches its caller's arguments",
},
{
source: `function g(){ patch(g.caller) } (function(p){ "use strict"; g() })(fs);`,
keys: ["kept"],
why: "a strict function exposes no arguments property",
},
{
source: `try { (function(p){ throw p })(fs); } catch (e) { patch(e) }`,
keys: [],
why: "a value thrown out of the callee reaches its caller's handler",
},
{
source: `try { (function(){ throw fs })(); } catch (e) { patch(e) }`,
keys: [],
why: "a captured value thrown out of a function reaches the caller's handler",
},
onlyKeptConfigurationKey(
`(function(p){})(fs);`,
"the body never uses the argument",
),
onlyKeptConfigurationKey(
`(function(p){ p.readFileSync = custom })({}, fs);`,
"no parameter binds the second argument and the body never reads arguments",
),
onlyKeptConfigurationKey(
`(function(p, p){ p.readFileSync = custom })(fs, other);`,
"the later duplicate parameter binds the other argument",
),
noConfigurationKeys(
`(function(p){ p.readFileSync = custom })(fs);`,
"a write through the parameter reaches the argument",
),
noConfigurationKeys(
`(function(p){ var q = p; q.readFileSync = custom })(fs);`,
"a local alias of the parameter reaches the argument",
),
noConfigurationKeys(
`(function(p){ later(() => { p.readFileSync = custom }) })(fs);`,
"a closure over the parameter reaches the argument",
),
noConfigurationKeys(
`(function(p){ return p })(fs).readFileSync = custom;`,
"the returned parameter is the argument",
),
noConfigurationKeys(
`(function(p){ patch(p) })(fs);`,
"the parameter escapes to unknown code",
),
noConfigurationKeys(
`(function(p){ store.v = p })(fs);`,
"the parameter is stored in an unknown object",
),
noConfigurationKeys(
`(function(p){ arguments[0].readFileSync = custom })(fs);`,
"the arguments object reaches the argument",
),
noConfigurationKeys(
`(function(...r){ r[0].readFileSync = custom })(fs);`,
"a rest parameter is a new array holding the argument",
),
noConfigurationKeys(
`(function(p){ eval(src) })(fs);`,
"direct eval may reach the parameter",
),
noConfigurationKeys(
`const box = {fs}; (function(b){ b.fs.readFileSync = custom })(box);`,
"a write below the parameter reaches the argument's child",
),
noConfigurationKeys(
`unknown(fs);`,
"an unknown callee still receives an escape",
),
noConfigurationKeys(
`(function(get){ patch(get()) })(() => fs);`,
"a callable argument's result reaches the parameter's callers",
),
noConfigurationKeys(
`(function(get){ get().readFileSync = custom })(() => fs);`,
"a write through a callable argument's result reaches its value",
),
noConfigurationKeys(
`(function f(p){ patch(f["arguments"][0]) })(fs);`,
"a sloppy function's arguments property reaches the argument",
),
noConfigurationKeys(
`(function f(p){ patch(f.arguments[0]) })(fs);`,
"a sloppy function's arguments property reaches the argument",
),
noConfigurationKeys(
`function g(){ patch(g.caller.arguments[0]) } (function(p){ g() })(fs);`,
"a called sloppy function reaches its caller's arguments",
),
onlyKeptConfigurationKey(
`function g(){ patch(g.caller) } (function(p){ "use strict"; g() })(fs);`,
"a strict function exposes no arguments property",
),
noConfigurationKeys(
`try { (function(p){ throw p })(fs); } catch (e) { patch(e) }`,
"a value thrown out of the callee reaches its caller's handler",
),
noConfigurationKeys(
`try { (function(){ throw fs })(); } catch (e) { patch(e) }`,
"a captured value thrown out of a function reaches the caller's handler",
),
])(
"tracks direct invocation arguments through parameters: $source ($why)",
({ source, keys }) => {