argument-placeholders.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { strictEqual } = assert
  12. const nodeApi = require('../lib')
  13. const { result, removeResult, runAll, runPar, runSeq } = require('./lib/util')
  14. // ------------------------------------------------------------------------------
  15. // Test
  16. // ------------------------------------------------------------------------------
  17. describe('[argument-placeholders]', () => {
  18. before(() => process.chdir('test-workspace'))
  19. after(() => process.chdir('..'))
  20. beforeEach(removeResult)
  21. describe("If arguments preceded by '--' are nothing, '{1}' should be empty:", () => {
  22. it('Node API', () =>
  23. nodeApi('test-task:dump {1}')
  24. .then(() => strictEqual(result(), '[]')))
  25. it('npm-run-all command', () =>
  26. runAll(['test-task:dump {1}'])
  27. .then(() => strictEqual(result(), '[]')))
  28. it("npm-run-all command (only '--' exists)", () =>
  29. runAll(['test-task:dump {1}', '--'])
  30. .then(() => strictEqual(result(), '[]')))
  31. it('run-s command', () =>
  32. runSeq(['test-task:dump {1}'])
  33. .then(() => strictEqual(result(), '[]')))
  34. it("run-s command (only '--' exists)", () =>
  35. runSeq(['test-task:dump {1}', '--'])
  36. .then(() => strictEqual(result(), '[]')))
  37. it('run-p command', () =>
  38. runPar(['test-task:dump {1}'])
  39. .then(() => strictEqual(result(), '[]')))
  40. it("run-p command (only '--' exists)", () =>
  41. runPar(['test-task:dump {1}', '--'])
  42. .then(() => strictEqual(result(), '[]')))
  43. })
  44. describe("'{1}' should be replaced by the 1st argument preceded by '--':", () => {
  45. it('Node API', () =>
  46. nodeApi('test-task:dump {1}', { arguments: ['1st', '2nd'] })
  47. .then(() => strictEqual(result(), '["1st"]')))
  48. it('npm-run-all command', () =>
  49. runAll(['test-task:dump {1}', '--', '1st', '2nd'])
  50. .then(() => strictEqual(result(), '["1st"]')))
  51. it('run-s command', () =>
  52. runSeq(['test-task:dump {1}', '--', '1st', '2nd'])
  53. .then(() => strictEqual(result(), '["1st"]')))
  54. it('run-p command', () =>
  55. runPar(['test-task:dump {1}', '--', '1st', '2nd'])
  56. .then(() => strictEqual(result(), '["1st"]')))
  57. })
  58. describe("'{2}' should be replaced by the 2nd argument preceded by '--':", () => {
  59. it('Node API', () =>
  60. nodeApi('test-task:dump {2}', { arguments: ['1st', '2nd'] })
  61. .then(() => strictEqual(result(), '["2nd"]')))
  62. it('npm-run-all command', () =>
  63. runAll(['test-task:dump {2}', '--', '1st', '2nd'])
  64. .then(() => strictEqual(result(), '["2nd"]')))
  65. it('run-s command', () =>
  66. runSeq(['test-task:dump {2}', '--', '1st', '2nd'])
  67. .then(() => strictEqual(result(), '["2nd"]')))
  68. it('run-p command', () =>
  69. runPar(['test-task:dump {2}', '--', '1st', '2nd'])
  70. .then(() => strictEqual(result(), '["2nd"]')))
  71. })
  72. describe("'{@}' should be replaced by the every argument preceded by '--':", () => {
  73. it('Node API', () =>
  74. nodeApi('test-task:dump {@}', { arguments: ['1st', '2nd'] })
  75. .then(() => strictEqual(result(), '["1st","2nd"]')))
  76. it('npm-run-all command', () =>
  77. runAll(['test-task:dump {@}', '--', '1st', '2nd'])
  78. .then(() => strictEqual(result(), '["1st","2nd"]')))
  79. it('run-s command', () =>
  80. runSeq(['test-task:dump {@}', '--', '1st', '2nd'])
  81. .then(() => strictEqual(result(), '["1st","2nd"]')))
  82. it('run-p command', () =>
  83. runPar(['test-task:dump {@}', '--', '1st', '2nd'])
  84. .then(() => strictEqual(result(), '["1st","2nd"]')))
  85. })
  86. describe("'{*}' should be replaced by the all arguments preceded by '--':", () => {
  87. it('Node API', () =>
  88. nodeApi('test-task:dump {*}', { arguments: ['1st', '2nd'] })
  89. .then(() => strictEqual(result(), '["1st 2nd"]')))
  90. it('npm-run-all command', () =>
  91. runAll(['test-task:dump {*}', '--', '1st', '2nd'])
  92. .then(() => strictEqual(result(), '["1st 2nd"]')))
  93. it('run-s command', () =>
  94. runSeq(['test-task:dump {*}', '--', '1st', '2nd'])
  95. .then(() => strictEqual(result(), '["1st 2nd"]')))
  96. it('run-p command', () =>
  97. runPar(['test-task:dump {*}', '--', '1st', '2nd'])
  98. .then(() => strictEqual(result(), '["1st 2nd"]')))
  99. })
  100. describe("'{%}' should be unfolded into one command for each argument following '--':", () => {
  101. it('Node API', () =>
  102. nodeApi('test-task:dump {%}', { arguments: ['1st', '2nd'] })
  103. .then(() => strictEqual(result(), '["1st"]["2nd"]')))
  104. it('npm-run-all command', () =>
  105. runAll(['test-task:dump {%}', '--', '1st', '2nd'])
  106. .then(() => strictEqual(result(), '["1st"]["2nd"]')))
  107. it('run-s command', () =>
  108. runSeq(['test-task:dump {%}', '--', '1st', '2nd'])
  109. .then(() => strictEqual(result(), '["1st"]["2nd"]')))
  110. it('run-p command', () =>
  111. runPar(['test-task:dump {%}', '--', '1st', '2nd'])
  112. .then(() => {
  113. const value = result()
  114. assert(value === '["1st"]["2nd"]' || value === '["2nd"]["1st"]')
  115. }))
  116. })
  117. describe("Every '{1}', '{2}', '{@}' and '{*}' should be replaced by the arguments preceded by '--':", () => {
  118. it('Node API', () =>
  119. nodeApi('test-task:dump {1} {2} {3} {@} {*}', { arguments: ['1st', '2nd'] })
  120. .then(() => strictEqual(result(), '["1st","2nd","1st","2nd","1st 2nd"]')))
  121. it('npm-run-all command', () =>
  122. runAll(['test-task:dump {1} {2} {3} {@} {*}', '--', '1st', '2nd'])
  123. .then(() => strictEqual(result(), '["1st","2nd","1st","2nd","1st 2nd"]')))
  124. it('run-s command', () =>
  125. runSeq(['test-task:dump {1} {2} {3} {@} {*}', '--', '1st', '2nd'])
  126. .then(() => strictEqual(result(), '["1st","2nd","1st","2nd","1st 2nd"]')))
  127. it('run-p command', () =>
  128. runPar(['test-task:dump {1} {2} {3} {@} {*}', '--', '1st', '2nd'])
  129. .then(() => strictEqual(result(), '["1st","2nd","1st","2nd","1st 2nd"]')))
  130. })
  131. describe("'{1:-foo}' should be replaced by 'foo' if arguments are nothing:", () => {
  132. it('Node API', () =>
  133. nodeApi('test-task:dump {1:-foo} {1}')
  134. .then(() => strictEqual(result(), '["foo"]')))
  135. it('npm-run-all command', () =>
  136. runAll(['test-task:dump {1:-foo} {1}'])
  137. .then(() => strictEqual(result(), '["foo"]')))
  138. it('run-s command', () =>
  139. runSeq(['test-task:dump {1:-foo} {1}'])
  140. .then(() => strictEqual(result(), '["foo"]')))
  141. it('run-p command', () =>
  142. runPar(['test-task:dump {1:-foo} {1}'])
  143. .then(() => strictEqual(result(), '["foo"]')))
  144. })
  145. describe("'{1:=foo}' should be replaced by 'foo' and should affect following '{1}' if arguments are nothing:", () => {
  146. it('Node API', () =>
  147. nodeApi('test-task:dump {1:=foo} {1}')
  148. .then(() => strictEqual(result(), '["foo","foo"]')))
  149. it('npm-run-all command', () =>
  150. runAll(['test-task:dump {1:=foo} {1}'])
  151. .then(() => strictEqual(result(), '["foo","foo"]')))
  152. it('run-s command', () =>
  153. runSeq(['test-task:dump {1:=foo} {1}'])
  154. .then(() => strictEqual(result(), '["foo","foo"]')))
  155. it('run-p command', () =>
  156. runPar(['test-task:dump {1:=foo} {1}'])
  157. .then(() => strictEqual(result(), '["foo","foo"]')))
  158. })
  159. })