From f76722686b91cf1c8db05a116ea0a960a5a49e8a Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Tue, 13 Jan 2015 03:21:29 +0100 Subject: [PATCH] test: use assert.throw to test exceptions The test wasn't checking directly that an assertion was thrown. Instead, it was checking that spawn did not sucessfully spawn a non-existent command. However, the command chosen, dir, exists in GNU coreutils, so it exists on Linux (though not on BSD derived OS X). The test as written passed on Linux, even with the TypeError it is supposed to be checking for deleted from spawn(). It would also pass on Windows if a ls.exe existed. The approach is unnecessarily obscure, assert.throw() is for asserting code throws, using it is more clear and works regardless of what commands do or do not exist. PR-URL: https://github.com/joyent/node/pull/8454 Reviewed-by: Trevor Norris Cherry-picked-from: https://github.com/joyent/node/commit/2ff29cc7e35f486daf86710fd2db48275442c788 Conflicts: test/parallel/test-child-process-spawn-typeerror.js --- .../test-child-process-spawn-typeerror.js | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/test/parallel/test-child-process-spawn-typeerror.js b/test/parallel/test-child-process-spawn-typeerror.js index 14d996e975bc4a..b95b208eb0c87e 100644 --- a/test/parallel/test-child-process-spawn-typeerror.js +++ b/test/parallel/test-child-process-spawn-typeerror.js @@ -1,23 +1,14 @@ -var spawn = require('child_process').spawn, - assert = require('assert'), - windows = (process.platform === 'win32'), - cmd = (windows) ? 'rundll32' : 'ls', - invalidcmd = 'hopefully_you_dont_have_this_on_your_machine', - invalidArgsMsg = /Incorrect value of args option/, - invalidOptionsMsg = /options argument must be an object/, - errors = 0; - -try { - // Ensure this throws a TypeError - var child = spawn(invalidcmd, 'this is not an array'); - - child.on('error', function (err) { - errors++; - }); - -} catch (e) { - assert.equal(e instanceof TypeError, true); -} +var assert = require('assert'); +var child_process = require('child_process'); +var spawn = child_process.spawn; +var cmd = (process.platform === 'win32') ? 'rundll32' : 'ls'; +var invalidArgsMsg = /Incorrect value of args option/; +var invalidOptionsMsg = /options argument must be an object/; + +// verify that args argument must be an array +assert.throws(function() { + spawn(cmd, 'this is not an array'); +}, TypeError); // verify that valid argument combinations do not throw assert.doesNotThrow(function() { @@ -57,6 +48,3 @@ assert.throws(function() { spawn(cmd, [], 1); }, invalidOptionsMsg); -process.on('exit', function() { - assert.equal(errors, 0); -});