mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
test: add tests for 3 methods in utils
Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com> PR-URL: https://github.com/nodejs/node/pull/63765 Refs: https://app.codecov.io/gh/nodejs/node/blob/main/lib%2Futil.js Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
This commit is contained in:
1 parent
2fb168f756
commit
3501b3e446
4 files changed
+117
No files matched your search
@@ -0,0 +1,2 @@
|
||||
const{getCallSites}=require('node:util');process.stdout.write(JSON.stringify(getCallSites({sourceMap:true})[0]));
|
||||
//# sourceMappingURL=get-call-sites-mapped.map
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"version": 3,
|
||||
"sources": ["get-call-sites-original.js"],
|
||||
"names": [],
|
||||
"mappings": "AAUA"
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('node:assert');
|
||||
const os = require('node:os');
|
||||
const util = require('node:util');
|
||||
|
||||
const ENOENT = -os.constants.errno.ENOENT;
|
||||
const EACCES = -os.constants.errno.EACCES;
|
||||
|
||||
// Address and port are included in the message
|
||||
{
|
||||
const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 8080);
|
||||
assert.ok(e instanceof Error);
|
||||
assert.strictEqual(e.errno, ENOENT);
|
||||
assert.strictEqual(e.code, util.getSystemErrorName(ENOENT));
|
||||
assert.strictEqual(e.syscall, 'connect');
|
||||
assert.strictEqual(e.address, '127.0.0.1');
|
||||
assert.strictEqual(e.port, 8080);
|
||||
assert.ok(e.message.includes('127.0.0.1:8080'), `expected 127.0.0.1:8080 in "${e.message}"`);
|
||||
}
|
||||
|
||||
// Port=0 omits host:port detail; port property is not set
|
||||
{
|
||||
const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 0);
|
||||
assert.strictEqual(e.address, '127.0.0.1');
|
||||
assert.strictEqual(e.port, undefined);
|
||||
assert.ok(e.message.includes('127.0.0.1'), `expected address in "${e.message}"`);
|
||||
assert.ok(!e.message.includes(':0'), `unexpected :0 in "${e.message}"`);
|
||||
}
|
||||
|
||||
// Additional is appended as "- Local (...)"
|
||||
{
|
||||
const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 80, '0.0.0.0:12345');
|
||||
assert.ok(e.message.includes('- Local (0.0.0.0:12345)'), `expected local info in "${e.message}"`);
|
||||
}
|
||||
|
||||
// No address or port
|
||||
{
|
||||
const e = util._exceptionWithHostPort(ENOENT, 'connect', null, 0);
|
||||
assert.strictEqual(e.address, null);
|
||||
assert.strictEqual(e.port, undefined);
|
||||
assert.ok(!e.message.includes('null'), `unexpected null in "${e.message}"`);
|
||||
}
|
||||
|
||||
// Different syscall and error code
|
||||
{
|
||||
const e = util._exceptionWithHostPort(EACCES, 'bind', '0.0.0.0', 443);
|
||||
assert.strictEqual(e.code, util.getSystemErrorName(EACCES));
|
||||
assert.strictEqual(e.syscall, 'bind');
|
||||
}
|
||||
|
||||
// Stack trace should not include _exceptionWithHostPort itself
|
||||
{
|
||||
const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 80);
|
||||
assert.ok(!e.stack.includes('_exceptionWithHostPort'), 'stack must not mention the wrapper');
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('node:assert');
|
||||
const { spawnSync } = require('node:child_process');
|
||||
const { getCallSites } = require('node:util');
|
||||
const fixtures = require('../common/fixtures');
|
||||
|
||||
// mapCallSite falls back to the original call site when no source map is
|
||||
// registered for the current file.
|
||||
{
|
||||
const withoutMap = getCallSites(1);
|
||||
const withMap = getCallSites(1, { sourceMap: true });
|
||||
// scriptName must be identical — no source map registered for this test file
|
||||
assert.strictEqual(withMap[0].scriptName, withoutMap[0].scriptName);
|
||||
// Both calls report the actual line in this file (they differ by one because
|
||||
// they are on consecutive source lines, which is expected).
|
||||
assert.ok(typeof withMap[0].lineNumber === 'number');
|
||||
assert.ok(typeof withMap[0].columnNumber === 'number');
|
||||
}
|
||||
|
||||
// reconstructCallSite remaps call sites to the original source when
|
||||
// --enable-source-maps is active and a source map is found.
|
||||
{
|
||||
const file = fixtures.path('source-map', 'get-call-sites-mapped.js');
|
||||
const { status, stderr, stdout } = spawnSync(
|
||||
process.execPath,
|
||||
['--enable-source-maps', file],
|
||||
);
|
||||
assert.strictEqual(status, 0, stderr.toString());
|
||||
const callSite = JSON.parse(stdout.toString());
|
||||
// scriptName should point to the original (unmapped) source file
|
||||
assert.ok(
|
||||
callSite.scriptName.endsWith('get-call-sites-original.js'),
|
||||
`expected original source in scriptName, got "${callSite.scriptName}"`,
|
||||
);
|
||||
// lineNumber and columnNumber should reflect the original source position
|
||||
assert.strictEqual(callSite.lineNumber, 11);
|
||||
assert.strictEqual(callSite.columnNumber, 1);
|
||||
}
|
||||
|
||||
// Without --enable-source-maps the generated file path is preserved.
|
||||
{
|
||||
const file = fixtures.path('source-map', 'get-call-sites-mapped.js');
|
||||
const { status, stderr, stdout } = spawnSync(process.execPath, [file]);
|
||||
assert.strictEqual(status, 0, stderr.toString());
|
||||
const callSite = JSON.parse(stdout.toString());
|
||||
assert.ok(
|
||||
callSite.scriptName.endsWith('get-call-sites-mapped.js'),
|
||||
`expected generated file in scriptName, got "${callSite.scriptName}"`,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user