From 33b3a6cf4dedfba39f30be858450ff4e3a7cb072 Mon Sep 17 00:00:00 2001 From: Andrew Glick <17516195+Antyos@users.noreply.github.com> Date: Sat, 28 Mar 2020 12:33:57 -0500 Subject: [PATCH 1/5] Replaced "dir" with "xcopy" for windows tests --- test/test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/test.js b/test/test.js index 89ee1ba..a4913e2 100644 --- a/test/test.js +++ b/test/test.js @@ -7,10 +7,10 @@ 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 +30,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 +56,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 +68,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); }); From 35e018e5a5270b6a5ee503a88165e28157133ecf Mon Sep 17 00:00:00 2001 From: Andrew Glick <17516195+Antyos@users.noreply.github.com> Date: Sat, 28 Mar 2020 14:18:32 -0500 Subject: [PATCH 2/5] Fixed not working for absolute path on windows --- lib/command-exists.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 { From 47f13ad4d34d0c0c71789de8d0f425d57d1642c8 Mon Sep 17 00:00:00 2001 From: Andrew Glick <17516195+Antyos@users.noreply.github.com> Date: Sat, 28 Mar 2020 14:20:54 -0500 Subject: [PATCH 3/5] Added test code for absolute path --- test/test.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/test.js b/test/test.js index a4913e2..4d90e2a 100644 --- a/test/test.js +++ b/test/test.js @@ -3,6 +3,7 @@ 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(){ @@ -127,4 +128,37 @@ describe('commandExists', function(){ }); } }); + + describe('absolute path', function() { + if (!isUsingWindows) { + it('it should report true if there is a command with that name in absolute path', function(done) { + var commandToUse = resolve('./test/executable-script.cmd') + 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.cmd') + expect(commandExists.sync(commandToUse)).to.be(false); + }); + } + if (isUsingWindows) { + it('it should report true if there is a command with that name in absolute path', function(done) { + var commandToUse = resolve('.\\test\\executable-script.cmd') + 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.cmd') + expect(commandExists.sync(commandToUse)).to.be(false); + }); + } + }); }); From acc029c4367b10bff6156826ec222ba07154694d Mon Sep 17 00:00:00 2001 From: Andrew Glick <17516195+Antyos@users.noreply.github.com> Date: Tue, 31 Mar 2020 22:06:36 -0500 Subject: [PATCH 4/5] Fixed non-windows absolute path test --- test/test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index 4d90e2a..5e67eb8 100644 --- a/test/test.js +++ b/test/test.js @@ -132,7 +132,7 @@ describe('commandExists', function(){ describe('absolute path', function() { if (!isUsingWindows) { it('it should report true if there is a command with that name in absolute path', function(done) { - var commandToUse = resolve('./test/executable-script.cmd') + var commandToUse = resolve('test/executable-script.js'); commandExists(commandToUse) .then(function(command){ expect(command).to.be(commandToUse); @@ -141,13 +141,13 @@ describe('commandExists', function(){ }); it('it should report false if there is not a command with that name in absolute path', function() { - var commandToUse = resolve('./executable-script.cmd') + var commandToUse = resolve('executable-script.js'); expect(commandExists.sync(commandToUse)).to.be(false); }); } if (isUsingWindows) { it('it should report true if there is a command with that name in absolute path', function(done) { - var commandToUse = resolve('.\\test\\executable-script.cmd') + var commandToUse = resolve('test\\executable-script.cmd'); commandExists(commandToUse) .then(function(command){ expect(command).to.be(commandToUse); @@ -156,7 +156,7 @@ describe('commandExists', function(){ }); it('it should report false if there is not a command with that name in absolute path', function() { - var commandToUse = resolve('.\\executable-script.cmd') + var commandToUse = resolve('executable-script.cmd'); expect(commandExists.sync(commandToUse)).to.be(false); }); } From 17594d576da1a571fe4009187fbaaf58527baa1f Mon Sep 17 00:00:00 2001 From: Andrew Glick <17516195+Antyos@users.noreply.github.com> Date: Wed, 1 Apr 2020 22:08:30 -0500 Subject: [PATCH 5/5] Simplified absolute path tests between platforms --- test/test.js | 41 ++++++++++++----------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/test/test.js b/test/test.js index 5e67eb8..34670e6 100644 --- a/test/test.js +++ b/test/test.js @@ -130,35 +130,18 @@ describe('commandExists', function(){ }); describe('absolute path', function() { - if (!isUsingWindows) { - 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); - }); - } - if (isUsingWindows) { - it('it should report true if there is a command with that name in absolute path', function(done) { - var commandToUse = resolve('test\\executable-script.cmd'); - 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.cmd'); - expect(commandExists.sync(commandToUse)).to.be(false); + 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); + }); }); });