deno.land / std@0.166.0 / node / _tools / test / parallel / test-os.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286// deno-fmt-ignore-file// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.// Taken from Node 16.13.0// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually
// Copyright Joyent, Inc. and other Node contributors.//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit// persons to whom the Software is furnished to do so, subject to the// following conditions://// The above copyright notice and this permission notice shall be included// in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';const common = require('../common');const assert = require('assert');const os = require('os');const path = require('path');const { inspect } = require('util');
const is = { number: (value, key) => { assert(!Number.isNaN(value), `${key} should not be NaN`); assert.strictEqual(typeof value, 'number'); }, string: (value) => { assert.strictEqual(typeof value, 'string'); }, array: (value) => { assert.ok(Array.isArray(value)); }, object: (value) => { assert.strictEqual(typeof value, 'object'); assert.notStrictEqual(value, null); }};
/* TODO(kt3k): Enable this testprocess.env.TMPDIR = '/tmpdir';process.env.TMP = '/tmp';process.env.TEMP = '/temp';if (common.isWindows) { assert.strictEqual(os.tmpdir(), '/temp'); process.env.TEMP = ''; assert.strictEqual(os.tmpdir(), '/tmp'); process.env.TMP = ''; const expected = `${process.env.SystemRoot || process.env.windir}\\temp`; assert.strictEqual(os.tmpdir(), expected); process.env.TEMP = '\\temp\\'; assert.strictEqual(os.tmpdir(), '\\temp'); process.env.TEMP = '\\tmpdir/'; assert.strictEqual(os.tmpdir(), '\\tmpdir/'); process.env.TEMP = '\\'; assert.strictEqual(os.tmpdir(), '\\'); process.env.TEMP = 'C:\\'; assert.strictEqual(os.tmpdir(), 'C:\\');} else { assert.strictEqual(os.tmpdir(), '/tmpdir'); process.env.TMPDIR = ''; assert.strictEqual(os.tmpdir(), '/tmp'); process.env.TMP = ''; assert.strictEqual(os.tmpdir(), '/temp'); process.env.TEMP = ''; assert.strictEqual(os.tmpdir(), '/tmp'); process.env.TMPDIR = '/tmpdir/'; assert.strictEqual(os.tmpdir(), '/tmpdir'); process.env.TMPDIR = '/tmpdir\\'; assert.strictEqual(os.tmpdir(), '/tmpdir\\'); process.env.TMPDIR = '/'; assert.strictEqual(os.tmpdir(), '/');}*/
const endianness = os.endianness();is.string(endianness);assert.match(endianness, /[BL]E/);
const hostname = os.hostname();is.string(hostname);assert.ok(hostname.length > 0);
/** TODO(kt3k): Emable this test// On IBMi, os.uptime() returns 'undefined'if (!common.isIBMi) { const uptime = os.uptime(); is.number(uptime); assert.ok(uptime > 0);}*/
const cpus = os.cpus();is.array(cpus);assert.ok(cpus.length > 0);for (const cpu of cpus) { assert.strictEqual(typeof cpu.model, 'string'); assert.strictEqual(typeof cpu.speed, 'number'); assert.strictEqual(typeof cpu.times.user, 'number'); assert.strictEqual(typeof cpu.times.nice, 'number'); assert.strictEqual(typeof cpu.times.sys, 'number'); assert.strictEqual(typeof cpu.times.idle, 'number'); assert.strictEqual(typeof cpu.times.irq, 'number');}
const type = os.type();is.string(type);assert.ok(type.length > 0);
const release = os.release();is.string(release);assert.ok(release.length > 0);// TODO: Check format on more than just AIXif (common.isAIX) assert.match(release, /^\d+\.\d+$/);
const platform = os.platform();is.string(platform);assert.ok(platform.length > 0);
const arch = os.arch();is.string(arch);assert.ok(arch.length > 0);
if (!common.isSunOS) { // not implemented yet assert.ok(os.loadavg().length > 0); assert.ok(os.freemem() > 0); 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';
const actual = interfaces.lo.filter(filter); const expected = [{ address: '127.0.0.1', netmask: '255.0.0.0', family: 'IPv4', mac: '00:00:00:00:00:00', internal: true, cidr: '127.0.0.1/8' }]; assert.deepStrictEqual(actual, expected); break; } 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', family: 'IPv4', mac: '00:00:00:00:00:00', internal: true, cidr: '127.0.0.1/8' }]; assert.deepStrictEqual(actual, expected); break; }}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}));
Object.values(interfaces) .flat(Infinity) .map((v) => ({ v, mask: netmaskToCIDRSuffixMap.get(v.netmask) })) .forEach(({ v, mask }) => { assert.ok('cidr' in v, `"cidr" prop not found in ${inspect(v)}`); if (mask) { assert.strictEqual(v.cidr, `${v.address}/${mask}`); } });
const EOL = os.EOL;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));
/* TODO(kt3k): Enable this testconst version = os.version();assert.strictEqual(typeof version, 'string');assert(version);*/
if (common.isWindows && process.env.USERPROFILE) { assert.strictEqual(home, process.env.USERPROFILE); delete process.env.USERPROFILE; assert.ok(os.homedir().includes(path.sep)); process.env.USERPROFILE = home;} else if (!common.isWindows && process.env.HOME) { assert.strictEqual(home, process.env.HOME); delete process.env.HOME; assert.ok(os.homedir().includes(path.sep)); process.env.HOME = home;}
/* TODO(kt3k): Enable this testconst pwd = os.userInfo();is.object(pwd);const pwdBuf = os.userInfo({ encoding: 'buffer' });
if (common.isWindows) { assert.strictEqual(pwd.uid, -1); assert.strictEqual(pwd.gid, -1); assert.strictEqual(pwd.shell, null); assert.strictEqual(pwdBuf.uid, -1); assert.strictEqual(pwdBuf.gid, -1); assert.strictEqual(pwdBuf.shell, null);} else { is.number(pwd.uid); is.number(pwd.gid); assert.strictEqual(typeof pwd.shell, 'string'); // It's possible for /etc/passwd to leave the user's shell blank. if (pwd.shell.length > 0) { assert(pwd.shell.includes(path.sep)); } assert.strictEqual(pwd.uid, pwdBuf.uid); assert.strictEqual(pwd.gid, pwdBuf.gid); assert.strictEqual(pwd.shell, pwdBuf.shell.toString('utf8'));}
is.string(pwd.username);assert.ok(pwd.homedir.includes(path.sep));assert.strictEqual(pwd.username, pwdBuf.username.toString('utf8'));assert.strictEqual(pwd.homedir, pwdBuf.homedir.toString('utf8'));*/
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());// TODO(kt3k): Enable this test// assert.strictEqual(`${os.tmpdir}`, os.tmpdir());assert.strictEqual(`${os.arch}`, os.arch());assert.strictEqual(`${os.platform}`, os.platform());// TODO(kt3k): Enable this test// assert.strictEqual(`${os.version}`, os.version());
assert.strictEqual(+os.totalmem, os.totalmem());
/* TODO(kt3k): Enable this test// Assert that the following values are coercible to numbers.// On IBMi, os.uptime() returns 'undefined'if (!common.isIBMi) { is.number(+os.uptime, 'uptime'); is.number(os.uptime(), 'uptime');}*/
is.number(+os.freemem, 'freemem');is.number(os.freemem(), 'freemem');
const devNull = os.devNull;if (common.isWindows) { assert.strictEqual(devNull, '\\\\.\\nul');} else { assert.strictEqual(devNull, '/dev/null');}
Version Info