test: add tests for 3 methods in utils · nodejs/node@562b831 · GitHub
Skip to content

Commit 562b831

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 db6a31d commit 562b831

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
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+
const assert = require('node:assert');
5+
const os = require('node:os');
6+
const util = require('node:util');
7+
8+
const ENOENT = -os.constants.errno.ENOENT;
9+
const EACCES = -os.constants.errno.EACCES;
10+
11+
// Address and port are included in the message
12+
{
13+
const e = util._exceptionWithHostPort(ENOENT, 'connect', '127.0.0.1', 8080);
14+
assert.ok(e instanceof Error);
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+
const e = 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+
const e = 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+
const e = 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+
const e = 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+
const e = 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

0 commit comments

Comments
 (0)