common.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * @author Toru Nagashima
  3. * @copyright 2016 Toru Nagashima. All rights reserved.
  4. * See LICENSE file in root directory for full license.
  5. */
  6. 'use strict'
  7. // ------------------------------------------------------------------------------
  8. // Requirements
  9. // ------------------------------------------------------------------------------
  10. const assert = require('assert').strict
  11. const nodeApi = require('../lib')
  12. const BufferStream = require('./lib/buffer-stream')
  13. const { result, removeResult, runAll, runPar, runSeq } = require('./lib/util')
  14. // ------------------------------------------------------------------------------
  15. // Test
  16. // ------------------------------------------------------------------------------
  17. describe('[common]', () => {
  18. before(() => process.chdir('test-workspace'))
  19. after(() => process.chdir('..'))
  20. beforeEach(removeResult)
  21. describe('should print a help text if arguments are nothing.', () => {
  22. it('npm-run-all command', async () => {
  23. const buf = new BufferStream()
  24. await runAll([], buf)
  25. assert(/Usage:/.test(buf.value))
  26. })
  27. it('run-s command', async () => {
  28. const buf = new BufferStream()
  29. await runSeq([], buf)
  30. assert(/Usage:/.test(buf.value))
  31. })
  32. it('run-p command', async () => {
  33. const buf = new BufferStream()
  34. await runPar([], buf)
  35. assert(/Usage:/.test(buf.value))
  36. })
  37. })
  38. describe('should print a help text if the first argument is --help (-h)', () => {
  39. it('npm-run-all command (-h)', async () => {
  40. const buf = new BufferStream()
  41. await runAll(['-h'], buf)
  42. assert(/Usage:/.test(buf.value))
  43. })
  44. it('run-s command (-h)', async () => {
  45. const buf = new BufferStream()
  46. await runSeq(['-h'], buf)
  47. assert(/Usage:/.test(buf.value))
  48. })
  49. it('run-p command (-h)', async () => {
  50. const buf = new BufferStream()
  51. await runPar(['-h'], buf)
  52. assert(/Usage:/.test(buf.value))
  53. })
  54. it('npm-run-all command (--help)', async () => {
  55. const buf = new BufferStream()
  56. await runAll(['--help'], buf)
  57. assert(/Usage:/.test(buf.value))
  58. })
  59. it('run-s command (--help)', async () => {
  60. const buf = new BufferStream()
  61. await runSeq(['--help'], buf)
  62. assert(/Usage:/.test(buf.value))
  63. })
  64. it('run-p command (--help)', async () => {
  65. const buf = new BufferStream()
  66. await runPar(['--help'], buf)
  67. assert(/Usage:/.test(buf.value))
  68. })
  69. })
  70. describe('should print a version number if the first argument is --version (-v)', () => {
  71. it('npm-run-all command (-v)', async () => {
  72. const buf = new BufferStream()
  73. await runAll(['-v'], buf)
  74. assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
  75. })
  76. it('run-s command (-v)', async () => {
  77. const buf = new BufferStream()
  78. await runSeq(['-v'], buf)
  79. assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
  80. })
  81. it('run-p command (-v)', async () => {
  82. const buf = new BufferStream()
  83. await runPar(['-v'], buf)
  84. assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
  85. })
  86. it('npm-run-all command (--version)', async () => {
  87. const buf = new BufferStream()
  88. await runAll(['--version'], buf)
  89. assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
  90. })
  91. it('run-s command (--version)', async () => {
  92. const buf = new BufferStream()
  93. await runSeq(['--version'], buf)
  94. assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
  95. })
  96. it('run-p command (--version)', async () => {
  97. const buf = new BufferStream()
  98. await runPar(['--version'], buf)
  99. assert(/v[0-9]+\.[0-9]+\.[0-9]+/.test(buf.value))
  100. })
  101. })
  102. describe('should do nothing if a task list is empty.', () => {
  103. it('Node API', async () => {
  104. await nodeApi(null)
  105. assert(result() == null)
  106. })
  107. })
  108. describe('should run a task by npm (check an environment variable):', () => {
  109. it('Node API', async () => {
  110. await nodeApi('test-task:package-config')
  111. assert(result() === 'OK')
  112. })
  113. it('npm-run-all command', async () => {
  114. await runAll(['test-task:package-config'])
  115. assert(result() === 'OK')
  116. })
  117. it('run-s command', async () => {
  118. await runSeq(['test-task:package-config'])
  119. assert(result() === 'OK')
  120. })
  121. it('run-p command', async () => {
  122. await runPar(['test-task:package-config'])
  123. assert(result() === 'OK')
  124. })
  125. })
  126. describe('stdin can be used in tasks:', () => {
  127. it('Node API', async () => {
  128. await nodeApi('test-task:stdin')
  129. assert(result().trim() === 'STDIN')
  130. })
  131. it('npm-run-all command', async () => {
  132. await runAll(['test-task:stdin'])
  133. assert(result().trim() === 'STDIN')
  134. })
  135. it('run-s command', async () => {
  136. await runSeq(['test-task:stdin'])
  137. assert(result().trim() === 'STDIN')
  138. })
  139. it('run-p command', async () => {
  140. await runPar(['test-task:stdin'])
  141. assert(result().trim() === 'STDIN')
  142. })
  143. })
  144. describe('stdout can be used in tasks:', () => {
  145. it('Node API', async () => {
  146. await nodeApi('test-task:stdout')
  147. assert(result() === 'STDOUT')
  148. })
  149. it('npm-run-all command', async () => {
  150. await runAll(['test-task:stdout'])
  151. assert(result() === 'STDOUT')
  152. })
  153. it('run-s command', async () => {
  154. await runSeq(['test-task:stdout'])
  155. assert(result() === 'STDOUT')
  156. })
  157. it('run-p command', async () => {
  158. await runPar(['test-task:stdout'])
  159. assert(result() === 'STDOUT')
  160. })
  161. })
  162. describe('stderr can be used in tasks:', () => {
  163. it('Node API', async () => {
  164. await nodeApi('test-task:stderr')
  165. assert(result() === 'STDERR')
  166. })
  167. it('npm-run-all command', async () => {
  168. await runAll(['test-task:stderr'])
  169. assert(result() === 'STDERR')
  170. })
  171. it('run-s command', async () => {
  172. await runSeq(['test-task:stderr'])
  173. assert(result() === 'STDERR')
  174. })
  175. it('run-p command', async () => {
  176. await runPar(['test-task:stderr'])
  177. assert(result() === 'STDERR')
  178. })
  179. })
  180. describe('should be able to use `restart` built-in task:', () => {
  181. it('Node API', () => nodeApi('restart'))
  182. it('npm-run-all command', () => runAll(['restart']))
  183. it('run-s command', () => runSeq(['restart']))
  184. it('run-p command', () => runPar(['restart']))
  185. })
  186. describe('should be able to use `env` built-in task:', () => {
  187. it('Node API', () => nodeApi('env'))
  188. it('npm-run-all command', () => runAll(['env']))
  189. it('run-s command', () => runSeq(['env']))
  190. it('run-p command', () => runPar(['env']))
  191. })
  192. if (process.platform === 'win32') {
  193. describe('issue14', () => {
  194. it('Node API', () => nodeApi('test-task:issue14:win32'))
  195. it('npm-run-all command', () => runAll(['test-task:issue14:win32']))
  196. it('run-s command', () => runSeq(['test-task:issue14:win32']))
  197. it('run-p command', () => runPar(['test-task:issue14:win32']))
  198. })
  199. } else {
  200. describe('issue14', () => {
  201. it('Node API', () => nodeApi('test-task:issue14:posix'))
  202. it('npm-run-all command', () => runAll(['test-task:issue14:posix']))
  203. it('run-s command', () => runSeq(['test-task:issue14:posix']))
  204. it('run-p command', () => runPar(['test-task:issue14:posix']))
  205. })
  206. }
  207. describe('should not print log if silent option was given:', () => {
  208. it('Node API', async () => {
  209. const stdout = new BufferStream()
  210. const stderr = new BufferStream()
  211. try {
  212. await nodeApi('test-task:error', { silent: true, stdout, stderr })
  213. } catch (_err) {
  214. assert(stdout.value === '' && stderr.value === '')
  215. return
  216. }
  217. assert(false, 'Should fail.')
  218. })
  219. /**
  220. * Strip unknown istanbul's warnings.
  221. * @param {string} str - The string to be stripped.
  222. * @returns {string} The stripped string.
  223. */
  224. function stripIstanbulWarnings (str) {
  225. return str.replace(/File \[.+?] ignored, nothing could be mapped\r?\n/, '')
  226. }
  227. it('npm-run-all command', async () => {
  228. const stdout = new BufferStream()
  229. const stderr = new BufferStream()
  230. try {
  231. await runAll(['--silent', 'test-task:error'], stdout, stderr)
  232. } catch (_err) {
  233. assert(stdout.value === '' && stripIstanbulWarnings(stderr.value) === '')
  234. return
  235. }
  236. assert(false, 'Should fail.')
  237. })
  238. it('run-s command', async () => {
  239. const stdout = new BufferStream()
  240. const stderr = new BufferStream()
  241. try {
  242. await runSeq(['--silent', 'test-task:error'], stdout, stderr)
  243. } catch (_err) {
  244. assert(stdout.value === '' && stripIstanbulWarnings(stderr.value) === '')
  245. return
  246. }
  247. assert(false, 'Should fail.')
  248. })
  249. it('run-p command', async () => {
  250. const stdout = new BufferStream()
  251. const stderr = new BufferStream()
  252. try {
  253. await runPar(['--silent', 'test-task:error'], stdout, stderr)
  254. } catch (_err) {
  255. assert(stdout.value === '' && stripIstanbulWarnings(stderr.value) === '')
  256. return
  257. }
  258. assert(false, 'Should fail.')
  259. })
  260. })
  261. // https://github.com/mysticatea/npm-run-all/issues/105
  262. describe('should not print MaxListenersExceededWarning when it runs 10 tasks:', () => {
  263. const tasks = Array.from({ length: 10 }, () => 'test-task:append:a')
  264. it('npm-run-all command', async () => {
  265. const buf = new BufferStream()
  266. await runAll(tasks, null, buf)
  267. assert(buf.value.indexOf('MaxListenersExceededWarning') === -1)
  268. })
  269. it('run-s command', async () => {
  270. const buf = new BufferStream()
  271. await runSeq(tasks, null, buf)
  272. assert(buf.value.indexOf('MaxListenersExceededWarning') === -1)
  273. })
  274. it('run-p command', async () => {
  275. const buf = new BufferStream()
  276. await runPar(tasks, null, buf)
  277. assert(buf.value.indexOf('MaxListenersExceededWarning') === -1)
  278. })
  279. })
  280. })