sequential.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 spawnWithKill = require('./lib/spawn-with-kill')
  13. const { delay, result, removeResult, runAll, runSeq } = require('./lib/util')
  14. // ------------------------------------------------------------------------------
  15. // Test
  16. // ------------------------------------------------------------------------------
  17. describe('[sequencial] npm-run-all', () => {
  18. before(() => process.chdir('test-workspace'))
  19. after(() => process.chdir('..'))
  20. beforeEach(() => delay(1000).then(removeResult))
  21. describe('should run tasks sequentially:', () => {
  22. it('Node API', async () => {
  23. const results = await nodeApi(['test-task:append a', 'test-task:append b'], { parallel: false })
  24. assert(results.length === 2)
  25. assert(results[0].name === 'test-task:append a')
  26. assert(results[0].code === 0)
  27. assert(results[1].name === 'test-task:append b')
  28. assert(results[1].code === 0)
  29. assert(result() === 'aabb')
  30. })
  31. it('npm-run-all command', async () => {
  32. await runAll(['test-task:append a', 'test-task:append b'])
  33. assert(result() === 'aabb')
  34. })
  35. it('run-s command', async () => {
  36. await runSeq(['test-task:append a', 'test-task:append b'])
  37. assert(result() === 'aabb')
  38. })
  39. })
  40. describe('should not run subsequent tasks if a task exited with a non-zero code:', () => {
  41. it('Node API', async () => {
  42. try {
  43. await nodeApi(['test-task:append2 a', 'test-task:error', 'test-task:append2 b'])
  44. } catch (err) {
  45. assert(err.results.length === 3)
  46. assert(err.results[0].name === 'test-task:append2 a')
  47. assert(err.results[0].code === 0)
  48. assert(err.results[1].name === 'test-task:error')
  49. assert(err.results[1].code === 1)
  50. assert(err.results[2].name === 'test-task:append2 b')
  51. assert(err.results[2].code === undefined)
  52. assert(result() === 'aa')
  53. return
  54. }
  55. assert(false, 'should fail')
  56. })
  57. it('npm-run-all command', async () => {
  58. try {
  59. await runAll(['test-task:append2 a', 'test-task:error', 'test-task:append2 b'])
  60. } catch (_err) {
  61. assert(result() === 'aa')
  62. return
  63. }
  64. assert(false, 'should fail')
  65. })
  66. it('run-s command', async () => {
  67. try {
  68. await runSeq(['test-task:append2 a', 'test-task:error', 'test-task:append2 b'])
  69. } catch (_err) {
  70. assert(result() === 'aa')
  71. return
  72. }
  73. assert(false, 'should fail')
  74. })
  75. })
  76. describe('should remove intersected tasks from two or more patterns:', () => {
  77. it('Node API', async () => {
  78. await nodeApi(['test-task:*:a', '*:append:a'], { parallel: false })
  79. assert(result() === 'aa')
  80. })
  81. it('npm-run-all command', async () => {
  82. await runAll(['test-task:*:a', '*:append:a'])
  83. assert(result() === 'aa')
  84. })
  85. it('run-s command', async () => {
  86. await runSeq(['test-task:*:a', '*:append:a'])
  87. assert(result() === 'aa')
  88. })
  89. })
  90. describe('should not remove duplicate tasks from two or more the same pattern:', () => {
  91. it('Node API', async () => {
  92. await nodeApi(['test-task:*:a', 'test-task:*:a'], { parallel: false })
  93. assert(result() === 'aaaa')
  94. })
  95. it('npm-run-all command', async () => {
  96. await runAll(['test-task:*:a', 'test-task:*:a'])
  97. assert(result() === 'aaaa')
  98. })
  99. it('run-s command', async () => {
  100. await runSeq(['test-task:*:a', 'test-task:*:a'])
  101. assert(result() === 'aaaa')
  102. })
  103. })
  104. describe("should kill child processes when it's killed", () => {
  105. it('npm-run-all command', async () => {
  106. await spawnWithKill(
  107. 'node',
  108. ['../bin/npm-run-all.js', 'test-task:append2 a']
  109. )
  110. assert(result() == null || result() === 'a')
  111. })
  112. it('run-s command', async () => {
  113. await spawnWithKill(
  114. 'node',
  115. ['../bin/run-s/index.js', 'test-task:append2 a']
  116. )
  117. assert(result() == null || result() === 'a')
  118. })
  119. })
  120. describe('should continue on error when --continue-on-error option was specified:', () => {
  121. it('Node API', async () => {
  122. try {
  123. await nodeApi(['test-task:append a', 'test-task:error', 'test-task:append b'], { continueOnError: true })
  124. } catch (_err) {
  125. console.log(result()) // TODO: Spurious failures windows
  126. assert(result() === 'aabb')
  127. return
  128. }
  129. assert(false, 'should fail')
  130. })
  131. it('npm-run-all command (--continue-on-error)', async () => {
  132. try {
  133. await runAll(['--continue-on-error', 'test-task:append a', 'test-task:error', 'test-task:append b'])
  134. } catch (_err) {
  135. assert(result() === 'aabb')
  136. return
  137. }
  138. assert(false, 'should fail')
  139. })
  140. it('run-s command (--continue-on-error)', async () => {
  141. try {
  142. await runSeq(['--continue-on-error', 'test-task:append a', 'test-task:error', 'test-task:append b'])
  143. } catch (_err) {
  144. assert(result() === 'aabb')
  145. return
  146. }
  147. assert(false, 'should fail')
  148. })
  149. it('npm-run-all command (-c)', async () => {
  150. try {
  151. await runAll(['-c', 'test-task:append a', 'test-task:error', 'test-task:append b'])
  152. } catch (_err) {
  153. assert(result() === 'aabb')
  154. return
  155. }
  156. assert(false, 'should fail')
  157. })
  158. it('run-s command (-c)', async () => {
  159. try {
  160. await runSeq(['-c', 'test-task:append a', 'test-task:error', 'test-task:append b'])
  161. } catch (_err) {
  162. assert(result() === 'aabb')
  163. return
  164. }
  165. assert(false, 'should fail')
  166. })
  167. })
  168. })