test.js 5.2 KB

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