OutputFileFinderTests.js 3.3 KB

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