cscript.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* eslint-disable consistent-this */
  2. var should = require('should')
  3. var cscript = require('../lib/cscript.js')
  4. describe('cscript', function () {
  5. var mockFs, mockExecFile
  6. it('must be initialized', function () {
  7. (function () {
  8. cscript.path()
  9. }).should.throw('must initialize first')
  10. })
  11. it('if cscript.exe is successfully spawned then no more checks are conducted', function (done) {
  12. mockExecFile['cscript.exe'].calls.should.eql(0)
  13. mockExecFile['cscript.exe'].stdout = cscript.CSCRIPT_EXPECTED_OUTPUT
  14. cscript.init(function (err) {
  15. if (err) {
  16. return done(err)
  17. }
  18. mockExecFile['cscript.exe'].calls.should.eql(1)
  19. mockExecFile['cscript.exe'].args[0].should.eql('cscript.exe')
  20. done()
  21. })
  22. })
  23. it('initializes only once', function (done) {
  24. mockExecFile['cscript.exe'].calls.should.eql(0)
  25. mockExecFile['cscript.exe'].stdout = cscript.CSCRIPT_EXPECTED_OUTPUT
  26. cscript.init(function (err) {
  27. if (err) {
  28. return done(err)
  29. }
  30. mockExecFile['cscript.exe'].calls.should.eql(1)
  31. cscript.init(function (err) {
  32. if (err) {
  33. return done(err)
  34. }
  35. mockExecFile['cscript.exe'].calls.should.eql(1)
  36. mockExecFile['where cscript.exe'].calls.should.eql(0)
  37. done()
  38. })
  39. })
  40. })
  41. it('if cscript.exe fails to execute, try to run "where cscript.exe"', function (done) {
  42. mockExecFile['cscript.exe'].calls.should.eql(0)
  43. mockExecFile['where cscript.exe'].calls.should.eql(0)
  44. mockExecFile['cscript.exe'].err = new Error()
  45. mockExecFile['cscript.exe'].err.code = 'ENOENT'
  46. mockExecFile['where cscript.exe'].stdout = '123'
  47. cscript.init(function (err) {
  48. if (err) {
  49. return done(err)
  50. }
  51. mockExecFile['cscript.exe'].calls.should.eql(1)
  52. mockExecFile['where cscript.exe'].calls.should.eql(1)
  53. cscript.path().should.eql('123')
  54. done()
  55. })
  56. })
  57. beforeEach(function () {
  58. mockFs = {
  59. err: null,
  60. calls: 0,
  61. stat: function (name, cb) {
  62. this.calls++
  63. var self = this
  64. setImmediate(function () {
  65. cb(self.err, {})
  66. })
  67. },
  68. }
  69. mockExecFile = function(command, args, options, callback) {
  70. if (!mockExecFile[command]) {
  71. throw new Error('unexpected command ' + command)
  72. }
  73. mockExecFile[command].args = arguments
  74. mockExecFile[command].calls++
  75. if (typeof args === 'function') {
  76. callback = args
  77. args = undefined
  78. options = undefined
  79. }
  80. if (typeof options === 'function') {
  81. callback = options
  82. args = undefined
  83. options = undefined
  84. }
  85. if (typeof callback !== 'function') {
  86. throw new Error('missing callback')
  87. }
  88. setImmediate(function () {
  89. callback(mockExecFile[command].err, mockExecFile[command].stdout, mockExecFile[command].stderr)
  90. })
  91. }
  92. mockExecFile['cscript.exe'] = {calls: 0, stdout: '', stderr: '', err: null}
  93. mockExecFile['where cscript.exe'] = {calls: 0, stdout: '', stderr: '', err: null}
  94. cscript._mock(mockFs, mockExecFile, false)
  95. })
  96. afterEach(function () {
  97. cscript._mockReset()
  98. })
  99. })