Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test,tools: move Flags: comment processing from Python to JavaScript #25167

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
4cdc61e
test: add common.exposeInternals()
Trott Dec 19, 2018
104854c
test: add common.relaunchWithFlags()
Trott Dec 19, 2018
afdddac
test: use common.relaunchWithFlags() for --expose-gc
Trott Dec 19, 2018
510fe11
test: introduce common.experimentalWorker()
Trott Dec 19, 2018
98fabd8
test: use common.relaunchWithFlags() for --no-force-async-hooks-check
Trott Dec 19, 2018
648a7c4
test: use --experimental-modules automatically for es-module tests
Trott Dec 20, 2018
6021445
test: use common.relaunchWithFlags() for --inspect=0
Trott Dec 21, 2018
4329520
test: use common.relaunchWithFlags() for --trace-warnings
Trott Dec 21, 2018
169b43f
test: use common.relaunchWithFlags() for --no-warnings
Trott Dec 21, 2018
013dd61
test: use common.relaunchWithFlags() for --pending-deprecations
Trott Dec 21, 2018
50b7fa6
test: use common.relaunchWithFlags() for --zero-fill-buffers
Trott Dec 21, 2018
df816e3
test: use common.relaunchWithFlags() for '--debug-code'
Trott Dec 21, 2018
2078385
test: use common.relaunchWithFlags() for --expose-externalize-string
Trott Dec 21, 2018
ed8e54b
test: use common.relaunchWithFlags() for --allow-natives-syntax
Trott Dec 21, 2018
2429547
test: use common.relaunchWithFlags() for --tls-v1.* flags
Trott Dec 21, 2018
1dce939
test: remove unneeded use of --no-deprecation
Trott Dec 21, 2018
4817b2b
test: use common.relaunchWithFlags() for --preserve-symlinks
Trott Dec 21, 2018
3a5b5d7
test: use common.relaunchWithFlags() for special-case --expose-internals
Trott Dec 21, 2018
6eecd13
test: use common.relaunchWithFlags() for --abort-on-uncaught-exception
Trott Dec 21, 2018
35e141c
test: use common.relaunchWithFlags() for --title
Trott Dec 21, 2018
33a7f0f
test: use common.relaunchWithFlags for --experimental-repl-await
Trott Dec 21, 2018
3c5cd68
test: use common.relaunchWithFlags() for --use-bundled-ca
Trott Dec 21, 2018
e9ff344
test: use common.relaunchWithFlags() for --experimental-vm-modules
Trott Dec 21, 2018
c662003
test: use common.relaunchWithFlags() for --max-old-space-size
Trott Dec 21, 2018
482ef23
test: use common.relaunchWithFlags() for --require
Trott Dec 21, 2018
ad858a9
test: remove check for Flags: in common module
Trott Dec 19, 2018
97b2101
test: remove Flags: functionality from Python test runner
Trott Dec 21, 2018
2fbf88f
test: implement common.requireFlags()
Trott Dec 26, 2018
7bb8b47
test: wrap common.requireFlags() with common.exposeInternals()
Trott Dec 27, 2018
ec18dcd
test: remove common.exposeInternals()
Trott Dec 27, 2018
3c8c66e
test: remove common.experimentalWorker()
Trott Dec 27, 2018
f9a3f5d
test: remove common.relaunchWithFlags()
Trott Dec 27, 2018
d4eb09d
test: remove unnecessary logic around requireFlags() calls
Trott Dec 27, 2018
cec4d0e
test: revise signature for common.requireFlags()
Trott Dec 28, 2018
7b50eb6
fixup on experimental worker flag
Trott Jan 4, 2019
e716c0a
test: improve worker_threads handling in common.requireFlags()
Trott Dec 28, 2018
539d365
test: check for errors after in requireFlags()
Trott Dec 30, 2018
ac1913b
test: refactor test-inspector-overwrite-config.js
Trott Dec 30, 2018
bdcbdae
test: short-circuit requireFlags() when cluster worker
Trott Dec 31, 2018
146681b
test: simplify flags logic for esm tests
Trott Jan 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 4 additions & 8 deletions doc/guides/writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,13 @@ fs.readFile('test-file').then(
### Flags

Some tests will require running Node.js with specific command line flags set. To
accomplish this, add a `// Flags: ` comment in the preamble of the
test followed by the flags. For example, to allow a test to require some of the
`internal/*` modules, add the `--expose-internals` flag.
A test that would require `internal/freelist` could start like this:
accomplish this, use [`common.requireFlags()`][].

```javascript
'use strict';

// Flags: --expose-internals

require('../common');
const assert = require('assert');
const common = require('../common');
common.requireFlags('--expose-internals');
const freelist = require('internal/freelist');
```

Expand Down Expand Up @@ -399,6 +394,7 @@ To generate a test coverage report, see the
[ASCII]: http://man7.org/linux/man-pages/man7/ascii.7.html
[Google Test]: https://github.com/google/googletest
[`common` module]: https://github.com/nodejs/node/blob/master/test/common/README.md
[`common.requireFlags()`]: https://github.com/nodejs/node/blob/master/test/common/README.md#requireflagsflags
[all maintained branches]: https://github.com/nodejs/lts
[node.green]: http://node.green/
[test fixture]: https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests
Expand Down
3 changes: 2 additions & 1 deletion test/abort/test-addon-uv-handle-leak.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Flags: --experimental-worker
'use strict';
const common = require('../common');
common.requireFlags('--experimental-worker');

const assert = require('assert');
const fs = require('fs');
const path = require('path');
Expand Down
2 changes: 1 addition & 1 deletion test/addons/buffer-free-callback/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// Flags: --expose-gc

const common = require('../../common');
common.requireFlags('--expose-gc');
const binding = require(`./build/${common.buildType}/binding`);

function check(size, alignment, offset) {
Expand Down
2 changes: 1 addition & 1 deletion test/addons/hello-world/test-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Flags: --experimental-worker
'use strict';
const common = require('../../common');
common.requireFlags('--experimental-worker');
const assert = require('assert');
const path = require('path');
const { Worker } = require('worker_threads');
Expand Down
2 changes: 1 addition & 1 deletion test/addons/null-buffer-neuter/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// Flags: --expose-gc

const common = require('../../common');
common.requireFlags('--expose-gc');
const binding = require(`./build/${common.buildType}/binding`);

binding.run();
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Flags: --expose-gc
'use strict';

const common = require('../../common');
const skipMessage = 'intensive toString tests due to memory confinements';
if (!common.enoughTestMem)
common.skip(skipMessage);

common.requireFlags('--expose-gc');

const binding = require(`./build/${common.buildType}/binding`);
const assert = require('assert');

Expand Down
2 changes: 1 addition & 1 deletion test/addons/worker-addon/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Flags: --experimental-worker
'use strict';
const common = require('../../common');
common.requireFlags('--experimental-worker');
const assert = require('assert');
const child_process = require('child_process');
const path = require('path');
Expand Down
1 change: 0 additions & 1 deletion test/async-hooks/init-hooks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
// Flags: --expose-gc

const common = require('../common');
const assert = require('assert');
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-emit-before-after.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
common.requireFlags('--expose-internals');
const assert = require('assert');
const spawnSync = require('child_process').spawnSync;
const async_hooks = require('internal/async_hooks');
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-emit-init.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
common.requireFlags('--expose-internals');
const assert = require('assert');
const spawnSync = require('child_process').spawnSync;
const async_hooks = require('internal/async_hooks');
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-httpparser.request.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
common.requireFlags('--expose-internals');
const assert = require('assert');
const tick = require('../common/tick');
const initHooks = require('./init-hooks');
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-httpparser.response.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
common.requireFlags('--expose-internals');
const assert = require('assert');
const tick = require('../common/tick');
const initHooks = require('./init-hooks');
Expand Down
3 changes: 2 additions & 1 deletion test/async-hooks/test-no-assert-when-disabled.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';
// Flags: --no-force-async-hooks-checks --expose-internals
const common = require('../common');

if (!common.isMainThread)
common.skip('Workers don\'t inherit per-env state like the check flag');

common.requireFlags('--expose-internals', '--no-force-async-hooks-checks');

const async_hooks = require('internal/async_hooks');

// Negative asyncIds and invalid type name
Expand Down
7 changes: 3 additions & 4 deletions test/async-hooks/test-pipewrap.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// NOTE: this also covers process wrap as one is created along with the pipes
// when we launch the sleep process
'use strict';
// Flags: --expose-gc

const common = require('../common');
if (!common.isMainThread)
common.skip('Worker bootstrapping works differently -> different async IDs');
common.requireFlags('--expose-gc');
const assert = require('assert');
const tick = require('../common/tick');
const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');
const { spawn } = require('child_process');

if (!common.isMainThread)
common.skip('Worker bootstrapping works differently -> different async IDs');

const hooks = initHooks();

hooks.enable();
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-querywrap.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
// Flags: --expose-gc

const common = require('../common');
common.requireFlags('--expose-gc');
const assert = require('assert');
const tick = require('../common/tick');
const initHooks = require('./init-hooks');
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-zlib.zlib-binding.deflate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Flags: --expose-internals
'use strict';

const common = require('../common');
common.requireFlags('--expose-internals');
const assert = require('assert');
const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');
Expand Down
4 changes: 2 additions & 2 deletions test/code-cache/test-code-cache.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

// Flags: --expose-internals
// This test verifies that if the binary is compiled with code cache,
// and the cache is used when built in modules are compiled.
// Otherwise, verifies that no cache is used when compiling builtins.

require('../common');
const common = require('../common');
common.requireFlags('--expose-internals');
const assert = require('assert');
const {
cachableBuiltins,
Expand Down
11 changes: 11 additions & 0 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ const { spawn } = require('child_process');
spawn(...common.pwdCommand, { stdio: ['pipe'] });
```

### requireFlags(...flags)
* `...flags` [<string>]

Checks that the provided command-line flags have been provided. If not, it
relaunches the test adding the missing flags.

```js
const common = require('../common');
common.requireFlags('--preserve-symlinks', '--expose-internals');
```

### rootDir
* [<string>]

Expand Down
62 changes: 21 additions & 41 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,47 +46,6 @@ const isMainThread = (() => {
return true;
})();

// Check for flags. Skip this for workers (both, the `cluster` module and
// `worker_threads`) and child processes.
if (process.argv.length === 2 &&
isMainThread &&
module.parent &&
require('cluster').isMaster) {
// The copyright notice is relatively big and the flags could come afterwards.
const bytesToRead = 1500;
const buffer = Buffer.allocUnsafe(bytesToRead);
const fd = fs.openSync(module.parent.filename, 'r');
fs.readSync(fd, buffer, 0, bytesToRead);
fs.closeSync(fd);
const source = buffer.toString();

const flagStart = source.indexOf('// Flags: --') + 10;
if (flagStart !== 9) {
let flagEnd = source.indexOf('\n', flagStart);
// Normalize different EOL.
if (source[flagEnd - 1] === '\r') {
flagEnd--;
}
const flags = source
.substring(flagStart, flagEnd)
.replace(/_/g, '-')
.split(' ');
const args = process.execArgv.map((arg) => arg.replace(/_/g, '-'));
for (const flag of flags) {
if (!args.includes(flag) &&
// If the binary was built without-ssl then the crypto flags are
// invalid (bad option). The test itself should handle this case.
hasCrypto &&
// If the binary is build without `intl` the inspect option is
// invalid. The test itself should handle this case.
(process.config.variables.v8_enable_inspector !== 0 ||
!flag.startsWith('--inspect'))) {
throw new Error(`Test has to be started with the flag: '${flag}'`);
}
}
}
}

const isWindows = process.platform === 'win32';
const isAIX = process.platform === 'aix';
const isLinuxPPCBE = (process.platform === 'linux') &&
Expand Down Expand Up @@ -378,6 +337,7 @@ function _mustCallInner(fn, criteria = 1, field) {
}

function hasMultiLocalhost() {
requireFlags('--expose-internals');
const { internalBinding } = require('internal/test/binding');
const { TCP, constants: TCPConstants } = internalBinding('tcp_wrap');
const t = new TCP(TCPConstants.SOCKET);
Expand Down Expand Up @@ -722,6 +682,25 @@ function runWithInvalidFD(func) {
printSkipMessage('Could not generate an invalid fd');
}

function requireFlags(...flags) {
if (require('cluster').isWorker)
return;
let missing = flags.filter((flag) => !process.execArgv.includes(flag));
Trott marked this conversation as resolved.
Show resolved Hide resolved

// Special handling for worker_threads.
if (!isMainThread) {
missing = missing.filter((flag) => !/--experimental[-_]worker/.test(flag));
}

if (missing.length > 0) {
const args = [ ...missing, ...process.execArgv, ...process.argv.slice(1) ];
const options = { encoding: 'utf8', stdio: 'inherit' };
const result = spawnSync(process.execPath, args, options);
assert.ifError(result.error);
process.exit(result.status);
Trott marked this conversation as resolved.
Show resolved Hide resolved
}
}

module.exports = {
allowGlobals,
buildType,
Expand Down Expand Up @@ -763,6 +742,7 @@ module.exports = {
platformTimeout,
printSkipMessage,
pwdCommand,
requireFlags,
rootDir,
runWithInvalidFD,
skip,
Expand Down
7 changes: 4 additions & 3 deletions test/common/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Flags: --experimental-modules
/* eslint-disable node-core/required-modules */
import common from './index.js';

Expand Down Expand Up @@ -42,10 +41,11 @@ const {
expectsError,
skipIfInspectorDisabled,
skipIf32Bits,
disableCrashOnUnhandledRejection,
getArrayBufferViews,
getBufferSources,
disableCrashOnUnhandledRejection,
getTTYfd,
requireFlags,
runWithInvalidFD
} = common;

Expand Down Expand Up @@ -89,9 +89,10 @@ export {
expectsError,
skipIfInspectorDisabled,
skipIf32Bits,
disableCrashOnUnhandledRejection,
getArrayBufferViews,
getBufferSources,
disableCrashOnUnhandledRejection,
getTTYfd,
requireFlags,
runWithInvalidFD
};
1 change: 0 additions & 1 deletion test/es-module/test-esm-basic-imports.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Flags: --experimental-modules
import '../common';
import assert from 'assert';
import ok from '../fixtures/es-modules/test-esm-ok.mjs';
Expand Down
1 change: 0 additions & 1 deletion test/es-module/test-esm-cyclic-dynamic-import.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
// Flags: --experimental-modules
import '../common';
import('./test-esm-cyclic-dynamic-import');
1 change: 0 additions & 1 deletion test/es-module/test-esm-double-encoding.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Flags: --experimental-modules
import '../common';

// Assert we can import files with `%` in their pathname.
Expand Down
1 change: 0 additions & 1 deletion test/es-module/test-esm-dynamic-import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Flags: --experimental-modules
'use strict';
const common = require('../common');
const assert = require('assert');
Expand Down
1 change: 0 additions & 1 deletion test/es-module/test-esm-encoded-path.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Flags: --experimental-modules
import '../common';
import assert from 'assert';
// ./test-esm-ok.mjs
Expand Down
2 changes: 0 additions & 2 deletions test/es-module/test-esm-error-cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

// Flags: --experimental-modules

require('../common');
const assert = require('assert');

Expand Down
18 changes: 14 additions & 4 deletions test/es-module/test-esm-example-loader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/example-loader.mjs
/* eslint-disable node-core/required-modules */
import assert from 'assert';
import ok from '../fixtures/es-modules/test-esm-ok.mjs';
if (typeof require === 'function') {
const common = require('../common');
common.requireFlags(
'--experimental-modules',
'--loader=./test/fixtures/es-module-loaders/example-loader.mjs'
);
} else {
async function test() {
const { default: assert } = await import('assert');
const ok = await import('../fixtures/es-modules/test-esm-ok.mjs');
assert(ok);
}

assert(ok);
test().catch((e) => { console.error(e); process.exit(1); });
}
1 change: 0 additions & 1 deletion test/es-module/test-esm-forbidden-globals.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Flags: --experimental-modules
import '../common';

// eslint-disable-next-line no-undef
Expand Down
2 changes: 0 additions & 2 deletions test/es-module/test-esm-import-meta.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Flags: --experimental-modules

import '../common';
import assert from 'assert';

Expand Down
Loading