Skip to content

Commit 1582260

Browse files
viktor-kuMylesBorins
authored andcommittedOct 11, 2017
test: increase test coverage for os.js
PR-URL: #14098 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent dcad2df commit 1582260

File tree

1 file changed

+56
-32
lines changed

1 file changed

+56
-32
lines changed
 

‎test/parallel/test-os.js

+56-32
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,22 @@ const path = require('path');
2727
const { inspect } = require('util');
2828

2929
const is = {
30+
number: (value, key) => {
31+
assert(!isNaN(value), `${key} should not be NaN`);
32+
assert.strictEqual(typeof value, 'number');
33+
},
3034
string: (value) => { assert.strictEqual(typeof value, 'string'); },
31-
number: (value) => { assert.strictEqual(typeof value, 'number'); },
3235
array: (value) => { assert.ok(Array.isArray(value)); },
3336
object: (value) => {
3437
assert.strictEqual(typeof value, 'object');
3538
assert.notStrictEqual(value, null);
3639
}
3740
};
3841

42+
const flatten = (arr) =>
43+
arr.reduce((acc, c) =>
44+
acc.concat(Array.isArray(c) ? flatten(c) : c), []);
45+
3946
process.env.TMPDIR = '/tmpdir';
4047
process.env.TMP = '/tmp';
4148
process.env.TEMP = '/temp';
@@ -112,43 +119,49 @@ if (!common.isSunOS) {
112119
assert.ok(os.totalmem() > 0);
113120
}
114121

115-
116122
const interfaces = os.networkInterfaces();
117123
switch (platform) {
118-
case 'linux':
119-
{
120-
const filter =
121-
(e) => e.address === '127.0.0.1' && e.netmask === '255.0.0.0';
124+
case 'linux': {
125+
const filter = (e) =>
126+
e.address === '127.0.0.1' &&
127+
e.netmask === '255.0.0.0';
128+
122129
const actual = interfaces.lo.filter(filter);
123-
const expected = [{ address: '127.0.0.1', netmask: '255.0.0.0',
124-
mac: '00:00:00:00:00:00', family: 'IPv4',
125-
internal: true, cidr: '127.0.0.1/8' }];
130+
const expected = [{
131+
address: '127.0.0.1',
132+
netmask: '255.0.0.0',
133+
mac: '00:00:00:00:00:00',
134+
family: 'IPv4',
135+
internal: true,
136+
cidr: '127.0.0.1/8'
137+
}];
126138
assert.deepStrictEqual(actual, expected);
127139
break;
128140
}
129-
case 'win32':
130-
{
131-
const filter = (e) => e.address === '127.0.0.1';
141+
case 'win32': {
142+
const filter = (e) =>
143+
e.address === '127.0.0.1';
144+
132145
const actual = interfaces['Loopback Pseudo-Interface 1'].filter(filter);
133-
const expected = [{ address: '127.0.0.1', netmask: '255.0.0.0',
134-
mac: '00:00:00:00:00:00', family: 'IPv4',
135-
internal: true, cidr: '127.0.0.1/8' }];
146+
const expected = [{
147+
address: '127.0.0.1',
148+
netmask: '255.0.0.0',
149+
mac: '00:00:00:00:00:00',
150+
family: 'IPv4',
151+
internal: true,
152+
cidr: '127.0.0.1/8'
153+
}];
136154
assert.deepStrictEqual(actual, expected);
137155
break;
138156
}
139157
}
140-
function flatten(arr) {
141-
return arr.reduce(
142-
(acc, c) => acc.concat(Array.isArray(c) ? flatten(c) : c),
143-
[]
144-
);
145-
}
146158
const netmaskToCIDRSuffixMap = new Map(Object.entries({
147159
'255.0.0.0': 8,
148160
'255.255.255.0': 24,
149161
'ffff:ffff:ffff:ffff::': 64,
150162
'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff': 128
151163
}));
164+
152165
flatten(Object.values(interfaces))
153166
.map((v) => ({ v, mask: netmaskToCIDRSuffixMap.get(v.netmask) }))
154167
.forEach(({ v, mask }) => {
@@ -159,11 +172,13 @@ flatten(Object.values(interfaces))
159172
});
160173

161174
const EOL = os.EOL;
162-
assert.ok(EOL.length > 0);
163-
175+
if (common.isWindows) {
176+
assert.strictEqual(EOL, '\r\n');
177+
} else {
178+
assert.strictEqual(EOL, '\n');
179+
}
164180

165181
const home = os.homedir();
166-
167182
is.string(home);
168183
assert.ok(home.includes(path.sep));
169184

@@ -204,11 +219,20 @@ assert.ok(pwd.homedir.includes(path.sep));
204219
assert.strictEqual(pwd.username, pwdBuf.username.toString('utf8'));
205220
assert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8'));
206221

207-
// Test that the Symbol.toPrimitive functions work correctly
208-
[
209-
[`${os.hostname}`, os.hostname()],
210-
[`${os.homedir}`, os.homedir()],
211-
[`${os.release}`, os.release()],
212-
[`${os.type}`, os.type()],
213-
[`${os.endianness}`, os.endianness()]
214-
].forEach((set) => assert.strictEqual(set[0], set[1]));
222+
assert.strictEqual(`${os.hostname}`, os.hostname());
223+
assert.strictEqual(`${os.homedir}`, os.homedir());
224+
assert.strictEqual(`${os.release}`, os.release());
225+
assert.strictEqual(`${os.type}`, os.type());
226+
assert.strictEqual(`${os.endianness}`, os.endianness());
227+
assert.strictEqual(`${os.tmpdir}`, os.tmpdir());
228+
assert.strictEqual(`${os.arch}`, os.arch());
229+
assert.strictEqual(`${os.platform}`, os.platform());
230+
231+
assert.strictEqual(+os.totalmem, os.totalmem());
232+
233+
// Assert that the following values are coercible to numbers.
234+
is.number(+os.uptime, 'uptime');
235+
is.number(os.uptime(), 'uptime');
236+
237+
is.number(+os.freemem, 'freemem');
238+
is.number(os.freemem(), 'freemem');

0 commit comments

Comments
 (0)
Please sign in to comment.