From 8f51e368981c7e584e1f7f278a940d6f31c076fb Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Sun, 29 May 2016 17:53:40 +0200 Subject: [PATCH] test: use common platform helpers everywhere Use the common.isWindows, common.isFreeBSD and common.isSunOS where possible. Add common.isOSX and common.isLinux. Fix `test-fs-read-file-sync-hostname` as in its current form was not being run anywhere. PR-URL: https://github.com/nodejs/node/pull/7845 Reviewed-By: Colin Ihrig Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell --- test/common.js | 10 ++++++---- test/internet/test-dns-ipv6.js | 2 +- test/parallel/test-cwd-enoent-preload.js | 2 +- test/parallel/test-cwd-enoent-repl.js | 2 +- test/parallel/test-cwd-enoent.js | 2 +- test/parallel/test-dgram-empty-packet.js | 2 +- test/parallel/test-dgram-send-empty-array.js | 2 +- test/parallel/test-dgram-send-empty-buffer.js | 2 +- test/parallel/test-domain-abort-on-uncaught.js | 2 +- ...omain-no-error-handler-abort-on-uncaught.js | 2 +- ...en-throw-from-uncaught-exception-handler.js | 2 +- ...-domain-with-abort-on-uncaught-exception.js | 2 +- .../test-fs-read-file-sync-hostname.js | 18 +++++++++++------- test/parallel/test-fs-readdir-ucs2.js | 2 +- test/parallel/test-fs-watch-recursive.js | 2 +- test/parallel/test-os.js | 2 +- .../parallel/test-process-constants-noatime.js | 4 ++-- test/parallel/test-process-getgroups.js | 2 +- test/parallel/test-repl-sigint-nested-eval.js | 2 +- test/parallel/test-repl-sigint.js | 2 +- test/parallel/test-setproctitle.js | 5 +++-- test/parallel/test-util-sigint-watchdog.js | 2 +- .../test-vm-sigint-existing-handler.js | 2 +- test/parallel/test-vm-sigint.js | 2 +- test/pummel/test-net-pingpong.js | 5 ++--- test/sequential/test-fs-watch.js | 4 ++-- 26 files changed, 46 insertions(+), 40 deletions(-) diff --git a/test/common.js b/test/common.js index aacea9ae76..4aa48de48f 100644 --- a/test/common.js +++ b/test/common.js @@ -26,6 +26,8 @@ exports.isLinuxPPCBE = (process.platform === 'linux') && (os.endianness() === 'BE'); exports.isSunOS = process.platform === 'sunos'; exports.isFreeBSD = process.platform === 'freebsd'; +exports.isLinux = process.platform === 'linux'; +exports.isOSX = process.platform === 'darwin'; exports.enoughTestMem = os.totalmem() > 0x40000000; /* 1 Gb */ exports.rootDir = exports.isWindows ? 'c:\\' : '/'; @@ -61,7 +63,7 @@ function rmdirSync(p, originalEr) { if (e.code === 'ENOTDIR') throw originalEr; if (e.code === 'ENOTEMPTY' || e.code === 'EEXIST' || e.code === 'EPERM') { - const enc = process.platform === 'linux' ? 'buffer' : 'utf8'; + const enc = exports.isLinux ? 'buffer' : 'utf8'; fs.readdirSync(p, enc).forEach((f) => { if (f instanceof Buffer) { const buf = Buffer.concat([Buffer.from(p), Buffer.from(path.sep), f]); @@ -91,7 +93,7 @@ var inFreeBSDJail = null; var localhostIPv4 = null; exports.localIPv6Hosts = ['localhost']; -if (process.platform === 'linux') { +if (exports.isLinux) { exports.localIPv6Hosts = [ // Debian/Ubuntu 'ip6-localhost', @@ -110,7 +112,7 @@ Object.defineProperty(exports, 'inFreeBSDJail', { get: function() { if (inFreeBSDJail !== null) return inFreeBSDJail; - if (process.platform === 'freebsd' && + if (exports.isFreeBSD && child_process.execSync('sysctl -n security.jail.jailed').toString() === '1\n') { inFreeBSDJail = true; @@ -469,7 +471,7 @@ exports.nodeProcessAborted = function nodeProcessAborted(exitCode, signal) { // On Windows, v8's base::OS::Abort triggers an access violation, // which corresponds to exit code 3221225477 (0xC0000005) - if (process.platform === 'win32') + if (exports.isWindows) expectedExitCodes = [3221225477]; // When using --abort-on-uncaught-exception, V8 will use diff --git a/test/internet/test-dns-ipv6.js b/test/internet/test-dns-ipv6.js index a9e46c8478..f14b45df07 100644 --- a/test/internet/test-dns-ipv6.js +++ b/test/internet/test-dns-ipv6.js @@ -123,7 +123,7 @@ TEST(function test_lookup_ipv6_hint(done) { }, function(err, ip, family) { if (err) { // FreeBSD does not support V4MAPPED - if (process.platform === 'freebsd') { + if (common.isFreeBSD) { assert(err instanceof Error); assert.strictEqual(err.code, 'EAI_BADFLAGS'); assert.strictEqual(err.hostname, 'www.google.com'); diff --git a/test/parallel/test-cwd-enoent-preload.js b/test/parallel/test-cwd-enoent-preload.js index b2f7ae0a4a..8418e1177e 100644 --- a/test/parallel/test-cwd-enoent-preload.js +++ b/test/parallel/test-cwd-enoent-preload.js @@ -5,7 +5,7 @@ const fs = require('fs'); const spawn = require('child_process').spawn; // Fails with EINVAL on SmartOS, EBUSY on Windows, EBUSY on AIX. -if (process.platform === 'sunos' || common.isWindows || common.isAix) { +if (common.isSunOS || common.isWindows || common.isAix) { common.skip('cannot rmdir current working directory'); return; } diff --git a/test/parallel/test-cwd-enoent-repl.js b/test/parallel/test-cwd-enoent-repl.js index 3c8b543cf1..2782ec4394 100644 --- a/test/parallel/test-cwd-enoent-repl.js +++ b/test/parallel/test-cwd-enoent-repl.js @@ -5,7 +5,7 @@ var fs = require('fs'); var spawn = require('child_process').spawn; // Fails with EINVAL on SmartOS, EBUSY on Windows, EBUSY on AIX. -if (process.platform === 'sunos' || common.isWindows || common.isAix) { +if (common.isSunOS || common.isWindows || common.isAix) { common.skip('cannot rmdir current working directory'); return; } diff --git a/test/parallel/test-cwd-enoent.js b/test/parallel/test-cwd-enoent.js index 9ff9f86405..f5c04fe068 100644 --- a/test/parallel/test-cwd-enoent.js +++ b/test/parallel/test-cwd-enoent.js @@ -5,7 +5,7 @@ var fs = require('fs'); var spawn = require('child_process').spawn; // Fails with EINVAL on SmartOS, EBUSY on Windows, EBUSY on AIX. -if (process.platform === 'sunos' || common.isWindows || common.isAix) { +if (common.isSunOS || common.isWindows || common.isAix) { common.skip('cannot rmdir current working directory'); return; } diff --git a/test/parallel/test-dgram-empty-packet.js b/test/parallel/test-dgram-empty-packet.js index cdda183e2a..1c7e7205d1 100644 --- a/test/parallel/test-dgram-empty-packet.js +++ b/test/parallel/test-dgram-empty-packet.js @@ -6,7 +6,7 @@ var callbacks = 0; var client; var timer; -if (process.platform === 'darwin') { +if (common.isOSX) { common.skip('because of 17894467 Apple bug'); return; } diff --git a/test/parallel/test-dgram-send-empty-array.js b/test/parallel/test-dgram-send-empty-array.js index 959fbed158..c5050a7d54 100644 --- a/test/parallel/test-dgram-send-empty-array.js +++ b/test/parallel/test-dgram-send-empty-array.js @@ -4,7 +4,7 @@ const common = require('../common'); const assert = require('assert'); const dgram = require('dgram'); -if (process.platform === 'darwin') { +if (common.isOSX) { common.skip('because of 17894467 Apple bug'); return; } diff --git a/test/parallel/test-dgram-send-empty-buffer.js b/test/parallel/test-dgram-send-empty-buffer.js index d6d31f4166..ac541a9c24 100644 --- a/test/parallel/test-dgram-send-empty-buffer.js +++ b/test/parallel/test-dgram-send-empty-buffer.js @@ -2,7 +2,7 @@ const common = require('../common'); const dgram = require('dgram'); -if (process.platform === 'darwin') { +if (common.isOSX) { common.skip('because of 17894467 Apple bug'); return; } diff --git a/test/parallel/test-domain-abort-on-uncaught.js b/test/parallel/test-domain-abort-on-uncaught.js index 4f290d22e7..2a5eb804bc 100644 --- a/test/parallel/test-domain-abort-on-uncaught.js +++ b/test/parallel/test-domain-abort-on-uncaught.js @@ -233,7 +233,7 @@ if (process.argv[2] === 'child') { tests.forEach(function(test, testIndex) { var testCmd = ''; - if (process.platform !== 'win32') { + if (!common.isWindows) { // Do not create core files, as it can take a lot of disk space on // continuous testing and developers' machines testCmd += 'ulimit -c 0 && '; diff --git a/test/parallel/test-domain-no-error-handler-abort-on-uncaught.js b/test/parallel/test-domain-no-error-handler-abort-on-uncaught.js index 329772e744..76ca3faad7 100644 --- a/test/parallel/test-domain-no-error-handler-abort-on-uncaught.js +++ b/test/parallel/test-domain-no-error-handler-abort-on-uncaught.js @@ -144,7 +144,7 @@ if (process.argv[2] === 'child') { tests.forEach(function(test, testIndex) { var testCmd = ''; - if (process.platform !== 'win32') { + if (!common.isWindows) { // Do not create core files, as it can take a lot of disk space on // continuous testing and developers' machines testCmd += 'ulimit -c 0 && '; diff --git a/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js b/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js index ae86ef2cdf..b490a30b52 100644 --- a/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js +++ b/test/parallel/test-domain-throw-error-then-throw-from-uncaught-exception-handler.js @@ -82,7 +82,7 @@ function runTestWithAbortOnUncaughtException() { function createTestCmdLine(options) { var testCmd = ''; - if (process.platform !== 'win32') { + if (!common.isWindows) { // Do not create core files, as it can take a lot of disk space on // continuous testing and developers' machines testCmd += 'ulimit -c 0 && '; diff --git a/test/parallel/test-domain-with-abort-on-uncaught-exception.js b/test/parallel/test-domain-with-abort-on-uncaught-exception.js index 423c77136f..455acc7804 100644 --- a/test/parallel/test-domain-with-abort-on-uncaught-exception.js +++ b/test/parallel/test-domain-with-abort-on-uncaught-exception.js @@ -93,7 +93,7 @@ if (process.argv[2] === 'child') { throwInDomainErrHandlerOpt = 'throwInDomainErrHandler'; var cmdToExec = ''; - if (process.platform !== 'win32') { + if (!common.isWindows) { // Do not create core files, as it can take a lot of disk space on // continuous testing and developers' machines cmdToExec += 'ulimit -c 0 && '; diff --git a/test/parallel/test-fs-read-file-sync-hostname.js b/test/parallel/test-fs-read-file-sync-hostname.js index de10573796..ca300819e7 100644 --- a/test/parallel/test-fs-read-file-sync-hostname.js +++ b/test/parallel/test-fs-read-file-sync-hostname.js @@ -1,10 +1,14 @@ 'use strict'; -require('../common'); -var assert = require('assert'); -var fs = require('fs'); +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); -// test reading from hostname -if (process.platform === 'linux2') { - var hostname = fs.readFileSync('/proc/sys/kernel/hostname'); - assert.ok(hostname.length > 0); +if (!common.isLinux) { + common.skip('Test is linux specific.'); + return; } + +// Test to make sure reading a file under the /proc directory works. See: +// https://groups.google.com/forum/#!topic/nodejs-dev/rxZ_RoH1Gn0 +const hostname = fs.readFileSync('/proc/sys/kernel/hostname'); +assert.ok(hostname.length > 0); diff --git a/test/parallel/test-fs-readdir-ucs2.js b/test/parallel/test-fs-readdir-ucs2.js index 46c9995435..916758fe94 100644 --- a/test/parallel/test-fs-readdir-ucs2.js +++ b/test/parallel/test-fs-readdir-ucs2.js @@ -5,7 +5,7 @@ const path = require('path'); const fs = require('fs'); const assert = require('assert'); -if (process.platform !== 'linux') { +if (!common.isLinux) { common.skip('Test is linux specific.'); return; } diff --git a/test/parallel/test-fs-watch-recursive.js b/test/parallel/test-fs-watch-recursive.js index 7853baaa02..23a92129a7 100644 --- a/test/parallel/test-fs-watch-recursive.js +++ b/test/parallel/test-fs-watch-recursive.js @@ -2,7 +2,7 @@ const common = require('../common'); -if (!(process.platform === 'darwin' || common.isWindows)) { +if (!(common.isOSX || common.isWindows)) { common.skip('recursive option is darwin/windows specific'); return; } diff --git a/test/parallel/test-os.js b/test/parallel/test-os.js index 2b01b78d2b..8f4db81707 100644 --- a/test/parallel/test-os.js +++ b/test/parallel/test-os.js @@ -71,7 +71,7 @@ var arch = os.arch(); console.log('arch = ', arch); assert.ok(arch.length > 0); -if (process.platform != 'sunos') { +if (!common.isSunOS) { // not implemeneted yet assert.ok(os.loadavg().length > 0); assert.ok(os.freemem() > 0); diff --git a/test/parallel/test-process-constants-noatime.js b/test/parallel/test-process-constants-noatime.js index bf4ed4d395..d0c027f0b9 100644 --- a/test/parallel/test-process-constants-noatime.js +++ b/test/parallel/test-process-constants-noatime.js @@ -1,10 +1,10 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const constants = process.binding('constants'); -if (process.platform === 'linux') { +if (common.isLinux) { assert('O_NOATIME' in constants.fs); assert.strictEqual(constants.fs.O_NOATIME, 0x40000); } else { diff --git a/test/parallel/test-process-getgroups.js b/test/parallel/test-process-getgroups.js index 6d07692a76..7910eb3e29 100644 --- a/test/parallel/test-process-getgroups.js +++ b/test/parallel/test-process-getgroups.js @@ -3,7 +3,7 @@ const common = require('../common'); var assert = require('assert'); var exec = require('child_process').exec; -if (process.platform === 'darwin') { +if (common.isOSX) { common.skip('Output of `id -G` is unreliable on Darwin.'); return; } diff --git a/test/parallel/test-repl-sigint-nested-eval.js b/test/parallel/test-repl-sigint-nested-eval.js index 288c4bceeb..1bb09ba396 100644 --- a/test/parallel/test-repl-sigint-nested-eval.js +++ b/test/parallel/test-repl-sigint-nested-eval.js @@ -4,7 +4,7 @@ const assert = require('assert'); const spawn = require('child_process').spawn; -if (process.platform === 'win32') { +if (common.isWindows) { // No way to send CTRL_C_EVENT to processes from JS right now. common.skip('platform not supported'); return; diff --git a/test/parallel/test-repl-sigint.js b/test/parallel/test-repl-sigint.js index 6b342ce861..5ee974aaea 100644 --- a/test/parallel/test-repl-sigint.js +++ b/test/parallel/test-repl-sigint.js @@ -4,7 +4,7 @@ const assert = require('assert'); const spawn = require('child_process').spawn; -if (process.platform === 'win32') { +if (common.isWindows) { // No way to send CTRL_C_EVENT to processes from JS right now. common.skip('platform not supported'); return; diff --git a/test/parallel/test-setproctitle.js b/test/parallel/test-setproctitle.js index 943cd60769..4d096b1941 100644 --- a/test/parallel/test-setproctitle.js +++ b/test/parallel/test-setproctitle.js @@ -1,9 +1,10 @@ 'use strict'; // Original test written by Jakub Lekstan +const common = require('../common'); require('../common'); // FIXME add sunos support -if ('linux freebsd darwin'.indexOf(process.platform) === -1) { +if (!(common.isFreeBSD || common.isOSX || common.isLinux)) { console.log(`1..0 # Skipped: Unsupported platform [${process.platform}]`); return; } @@ -26,7 +27,7 @@ exec('ps -p ' + process.pid + ' -o args=', function(error, stdout, stderr) { assert.equal(stderr, ''); // freebsd always add ' (procname)' to the process title - if (process.platform === 'freebsd') + if (common.isFreeBSD) title += ` (${path.basename(process.execPath)})`; // omitting trailing whitespace and \n diff --git a/test/parallel/test-util-sigint-watchdog.js b/test/parallel/test-util-sigint-watchdog.js index 2f95286a5e..f9bb3ecd93 100644 --- a/test/parallel/test-util-sigint-watchdog.js +++ b/test/parallel/test-util-sigint-watchdog.js @@ -3,7 +3,7 @@ const common = require('../common'); const assert = require('assert'); const binding = process.binding('util'); -if (process.platform === 'win32') { +if (common.isWindows) { // No way to send CTRL_C_EVENT to processes from JS right now. common.skip('platform not supported'); return; diff --git a/test/parallel/test-vm-sigint-existing-handler.js b/test/parallel/test-vm-sigint-existing-handler.js index e86bbeec0b..0f86b53dd2 100644 --- a/test/parallel/test-vm-sigint-existing-handler.js +++ b/test/parallel/test-vm-sigint-existing-handler.js @@ -5,7 +5,7 @@ const vm = require('vm'); const spawn = require('child_process').spawn; -if (process.platform === 'win32') { +if (common.isWindows) { // No way to send CTRL_C_EVENT to processes from JS right now. common.skip('platform not supported'); return; diff --git a/test/parallel/test-vm-sigint.js b/test/parallel/test-vm-sigint.js index 11497733ce..2488b0d1c0 100644 --- a/test/parallel/test-vm-sigint.js +++ b/test/parallel/test-vm-sigint.js @@ -5,7 +5,7 @@ const vm = require('vm'); const spawn = require('child_process').spawn; -if (process.platform === 'win32') { +if (common.isWindows) { // No way to send CTRL_C_EVENT to processes from JS right now. common.skip('platform not supported'); return; diff --git a/test/pummel/test-net-pingpong.js b/test/pummel/test-net-pingpong.js index a7ede0b056..85dce02b6c 100644 --- a/test/pummel/test-net-pingpong.js +++ b/test/pummel/test-net-pingpong.js @@ -94,9 +94,8 @@ pingPongTest(common.PORT, 'localhost'); pingPongTest(common.PORT + 1, null); // This IPv6 isn't working on Solaris -var solaris = /sunos/i.test(process.platform); -if (!solaris) pingPongTest(common.PORT + 2, '::1'); +if (!common.isSunOS) pingPongTest(common.PORT + 2, '::1'); process.on('exit', function() { - assert.equal(solaris ? 2 : 3, tests_run); + assert.equal(common.isSunOS ? 2 : 3, tests_run); }); diff --git a/test/sequential/test-fs-watch.js b/test/sequential/test-fs-watch.js index 249164c7e4..f254ebc17c 100644 --- a/test/sequential/test-fs-watch.js +++ b/test/sequential/test-fs-watch.js @@ -5,8 +5,8 @@ var path = require('path'); var fs = require('fs'); var expectFilePath = common.isWindows || - process.platform === 'linux' || - process.platform === 'darwin'; + common.isLinux || + common.isOSX; var watchSeenOne = 0; var watchSeenTwo = 0;