OutputFileFinderTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const SandboxedModule = require('sandboxed-module')
  13. const sinon = require('sinon')
  14. const modulePath = require('path').join(
  15. __dirname,
  16. '../../../app/js/OutputFileFinder'
  17. )
  18. const path = require('path')
  19. const { expect } = require('chai')
  20. const { EventEmitter } = require('events')
  21. describe('OutputFileFinder', function () {
  22. beforeEach(function () {
  23. this.OutputFileFinder = SandboxedModule.require(modulePath, {
  24. requires: {
  25. fs: (this.fs = {}),
  26. child_process: { spawn: (this.spawn = sinon.stub()) },
  27. },
  28. globals: {
  29. Math, // used by lodash
  30. },
  31. })
  32. this.directory = '/test/dir'
  33. return (this.callback = sinon.stub())
  34. })
  35. describe('findOutputFiles', function () {
  36. beforeEach(function (done) {
  37. this.resource_path = 'resource/path.tex'
  38. this.output_paths = ['output.pdf', 'extra/file.tex']
  39. this.all_paths = this.output_paths.concat([this.resource_path])
  40. this.resources = [{ path: (this.resource_path = 'resource/path.tex') }]
  41. this.OutputFileFinder._getAllFiles = sinon
  42. .stub()
  43. .callsArgWith(1, null, this.all_paths)
  44. return this.OutputFileFinder.findOutputFiles(
  45. this.resources,
  46. this.directory,
  47. (error, outputFiles) => {
  48. if (error) return done(error)
  49. this.outputFiles = outputFiles
  50. done()
  51. }
  52. )
  53. })
  54. return it('should only return the output files, not directories or resource paths', function () {
  55. return expect(this.outputFiles).to.deep.equal([
  56. {
  57. path: 'output.pdf',
  58. type: 'pdf',
  59. },
  60. {
  61. path: 'extra/file.tex',
  62. type: 'tex',
  63. },
  64. ])
  65. })
  66. })
  67. return describe('_getAllFiles', function () {
  68. beforeEach(function () {
  69. this.proc = new EventEmitter()
  70. this.proc.stdout = new EventEmitter()
  71. this.proc.stdout.setEncoding = sinon.stub().returns(this.proc.stdout)
  72. this.spawn.returns(this.proc)
  73. this.directory = '/base/dir'
  74. return this.OutputFileFinder._getAllFiles(this.directory, this.callback)
  75. })
  76. describe('successfully', function () {
  77. beforeEach(function () {
  78. this.proc.stdout.emit(
  79. 'data',
  80. ['/base/dir/main.tex', '/base/dir/chapters/chapter1.tex'].join('\n') +
  81. '\n'
  82. )
  83. return this.proc.emit('close', 0)
  84. })
  85. return it('should call the callback with the relative file paths', function () {
  86. return this.callback
  87. .calledWith(null, ['main.tex', 'chapters/chapter1.tex'])
  88. .should.equal(true)
  89. })
  90. })
  91. return describe("when the directory doesn't exist", function () {
  92. beforeEach(function () {
  93. return this.proc.emit('close', 1)
  94. })
  95. return it('should call the callback with a blank array', function () {
  96. return this.callback.calledWith(null, []).should.equal(true)
  97. })
  98. })
  99. })
  100. })