Skip to content
Permalink
Browse files

test: cleanup test-os.js

Moved from var to const.
Moved from .equal to .strictEqual.
Added more checks about the type of the returned values.

PR-URL: #8606
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
  • Loading branch information...
delvedor authored and Fishrock123 committed Sep 17, 2016
1 parent 7a59449 commit edf9242f56218d35cb1ef50f23ee0c67e1f8f041
Showing with 54 additions and 36 deletions.
  1. +54 −36 test/parallel/test-os.js
@@ -1,74 +1,91 @@
'use strict';
var common = require('../common');
var assert = require('assert');
var os = require('os');
var path = require('path');

const common = require('../common');
const assert = require('assert');
const os = require('os');
const path = require('path');

const is = {
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);
}
};

process.env.TMPDIR = '/tmpdir';
process.env.TMP = '/tmp';
process.env.TEMP = '/temp';
if (common.isWindows) {
assert.equal(os.tmpdir(), '/temp');
assert.strictEqual(os.tmpdir(), '/temp');
process.env.TEMP = '';
assert.equal(os.tmpdir(), '/tmp');
assert.strictEqual(os.tmpdir(), '/tmp');
process.env.TMP = '';
const expected = (process.env.SystemRoot || process.env.windir) + '\\temp';
assert.equal(os.tmpdir(), expected);
assert.strictEqual(os.tmpdir(), expected);
process.env.TEMP = '\\temp\\';
assert.equal(os.tmpdir(), '\\temp');
assert.strictEqual(os.tmpdir(), '\\temp');
process.env.TEMP = '\\tmpdir/';
assert.equal(os.tmpdir(), '\\tmpdir/');
assert.strictEqual(os.tmpdir(), '\\tmpdir/');
process.env.TEMP = '\\';
assert.equal(os.tmpdir(), '\\');
assert.strictEqual(os.tmpdir(), '\\');
process.env.TEMP = 'C:\\';
assert.equal(os.tmpdir(), 'C:\\');
assert.strictEqual(os.tmpdir(), 'C:\\');
} else {
assert.equal(os.tmpdir(), '/tmpdir');
assert.strictEqual(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '';
assert.equal(os.tmpdir(), '/tmp');
assert.strictEqual(os.tmpdir(), '/tmp');
process.env.TMP = '';
assert.equal(os.tmpdir(), '/temp');
assert.strictEqual(os.tmpdir(), '/temp');
process.env.TEMP = '';
assert.equal(os.tmpdir(), '/tmp');
assert.strictEqual(os.tmpdir(), '/tmp');
process.env.TMPDIR = '/tmpdir/';
assert.equal(os.tmpdir(), '/tmpdir');
assert.strictEqual(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '/tmpdir\\';
assert.equal(os.tmpdir(), '/tmpdir\\');
assert.strictEqual(os.tmpdir(), '/tmpdir\\');
process.env.TMPDIR = '/';
assert.equal(os.tmpdir(), '/');
assert.strictEqual(os.tmpdir(), '/');
}

var endianness = os.endianness();
const endianness = os.endianness();
console.log('endianness = %s', endianness);
is.string(endianness);
assert.ok(/[BL]E/.test(endianness));

var hostname = os.hostname();
const hostname = os.hostname();
console.log('hostname = %s', hostname);
is.string(hostname);
assert.ok(hostname.length > 0);

var uptime = os.uptime();
const uptime = os.uptime();
console.log('uptime = %d', uptime);
is.number(uptime);
assert.ok(uptime > 0);

var cpus = os.cpus();
const cpus = os.cpus();
console.log('cpus = ', cpus);
is.array(cpus);
assert.ok(cpus.length > 0);

var type = os.type();
const type = os.type();
console.log('type = ', type);
is.string(type);
assert.ok(type.length > 0);

var release = os.release();
const release = os.release();
console.log('release = ', release);
is.string(release);
assert.ok(release.length > 0);

var platform = os.platform();
const platform = os.platform();
console.log('platform = ', platform);
is.string(platform);
assert.ok(platform.length > 0);

var arch = os.arch();
const arch = os.arch();
console.log('arch = ', arch);
is.string(arch);
assert.ok(arch.length > 0);

if (!common.isSunOS) {
@@ -79,7 +96,7 @@ if (!common.isSunOS) {
}


var interfaces = os.networkInterfaces();
const interfaces = os.networkInterfaces();
console.error(interfaces);
switch (platform) {
case 'linux':
@@ -104,29 +121,30 @@ switch (platform) {
}
}

var EOL = os.EOL;
const EOL = os.EOL;
assert.ok(EOL.length > 0);


var home = os.homedir();
const home = os.homedir();

console.log('homedir = ' + home);
assert.ok(typeof home === 'string');
is.string(home);
assert.ok(home.indexOf(path.sep) !== -1);

if (common.isWindows && process.env.USERPROFILE) {
assert.equal(home, process.env.USERPROFILE);
assert.strictEqual(home, process.env.USERPROFILE);
delete process.env.USERPROFILE;
assert.ok(os.homedir().indexOf(path.sep) !== -1);
process.env.USERPROFILE = home;
} else if (!common.isWindows && process.env.HOME) {
assert.equal(home, process.env.HOME);
assert.strictEqual(home, process.env.HOME);
delete process.env.HOME;
assert.ok(os.homedir().indexOf(path.sep) !== -1);
process.env.HOME = home;
}

const pwd = os.userInfo();
is.object(pwd);
const pwdBuf = os.userInfo({ encoding: 'buffer' });

if (common.isWindows) {
@@ -137,15 +155,15 @@ if (common.isWindows) {
assert.strictEqual(pwdBuf.gid, -1);
assert.strictEqual(pwdBuf.shell, null);
} else {
assert.strictEqual(typeof pwd.uid, 'number');
assert.strictEqual(typeof pwd.gid, 'number');
is.number(pwd.uid);
is.number(pwd.gid);
assert.notStrictEqual(pwd.shell.indexOf(path.sep), -1);
assert.strictEqual(pwd.uid, pwdBuf.uid);
assert.strictEqual(pwd.gid, pwdBuf.gid);
assert.strictEqual(pwd.shell, pwdBuf.shell.toString('utf8'));
}

assert.strictEqual(typeof pwd.username, 'string');
is.string(pwd.username);
assert.notStrictEqual(pwd.homedir.indexOf(path.sep), -1);
assert.strictEqual(pwd.username, pwdBuf.username.toString('utf8'));
assert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8'));

0 comments on commit edf9242

Please sign in to comment.
You can’t perform that action at this time.