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: increase test coverage for os.js #14098

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion test/parallel/parallel.status
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ test-npm-install: PASS,FLAKY
[$system==solaris] # Also applies to SmartOS

[$system==freebsd]

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor

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...)

91 changes: 58 additions & 33 deletions test/parallel/test-os.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
}
Copy link
Member

Choose a reason for hiding this comment

The 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);
Expand All @@ -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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add a default:

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 }) => {
Expand All @@ -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));

Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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');