exec-sh.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* global describe, it, beforeEach, afterEach */
  2. var execSh = require('..')
  3. var assert = require('assert')
  4. var sinon = require('sinon')
  5. var merge = require('merge')
  6. var cp = require('child_process')
  7. describe('exec-sh', function () {
  8. describe('module.exports', function () {
  9. it('should export a single function', function () {
  10. assert.strictEqual(typeof execSh, 'function')
  11. })
  12. })
  13. describe('#execSh() arguments', function () {
  14. var spawn, exitCode, stream
  15. stream = {
  16. on: function (e, c) {
  17. if (e === 'data') {
  18. // execute callback two times to check if stream
  19. // aggregation works correctly
  20. c('1')
  21. c('2')
  22. }
  23. }
  24. }
  25. beforeEach(function () {
  26. exitCode = 0
  27. spawn = sinon.stub(cp, 'spawn')
  28. spawn.returns({
  29. spawn_return: true,
  30. on: function (e, c) {
  31. if (e === 'close') {
  32. c(exitCode)
  33. }
  34. },
  35. stdout: stream,
  36. stderr: stream
  37. })
  38. })
  39. afterEach(function () {
  40. cp.spawn.restore()
  41. })
  42. it('should pass command to spawn function', function () {
  43. execSh('command')
  44. sinon.assert.calledOnce(spawn)
  45. assert.strictEqual('command', spawn.getCall(0).args[1][1])
  46. })
  47. it('should accept array of commands to run', function () {
  48. execSh(['command1', 'command2'])
  49. sinon.assert.calledOnce(spawn)
  50. assert.strictEqual('command1;command2', spawn.getCall(0).args[1][1])
  51. })
  52. it('should accept true as options argument', function () {
  53. execSh('command', true)
  54. sinon.assert.calledOnce(spawn)
  55. assert.strictEqual(spawn.getCall(0).args[2].stdio, null)
  56. })
  57. it('should merge defaults with options', function () {
  58. execSh('command')
  59. var defOptionsClone = merge(true, spawn.getCall(0).args[2])
  60. var options = { key: 'value' }
  61. execSh('command', options)
  62. assert.deepEqual(spawn.getCall(1).args[2], merge(true, defOptionsClone, options))
  63. // change value of the fist property in default options to null
  64. assert.ok(Object.keys(defOptionsClone).length > 0)
  65. defOptionsClone[Object.keys(defOptionsClone)[0]] = null
  66. execSh('command', defOptionsClone)
  67. assert.deepEqual(spawn.getCall(2).args[2], defOptionsClone)
  68. })
  69. it("should accept optional 'callback' parameter", function () {
  70. var callback = sinon.spy()
  71. execSh('command', callback)
  72. execSh('command', { key: 'value' }, callback)
  73. sinon.assert.callCount(callback, 2)
  74. })
  75. it("should use 'cmd /C' command prefix on windows", function () {
  76. var platform = process.platform
  77. Object.defineProperty(process, 'platform', { value: 'win32' })
  78. execSh('command')
  79. Object.defineProperty(process, 'platform', { value: platform })
  80. sinon.assert.calledOnce(spawn)
  81. assert.strictEqual(spawn.getCall(0).args[0], 'cmd')
  82. })
  83. it("should use 'sh -c' command prefix on *nix", function () {
  84. var platform = process.platform
  85. process.platform = 'linux'
  86. execSh('command')
  87. process.platform = platform
  88. sinon.assert.calledOnce(spawn)
  89. assert.strictEqual(spawn.getCall(0).args[1][0], '-c')
  90. assert.strictEqual(spawn.getCall(0).args[0], 'sh')
  91. })
  92. it('should return spawn() result', function () {
  93. assert(execSh('command').spawn_return)
  94. })
  95. it('should aggregate stdoout and stderr', function (done) {
  96. execSh('command', function (_err, stdout, stderr) {
  97. assert.strictEqual(stdout, '12')
  98. assert.strictEqual(stderr, '12')
  99. done()
  100. })
  101. })
  102. it('should catch exceptions thrown by spawn', function (done) {
  103. spawn.throws()
  104. execSh('command', function (err, stdout, stderr) {
  105. assert(err instanceof Error)
  106. done()
  107. })
  108. })
  109. it('should return empty stdout and stderr when spawn throws', function (done) {
  110. spawn.throws()
  111. stream = null
  112. execSh('command', function (_err, stdout, stderr) {
  113. assert.strictEqual(stderr, '')
  114. assert.strictEqual(stdout, '')
  115. done()
  116. })
  117. })
  118. it('should run callback with error when shell exit with non-zero code', function (done) {
  119. exitCode = 1
  120. execSh('command', function (err) {
  121. assert(err instanceof Error)
  122. assert.equal(exitCode, err.code)
  123. done()
  124. })
  125. })
  126. })
  127. })