basic.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. var t = require('tap')
  2. var fs = require('fs')
  3. var path = require('path')
  4. var fixture = path.resolve(__dirname, 'fixtures')
  5. var meow = fixture + '/meow.cat'
  6. var mine = fixture + '/mine.cat'
  7. var ours = fixture + '/ours.cat'
  8. var fail = fixture + '/fail.false'
  9. var noent = fixture + '/enoent.exe'
  10. var mkdirp = require('mkdirp')
  11. var rimraf = require('rimraf')
  12. var isWindows = process.platform === 'win32'
  13. var hasAccess = typeof fs.access === 'function'
  14. var winSkip = isWindows && 'windows'
  15. var accessSkip = !hasAccess && 'no fs.access function'
  16. var hasPromise = typeof Promise === 'function'
  17. var promiseSkip = !hasPromise && 'no global Promise'
  18. function reset () {
  19. delete require.cache[require.resolve('../')]
  20. return require('../')
  21. }
  22. t.test('setup fixtures', function (t) {
  23. rimraf.sync(fixture)
  24. mkdirp.sync(fixture)
  25. fs.writeFileSync(meow, '#!/usr/bin/env cat\nmeow\n')
  26. fs.chmodSync(meow, parseInt('0755', 8))
  27. fs.writeFileSync(fail, '#!/usr/bin/env false\n')
  28. fs.chmodSync(fail, parseInt('0644', 8))
  29. fs.writeFileSync(mine, '#!/usr/bin/env cat\nmine\n')
  30. fs.chmodSync(mine, parseInt('0744', 8))
  31. fs.writeFileSync(ours, '#!/usr/bin/env cat\nours\n')
  32. fs.chmodSync(ours, parseInt('0754', 8))
  33. t.end()
  34. })
  35. t.test('promise', { skip: promiseSkip }, function (t) {
  36. var isexe = reset()
  37. t.test('meow async', function (t) {
  38. isexe(meow).then(function (is) {
  39. t.ok(is)
  40. t.end()
  41. })
  42. })
  43. t.test('fail async', function (t) {
  44. isexe(fail).then(function (is) {
  45. t.notOk(is)
  46. t.end()
  47. })
  48. })
  49. t.test('noent async', function (t) {
  50. isexe(noent).catch(function (er) {
  51. t.ok(er)
  52. t.end()
  53. })
  54. })
  55. t.test('noent ignore async', function (t) {
  56. isexe(noent, { ignoreErrors: true }).then(function (is) {
  57. t.notOk(is)
  58. t.end()
  59. })
  60. })
  61. t.end()
  62. })
  63. t.test('no promise', function (t) {
  64. global.Promise = null
  65. var isexe = reset()
  66. t.throws('try to meow a promise', function () {
  67. isexe(meow)
  68. })
  69. t.end()
  70. })
  71. t.test('access', { skip: accessSkip || winSkip }, function (t) {
  72. runTest(t)
  73. })
  74. t.test('mode', { skip: winSkip }, function (t) {
  75. delete fs.access
  76. delete fs.accessSync
  77. var isexe = reset()
  78. t.ok(isexe.sync(ours, { uid: 0, gid: 0 }))
  79. t.ok(isexe.sync(mine, { uid: 0, gid: 0 }))
  80. runTest(t)
  81. })
  82. t.test('windows', function (t) {
  83. global.TESTING_WINDOWS = true
  84. var pathExt = '.EXE;.CAT;.CMD;.COM'
  85. t.test('pathExt option', function (t) {
  86. runTest(t, { pathExt: '.EXE;.CAT;.CMD;.COM' })
  87. })
  88. t.test('pathExt env', function (t) {
  89. process.env.PATHEXT = pathExt
  90. runTest(t)
  91. })
  92. t.test('no pathExt', function (t) {
  93. // with a pathExt of '', any filename is fine.
  94. // so the "fail" one would still pass.
  95. runTest(t, { pathExt: '', skipFail: true })
  96. })
  97. t.test('pathext with empty entry', function (t) {
  98. // with a pathExt of '', any filename is fine.
  99. // so the "fail" one would still pass.
  100. runTest(t, { pathExt: ';' + pathExt, skipFail: true })
  101. })
  102. t.end()
  103. })
  104. t.test('cleanup', function (t) {
  105. rimraf.sync(fixture)
  106. t.end()
  107. })
  108. function runTest (t, options) {
  109. var isexe = reset()
  110. var optionsIgnore = Object.create(options || {})
  111. optionsIgnore.ignoreErrors = true
  112. if (!options || !options.skipFail) {
  113. t.notOk(isexe.sync(fail, options))
  114. }
  115. t.notOk(isexe.sync(noent, optionsIgnore))
  116. if (!options) {
  117. t.ok(isexe.sync(meow))
  118. } else {
  119. t.ok(isexe.sync(meow, options))
  120. }
  121. t.ok(isexe.sync(mine, options))
  122. t.ok(isexe.sync(ours, options))
  123. t.throws(function () {
  124. isexe.sync(noent, options)
  125. })
  126. t.test('meow async', function (t) {
  127. if (!options) {
  128. isexe(meow, function (er, is) {
  129. if (er) {
  130. throw er
  131. }
  132. t.ok(is)
  133. t.end()
  134. })
  135. } else {
  136. isexe(meow, options, function (er, is) {
  137. if (er) {
  138. throw er
  139. }
  140. t.ok(is)
  141. t.end()
  142. })
  143. }
  144. })
  145. t.test('mine async', function (t) {
  146. isexe(mine, options, function (er, is) {
  147. if (er) {
  148. throw er
  149. }
  150. t.ok(is)
  151. t.end()
  152. })
  153. })
  154. t.test('ours async', function (t) {
  155. isexe(ours, options, function (er, is) {
  156. if (er) {
  157. throw er
  158. }
  159. t.ok(is)
  160. t.end()
  161. })
  162. })
  163. if (!options || !options.skipFail) {
  164. t.test('fail async', function (t) {
  165. isexe(fail, options, function (er, is) {
  166. if (er) {
  167. throw er
  168. }
  169. t.notOk(is)
  170. t.end()
  171. })
  172. })
  173. }
  174. t.test('noent async', function (t) {
  175. isexe(noent, options, function (er, is) {
  176. t.ok(er)
  177. t.notOk(is)
  178. t.end()
  179. })
  180. })
  181. t.test('noent ignore async', function (t) {
  182. isexe(noent, optionsIgnore, function (er, is) {
  183. if (er) {
  184. throw er
  185. }
  186. t.notOk(is)
  187. t.end()
  188. })
  189. })
  190. t.end()
  191. }