fail.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 { delay, removeResult, runAll, runPar, runSeq } = require('./lib/util')
  13. // ------------------------------------------------------------------------------
  14. // Helpers
  15. // ------------------------------------------------------------------------------
  16. /**
  17. * Throws an assertion error if a given promise comes to be fulfilled.
  18. *
  19. * @param {Promise} p - A promise to check.
  20. * @returns {Promise} A promise which is checked.
  21. */
  22. function shouldFail (p) {
  23. return p.then(
  24. () => assert(false, 'should fail'),
  25. () => null // OK!
  26. )
  27. }
  28. // ------------------------------------------------------------------------------
  29. // Test
  30. // ------------------------------------------------------------------------------
  31. describe('[fail] it should fail', () => {
  32. before(() => process.chdir('test-workspace'))
  33. after(() => process.chdir('..'))
  34. beforeEach(removeResult)
  35. afterEach(() => delay(1000))
  36. describe('if an invalid option exists.', () => {
  37. it('npm-run-all command', () => shouldFail(runAll(['--invalid'])))
  38. it('run-s command', () => shouldFail(runSeq(['--parallel'])))
  39. it('run-p command', () => shouldFail(runPar(['--sequential'])))
  40. it('npm-run-all command with --race without --parallel', () => shouldFail(runAll(['--race'])))
  41. it('npm-run-all command with --r without --parallel', () => shouldFail(runAll(['--r'])))
  42. it('run-s command with --race', () => shouldFail(runSeq(['--race'])))
  43. it('run-s command with --r', () => shouldFail(runSeq(['--r'])))
  44. })
  45. describe('if invalid `options.taskList` is given.', () => {
  46. it('Node API', () => shouldFail(nodeApi('test-task:append a', { taskList: { invalid: 0 } })))
  47. })
  48. describe('if unknown tasks are given:', () => {
  49. it('Node API', () => shouldFail(nodeApi('unknown-task')))
  50. it('npm-run-all command', () => shouldFail(runAll(['unknown-task'])))
  51. it('run-s command', () => shouldFail(runSeq(['unknown-task'])))
  52. it('run-p command', () => shouldFail(runPar(['unknown-task'])))
  53. })
  54. describe('if unknown tasks are given (2):', () => {
  55. it('Node API', () => shouldFail(nodeApi(['test-task:append:a', 'unknown-task'])))
  56. it('npm-run-all command', () => shouldFail(runAll(['test-task:append:a', 'unknown-task'])))
  57. it('run-s command', () => shouldFail(runSeq(['test-task:append:a', 'unknown-task'])))
  58. it('run-p command', () => shouldFail(runPar(['test-task:append:a', 'unknown-task'])))
  59. })
  60. describe('if package.json is not found:', () => {
  61. before(() => process.chdir('no-package-json'))
  62. after(() => process.chdir('..'))
  63. it('Node API', () => shouldFail(nodeApi(['test-task:append:a'])))
  64. it('npm-run-all command', () => shouldFail(runAll(['test-task:append:a'])))
  65. it('run-s command', () => shouldFail(runSeq(['test-task:append:a'])))
  66. it('run-p command', () => shouldFail(runPar(['test-task:append:a'])))
  67. })
  68. describe('if package.json does not have scripts field:', () => {
  69. before(() => process.chdir('no-scripts'))
  70. after(() => process.chdir('..'))
  71. it('Node API', () => shouldFail(nodeApi(['test-task:append:a'])))
  72. it('npm-run-all command', () => shouldFail(runAll(['test-task:append:a'])))
  73. it('run-s command', () => shouldFail(runSeq(['test-task:append:a'])))
  74. it('run-p command', () => shouldFail(runPar(['test-task:append:a'])))
  75. })
  76. describe('if tasks exited with non-zero code:', () => {
  77. it('Node API', () => shouldFail(nodeApi('test-task:error')))
  78. it('npm-run-all command', () => shouldFail(runAll(['test-task:error'])))
  79. it('run-s command', () => shouldFail(runSeq(['test-task:error'])))
  80. it('run-p command', () => shouldFail(runPar(['test-task:error'])))
  81. })
  82. describe('if tasks exited via a signal:', () => {
  83. it('Node API', () => shouldFail(nodeApi('test-task:abort')))
  84. it('npm-run-all command', () => shouldFail(runAll(['test-task:abort'])))
  85. it('run-s command', () => shouldFail(runSeq(['test-task:abort'])))
  86. it('run-p command', () => shouldFail(runPar(['test-task:abort'])))
  87. it('with correct exit code', () => nodeApi('test-task:abort').then(() =>
  88. assert(false, 'should fail')).catch(err => {
  89. // In NodeJS versions > 6, the child process correctly sends back
  90. // the signal + code of null. In NodeJS versions <= 6, the child
  91. // process does not set the signal, and sets the code to 1.
  92. const code = Number(process.version.match(/^v(\d+)/)[1]) > 6 ? 134 : 1
  93. assert(err.code === code, 'should have correct exit code')
  94. }))
  95. })
  96. })