-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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: increase test coverage for os.js #14098
Changes from all commits
bf4b193
3233b4d
4c6aaab
fdd2780
c923d4e
eced626
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,3 @@ test-npm-install: PASS,FLAKY | |
[$system==solaris] # Also applies to SmartOS | ||
|
||
[$system==freebsd] | ||
|
||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,15 +27,22 @@ const path = require('path'); | |
const { inspect } = require('util'); | ||
|
||
const is = { | ||
number: (value, key) => { | ||
assert(!isNaN(value), `${key} should not be NaN`); | ||
assert.strictEqual(typeof value, 'number'); | ||
}, | ||
string: (value) => { assert.strictEqual(typeof value, 'string'); }, | ||
number: (value) => { assert.strictEqual(typeof value, 'number'); }, | ||
array: (value) => { assert.ok(Array.isArray(value)); }, | ||
object: (value) => { | ||
assert.strictEqual(typeof value, 'object'); | ||
assert.notStrictEqual(value, null); | ||
} | ||
}; | ||
|
||
const flatten = (arr) => | ||
arr.reduce((acc, c) => | ||
acc.concat(Array.isArray(c) ? flatten(c) : c), []); | ||
|
||
process.env.TMPDIR = '/tmpdir'; | ||
process.env.TMP = '/tmp'; | ||
process.env.TEMP = '/temp'; | ||
|
@@ -94,8 +101,9 @@ const release = os.release(); | |
is.string(release); | ||
assert.ok(release.length > 0); | ||
//TODO: Check format on more than just AIX | ||
if (common.isAIX) | ||
if (common.isAIX) { | ||
assert.ok(/^\d+\.\d+$/.test(release)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a unrelated change and should be reverted. |
||
|
||
const platform = os.platform(); | ||
is.string(platform); | ||
|
@@ -112,43 +120,49 @@ if (!common.isSunOS) { | |
assert.ok(os.totalmem() > 0); | ||
} | ||
|
||
|
||
const interfaces = os.networkInterfaces(); | ||
switch (platform) { | ||
case 'linux': | ||
{ | ||
const filter = | ||
(e) => e.address === '127.0.0.1' && e.netmask === '255.0.0.0'; | ||
case 'linux': { | ||
const filter = (e) => | ||
e.address === '127.0.0.1' && | ||
e.netmask === '255.0.0.0'; | ||
|
||
const actual = interfaces.lo.filter(filter); | ||
const expected = [{ address: '127.0.0.1', netmask: '255.0.0.0', | ||
mac: '00:00:00:00:00:00', family: 'IPv4', | ||
internal: true, cidr: '127.0.0.1/8' }]; | ||
const expected = [{ | ||
address: '127.0.0.1', | ||
netmask: '255.0.0.0', | ||
mac: '00:00:00:00:00:00', | ||
family: 'IPv4', | ||
internal: true, | ||
cidr: '127.0.0.1/8' | ||
}]; | ||
assert.deepStrictEqual(actual, expected); | ||
break; | ||
} | ||
case 'win32': | ||
{ | ||
const filter = (e) => e.address === '127.0.0.1'; | ||
case 'win32': { | ||
const filter = (e) => | ||
e.address === '127.0.0.1'; | ||
|
||
const actual = interfaces['Loopback Pseudo-Interface 1'].filter(filter); | ||
const expected = [{ address: '127.0.0.1', netmask: '255.0.0.0', | ||
mac: '00:00:00:00:00:00', family: 'IPv4', | ||
internal: true, cidr: '127.0.0.1/8' }]; | ||
const expected = [{ | ||
address: '127.0.0.1', | ||
netmask: '255.0.0.0', | ||
mac: '00:00:00:00:00:00', | ||
family: 'IPv4', | ||
internal: true, | ||
cidr: '127.0.0.1/8' | ||
}]; | ||
assert.deepStrictEqual(actual, expected); | ||
break; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add a default:
common.printSkipMessage(`Skipping assertion of 'os.networkInterfaces()' on ${platform}`);
break; |
||
} | ||
function flatten(arr) { | ||
return arr.reduce( | ||
(acc, c) => acc.concat(Array.isArray(c) ? flatten(c) : c), | ||
[] | ||
); | ||
} | ||
const netmaskToCIDRSuffixMap = new Map(Object.entries({ | ||
'255.0.0.0': 8, | ||
'255.255.255.0': 24, | ||
'ffff:ffff:ffff:ffff::': 64, | ||
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff': 128 | ||
})); | ||
|
||
flatten(Object.values(interfaces)) | ||
.map((v) => ({ v, mask: netmaskToCIDRSuffixMap.get(v.netmask) })) | ||
.forEach(({ v, mask }) => { | ||
|
@@ -159,11 +173,13 @@ flatten(Object.values(interfaces)) | |
}); | ||
|
||
const EOL = os.EOL; | ||
assert.ok(EOL.length > 0); | ||
|
||
if (common.isWindows) { | ||
assert.strictEqual(EOL, '\r\n'); | ||
} else { | ||
assert.strictEqual(EOL, '\n'); | ||
} | ||
|
||
const home = os.homedir(); | ||
|
||
is.string(home); | ||
assert.ok(home.includes(path.sep)); | ||
|
||
|
@@ -204,11 +220,20 @@ assert.ok(pwd.homedir.includes(path.sep)); | |
assert.strictEqual(pwd.username, pwdBuf.username.toString('utf8')); | ||
assert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8')); | ||
|
||
// Test that the Symbol.toPrimitive functions work correctly | ||
[ | ||
[`${os.hostname}`, os.hostname()], | ||
[`${os.homedir}`, os.homedir()], | ||
[`${os.release}`, os.release()], | ||
[`${os.type}`, os.type()], | ||
[`${os.endianness}`, os.endianness()] | ||
].forEach((set) => assert.strictEqual(set[0], set[1])); | ||
assert.strictEqual(`${os.hostname}`, os.hostname()); | ||
assert.strictEqual(`${os.homedir}`, os.homedir()); | ||
assert.strictEqual(`${os.release}`, os.release()); | ||
assert.strictEqual(`${os.type}`, os.type()); | ||
assert.strictEqual(`${os.endianness}`, os.endianness()); | ||
assert.strictEqual(`${os.tmpdir}`, os.tmpdir()); | ||
assert.strictEqual(`${os.arch}`, os.arch()); | ||
assert.strictEqual(`${os.platform}`, os.platform()); | ||
|
||
assert.strictEqual(+os.totalmem, os.totalmem()); | ||
|
||
// At least we can check that these values are numbers | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: change comment to: // Assert that the following values are coercible to numbers. |
||
is.number(+os.uptime, 'uptime'); | ||
is.number(os.uptime(), 'uptime'); | ||
|
||
is.number(+os.freemem, 'freemem'); | ||
is.number(os.freemem(), 'freemem'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change. Please add that back in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's odd because I have this last line in my editor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shows two lines at the end of the file (i.e. the file ends in
\n\n
- one shows up as a new line, and the other as an EOF\n
which is required...)