test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. var expect = require('expect.js');
  3. var commandExists = require('..');
  4. var commandExistsSync = commandExists.sync;
  5. var isUsingWindows = process.platform == 'win32'
  6. describe('commandExists', function(){
  7. describe('async - callback', function() {
  8. it('it should find a command named ls or dir', function(done){
  9. var commandToUse = 'ls'
  10. if (isUsingWindows) {
  11. commandToUse = 'dir'
  12. }
  13. commandExists(commandToUse, function(err, exists) {
  14. expect(err).to.be(null);
  15. expect(exists).to.be(true);
  16. done();
  17. });
  18. });
  19. it('it should not find a command named fdsafdsafdsafdsafdsa', function(done){
  20. commandExists('fdsafdsafdsafdsafdsa', function(err, exists) {
  21. expect(err).to.be(null);
  22. expect(exists).to.be(false);
  23. done();
  24. });
  25. });
  26. });
  27. describe('async - promise', function() {
  28. it('it should find a command named ls or dir', function(done){
  29. var commandToUse = 'ls'
  30. if (isUsingWindows) {
  31. commandToUse = 'dir'
  32. }
  33. commandExists(commandToUse)
  34. .then(function(command) {
  35. expect(command).to.be(commandToUse);
  36. done();
  37. });
  38. });
  39. it('it should not find a command named fdsafdsafdsafdsafdsa', function(done){
  40. commandExists('fdsafdsafdsafdsafdsa')
  41. .then(function() {
  42. // We should not execute this line.
  43. expect(true).to.be(false);
  44. })
  45. .catch(function() {
  46. done();
  47. });
  48. });
  49. });
  50. describe('sync', function() {
  51. it('it should find a command named ls or dir', function(){
  52. var commandToUse = 'ls'
  53. if (isUsingWindows) {
  54. commandToUse = 'dir'
  55. }
  56. expect(commandExistsSync(commandToUse)).to.be(true);
  57. });
  58. it('it should not find a command named fdsafdsafdsafdsafdsa', function(){
  59. expect(commandExistsSync('fdsafdsafdsafdsafdsa')).to.be(false);
  60. });
  61. it('it should not find a command named ls or dir prefixed with some nonsense', function(){
  62. var commandToUse = 'fdsafdsa ls'
  63. if (isUsingWindows) {
  64. commandToUse = 'fdsafdsaf dir'
  65. }
  66. expect(commandExistsSync(commandToUse)).to.be(false);
  67. });
  68. it('it should not execute some nefarious code', function(){
  69. expect(commandExistsSync('ls; touch /tmp/foo0')).to.be(false);
  70. });
  71. it('it should not execute some nefarious code', function(){
  72. expect(commandExistsSync('ls touch /tmp/foo0')).to.be(false);
  73. });
  74. });
  75. describe('local file', function() {
  76. it('it should report false if there is a non-executable file with that name', function(done) {
  77. var commandToUse = 'test/non-executable-script.js'
  78. commandExists(commandToUse)
  79. .then(function(command){
  80. // We should not execute this line.
  81. expect(true).to.be(false);
  82. }).catch(function(err){
  83. expect(err).to.be(null);
  84. done();
  85. });
  86. });
  87. if (!isUsingWindows) {
  88. it('it should report true if there is an executable file with that name', function(done) {
  89. var commandToUse = 'test/executable-script.js'
  90. commandExists(commandToUse)
  91. .then(function(command){
  92. // We should not execute this line.
  93. expect(command).to.be(commandToUse);
  94. done();
  95. });
  96. });
  97. }
  98. if (isUsingWindows) {
  99. it('it should report true if there is an executable file with that name', function(done) {
  100. var commandToUse = 'test\\executable-script.cmd'
  101. commandExists(commandToUse)
  102. .then(function(command){
  103. expect(command).to.be(commandToUse);
  104. done();
  105. });
  106. });
  107. it('it should report false if there is a double quotation mark in the file path', function() {
  108. var commandToUse = 'test\\"executable-script.cmd'
  109. expect(commandExists.sync(commandToUse)).to.be(false);
  110. });
  111. }
  112. });
  113. });