diff --git a/lib/command-exists.js b/lib/command-exists.js index 2230e18..7c37ca3 100644 --- a/lib/command-exists.js +++ b/lib/command-exists.js @@ -62,7 +62,8 @@ var commandExistsUnix = function(commandName, cleanedCommandName, callback) { } var commandExistsWindows = function(commandName, cleanedCommandName, callback) { - if (/[\x00-\x1f<>:"\|\?\*]/.test(commandName)) { + // Regex from Julio from: https://stackoverflow.com/questions/51494579/regex-windows-path-validator + if (!(/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-zA-Z]:)?(?:(?:[^<>:"\|\?\*\n])+(?:\/\/|\/|\\\\|\\)?)+$/m.test(commandName))) { callback(null, false); return; } @@ -93,7 +94,8 @@ var commandExistsUnixSync = function(commandName, cleanedCommandName) { } var commandExistsWindowsSync = function(commandName, cleanedCommandName, callback) { - if (/[\x00-\x1f<>:"\|\?\*]/.test(commandName)) { + // Regex from Julio from: https://stackoverflow.com/questions/51494579/regex-windows-path-validator + if (!(/^(?!(?:.*\s|.*\.|\W+)$)(?:[a-zA-Z]:)?(?:(?:[^<>:"\|\?\*\n])+(?:\/\/|\/|\\\\|\\)?)+$/m.test(commandName))) { return false; } try { diff --git a/test/test.js b/test/test.js index 89ee1ba..34670e6 100644 --- a/test/test.js +++ b/test/test.js @@ -3,14 +3,15 @@ var expect = require('expect.js'); var commandExists = require('..'); var commandExistsSync = commandExists.sync; +var resolve = require('path').resolve; var isUsingWindows = process.platform == 'win32' describe('commandExists', function(){ describe('async - callback', function() { - it('it should find a command named ls or dir', function(done){ + it('it should find a command named ls or xcopy', function(done){ var commandToUse = 'ls' if (isUsingWindows) { - commandToUse = 'dir' + commandToUse = 'xcopy' } commandExists(commandToUse, function(err, exists) { @@ -30,10 +31,10 @@ describe('commandExists', function(){ }); describe('async - promise', function() { - it('it should find a command named ls or dir', function(done){ + it('it should find a command named ls or xcopy', function(done){ var commandToUse = 'ls' if (isUsingWindows) { - commandToUse = 'dir' + commandToUse = 'xcopy' } commandExists(commandToUse) @@ -56,10 +57,10 @@ describe('commandExists', function(){ }); describe('sync', function() { - it('it should find a command named ls or dir', function(){ + it('it should find a command named ls or xcopy', function(){ var commandToUse = 'ls' if (isUsingWindows) { - commandToUse = 'dir' + commandToUse = 'xcopy' } expect(commandExistsSync(commandToUse)).to.be(true); }); @@ -68,10 +69,10 @@ describe('commandExists', function(){ expect(commandExistsSync('fdsafdsafdsafdsafdsa')).to.be(false); }); - it('it should not find a command named ls or dir prefixed with some nonsense', function(){ + it('it should not find a command named ls or xcopy prefixed with some nonsense', function(){ var commandToUse = 'fdsafdsa ls' if (isUsingWindows) { - commandToUse = 'fdsafdsaf dir' + commandToUse = 'fdsafdsaf xcopy' } expect(commandExistsSync(commandToUse)).to.be(false); }); @@ -127,4 +128,20 @@ describe('commandExists', function(){ }); } }); + + describe('absolute path', function() { + it('it should report true if there is a command with that name in absolute path', function(done) { + var commandToUse = resolve('test/executable-script.js'); + commandExists(commandToUse) + .then(function(command){ + expect(command).to.be(commandToUse); + done(); + }); + }); + + it('it should report false if there is not a command with that name in absolute path', function() { + var commandToUse = resolve('executable-script.js'); + expect(commandExists.sync(commandToUse)).to.be(false); + }); + }); });