Skip to content

Commit d2c9c07

Browse files
watildeaduh95
authored andcommitted
test: add tests for 3 methods in utils
Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com> PR-URL: #63765 Refs: https://app.codecov.io/gh/nodejs/node/blob/main/lib%2Futil.js Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
1 parent e5289d1 commit d2c9c07

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

‎test/fixtures/source-map/get-call-sites-mapped.js‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"version": 3,
3+
"sources": ["get-call-sites-original.js"],
4+
"names": [],
5+
"mappings": "AAUA"
6+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
require('../common');
4+
constassert=require('node:assert');
5+
constos=require('node:os');
6+
constutil=require('node:util');
7+
8+
constENOENT=-os.constants.errno.ENOENT;
9+
constEACCES=-os.constants.errno.EACCES;
10+
11+
// Address and port are included in the message
12+
{
13+
conste=util._exceptionWithHostPort(ENOENT,'connect','127.0.0.1',8080);
14+
assert.ok(einstanceofError);
15+
assert.strictEqual(e.errno,ENOENT);
16+
assert.strictEqual(e.code,util.getSystemErrorName(ENOENT));
17+
assert.strictEqual(e.syscall,'connect');
18+
assert.strictEqual(e.address,'127.0.0.1');
19+
assert.strictEqual(e.port,8080);
20+
assert.ok(e.message.includes('127.0.0.1:8080'),`expected 127.0.0.1:8080 in "${e.message}"`);
21+
}
22+
23+
// Port=0 omits host:port detail; port property is not set
24+
{
25+
conste=util._exceptionWithHostPort(ENOENT,'connect','127.0.0.1',0);
26+
assert.strictEqual(e.address,'127.0.0.1');
27+
assert.strictEqual(e.port,undefined);
28+
assert.ok(e.message.includes('127.0.0.1'),`expected address in "${e.message}"`);
29+
assert.ok(!e.message.includes(':0'),`unexpected :0 in "${e.message}"`);
30+
}
31+
32+
// Additional is appended as "- Local (...)"
33+
{
34+
conste=util._exceptionWithHostPort(ENOENT,'connect','127.0.0.1',80,'0.0.0.0:12345');
35+
assert.ok(e.message.includes('- Local (0.0.0.0:12345)'),`expected local info in "${e.message}"`);
36+
}
37+
38+
// No address or port
39+
{
40+
conste=util._exceptionWithHostPort(ENOENT,'connect',null,0);
41+
assert.strictEqual(e.address,null);
42+
assert.strictEqual(e.port,undefined);
43+
assert.ok(!e.message.includes('null'),`unexpected null in "${e.message}"`);
44+
}
45+
46+
// Different syscall and error code
47+
{
48+
conste=util._exceptionWithHostPort(EACCES,'bind','0.0.0.0',443);
49+
assert.strictEqual(e.code,util.getSystemErrorName(EACCES));
50+
assert.strictEqual(e.syscall,'bind');
51+
}
52+
53+
// Stack trace should not include _exceptionWithHostPort itself
54+
{
55+
conste=util._exceptionWithHostPort(ENOENT,'connect','127.0.0.1',80);
56+
assert.ok(!e.stack.includes('_exceptionWithHostPort'),'stack must not mention the wrapper');
57+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
require('../common');
4+
constassert=require('node:assert');
5+
const{ spawnSync }=require('node:child_process');
6+
const{ getCallSites }=require('node:util');
7+
constfixtures=require('../common/fixtures');
8+
9+
// mapCallSite falls back to the original call site when no source map is
10+
// registered for the current file.
11+
{
12+
constwithoutMap=getCallSites(1);
13+
constwithMap=getCallSites(1,{sourceMap: true});
14+
// scriptName must be identical — no source map registered for this test file
15+
assert.strictEqual(withMap[0].scriptName,withoutMap[0].scriptName);
16+
// Both calls report the actual line in this file (they differ by one because
17+
// they are on consecutive source lines, which is expected).
18+
assert.ok(typeofwithMap[0].lineNumber==='number');
19+
assert.ok(typeofwithMap[0].columnNumber==='number');
20+
}
21+
22+
// reconstructCallSite remaps call sites to the original source when
23+
// --enable-source-maps is active and a source map is found.
24+
{
25+
constfile=fixtures.path('source-map','get-call-sites-mapped.js');
26+
const{ status, stderr, stdout }=spawnSync(
27+
process.execPath,
28+
['--enable-source-maps',file],
29+
);
30+
assert.strictEqual(status,0,stderr.toString());
31+
constcallSite=JSON.parse(stdout.toString());
32+
// scriptName should point to the original (unmapped) source file
33+
assert.ok(
34+
callSite.scriptName.endsWith('get-call-sites-original.js'),
35+
`expected original source in scriptName, got "${callSite.scriptName}"`,
36+
);
37+
// lineNumber and columnNumber should reflect the original source position
38+
assert.strictEqual(callSite.lineNumber,11);
39+
assert.strictEqual(callSite.columnNumber,1);
40+
}
41+
42+
// Without --enable-source-maps the generated file path is preserved.
43+
{
44+
constfile=fixtures.path('source-map','get-call-sites-mapped.js');
45+
const{ status, stderr, stdout }=spawnSync(process.execPath,[file]);
46+
assert.strictEqual(status,0,stderr.toString());
47+
constcallSite=JSON.parse(stdout.toString());
48+
assert.ok(
49+
callSite.scriptName.endsWith('get-call-sites-mapped.js'),
50+
`expected generated file in scriptName, got "${callSite.scriptName}"`,
51+
);
52+
}

0 commit comments

Comments
 (0)