Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 97888e0

Browse filesBrowse files
tinybeachthorgibfahn
authored andcommitted
test: add es6 module global leakage tests
* created testing es6 module for global leakage tests * fixed a couple basic errors (vars -> lets, ...) * added globals leakage detection to es module tests * reversed whitespace change * whitespace change reversed PR-URL: #16341 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 28a0c10 commit 97888e0
Copy full SHA for 97888e0

File tree

Expand file treeCollapse file tree

5 files changed

+113
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+113
-0
lines changed
Open diff view settings
Collapse file

‎test/common/index.mjs‎

Copy file name to clipboard
+109Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Flags: --experimental-modules
2+
/* eslint-disable required-modules */
3+
4+
import assert from 'assert';
5+
6+
let knownGlobals = [
7+
Buffer,
8+
clearImmediate,
9+
clearInterval,
10+
clearTimeout,
11+
console,
12+
constructor, // Enumerable in V8 3.21.
13+
global,
14+
process,
15+
setImmediate,
16+
setInterval,
17+
setTimeout
18+
];
19+
20+
if (process.env.NODE_TEST_KNOWN_GLOBALS) {
21+
const knownFromEnv = process.env.NODE_TEST_KNOWN_GLOBALS.split(',');
22+
allowGlobals(...knownFromEnv);
23+
}
24+
25+
export function allowGlobals(...whitelist) {
26+
knownGlobals = knownGlobals.concat(whitelist);
27+
}
28+
29+
export function leakedGlobals() {
30+
//add possible expected globals
31+
if (global.gc) {
32+
knownGlobals.push(global.gc);
33+
}
34+
35+
if (global.DTRACE_HTTP_SERVER_RESPONSE) {
36+
knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE);
37+
knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST);
38+
knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE);
39+
knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST);
40+
knownGlobals.push(DTRACE_NET_STREAM_END);
41+
knownGlobals.push(DTRACE_NET_SERVER_CONNECTION);
42+
}
43+
44+
if (global.COUNTER_NET_SERVER_CONNECTION) {
45+
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION);
46+
knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE);
47+
knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST);
48+
knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE);
49+
knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST);
50+
knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE);
51+
}
52+
53+
if (global.LTTNG_HTTP_SERVER_RESPONSE) {
54+
knownGlobals.push(LTTNG_HTTP_SERVER_RESPONSE);
55+
knownGlobals.push(LTTNG_HTTP_SERVER_REQUEST);
56+
knownGlobals.push(LTTNG_HTTP_CLIENT_RESPONSE);
57+
knownGlobals.push(LTTNG_HTTP_CLIENT_REQUEST);
58+
knownGlobals.push(LTTNG_NET_STREAM_END);
59+
knownGlobals.push(LTTNG_NET_SERVER_CONNECTION);
60+
}
61+
62+
if (global.ArrayBuffer) {
63+
knownGlobals.push(ArrayBuffer);
64+
knownGlobals.push(Int8Array);
65+
knownGlobals.push(Uint8Array);
66+
knownGlobals.push(Uint8ClampedArray);
67+
knownGlobals.push(Int16Array);
68+
knownGlobals.push(Uint16Array);
69+
knownGlobals.push(Int32Array);
70+
knownGlobals.push(Uint32Array);
71+
knownGlobals.push(Float32Array);
72+
knownGlobals.push(Float64Array);
73+
knownGlobals.push(DataView);
74+
}
75+
76+
// Harmony features.
77+
if (global.Proxy) {
78+
knownGlobals.push(Proxy);
79+
}
80+
81+
if (global.Symbol) {
82+
knownGlobals.push(Symbol);
83+
}
84+
85+
const leaked = [];
86+
87+
for (const val in global) {
88+
if (!knownGlobals.includes(global[val])) {
89+
leaked.push(val);
90+
}
91+
}
92+
93+
if (global.__coverage__) {
94+
return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname));
95+
} else {
96+
return leaked;
97+
}
98+
}
99+
100+
// Turn this off if the test should not check for global leaks.
101+
export let globalCheck = true; // eslint-disable-line
102+
103+
process.on('exit', function() {
104+
if (!globalCheck) return;
105+
const leaked = leakedGlobals();
106+
if (leaked.length > 0) {
107+
assert.fail(`Unexpected global(s) found: ${leaked.join(', ')}`);
108+
}
109+
});
Collapse file

‎test/es-module/test-esm-json.mjs‎

Copy file name to clipboardExpand all lines: test/es-module/test-esm-json.mjs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Flags: --experimental-modules
22
/* eslint-disable required-modules */
3+
import '../common/index';
34
import assert from 'assert';
45
import ok from './test-esm-ok.mjs';
56
import json from './json.json';
Collapse file

‎test/es-module/test-esm-named-exports.mjs‎

Copy file name to clipboardExpand all lines: test/es-module/test-esm-named-exports.mjs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs
22
/* eslint-disable required-modules */
3+
import '../common/index';
34
import { readFile } from 'fs';
45
import assert from 'assert';
56
import ok from './test-esm-ok.mjs';
Collapse file

‎test/es-module/test-esm-namespace.mjs‎

Copy file name to clipboardExpand all lines: test/es-module/test-esm-namespace.mjs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Flags: --experimental-modules
22
/* eslint-disable required-modules */
33

4+
import '../common/index';
45
import * as fs from 'fs';
56
import assert from 'assert';
67

Collapse file

‎test/es-module/test-esm-snapshot.mjs‎

Copy file name to clipboardExpand all lines: test/es-module/test-esm-snapshot.mjs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Flags: --experimental-modules
22
/* eslint-disable required-modules */
3+
import '../common/index';
34
import './esm-snapshot-mutator';
45
import one from './esm-snapshot';
56
import assert from 'assert';

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.