OutputFileFinderTests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. })
  32. this.directory = '/test/dir'
  33. return (this.callback = sinon.stub())
  34. })
  35. describe('findOutputFiles', function() {
  36. beforeEach(function() {
  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. this.outputFiles = outputFiles
  49. }
  50. )
  51. })
  52. return it('should only return the output files, not directories or resource paths', function() {
  53. return expect(this.outputFiles).to.deep.equal([
  54. {
  55. path: 'output.pdf',
  56. type: 'pdf'
  57. },
  58. {
  59. path: 'extra/file.tex',
  60. type: 'tex'
  61. }
  62. ])
  63. })
  64. })
  65. return describe('_getAllFiles', function() {
  66. beforeEach(function() {
  67. this.proc = new EventEmitter()
  68. this.proc.stdout = new EventEmitter()
  69. this.spawn.returns(this.proc)
  70. this.directory = '/base/dir'
  71. return this.OutputFileFinder._getAllFiles(this.directory, this.callback)
  72. })
  73. describe('successfully', function() {
  74. beforeEach(function() {
  75. this.proc.stdout.emit(
  76. 'data',
  77. ['/base/dir/main.tex', '/base/dir/chapters/chapter1.tex'].join('\n') +
  78. '\n'
  79. )
  80. return this.proc.emit('close', 0)
  81. })
  82. return it('should call the callback with the relative file paths', function() {
  83. return this.callback
  84. .calledWith(null, ['main.tex', 'chapters/chapter1.tex'])
  85. .should.equal(true)
  86. })
  87. })
  88. return describe("when the directory doesn't exist", function() {
  89. beforeEach(function() {
  90. return this.proc.emit('close', 1)
  91. })
  92. return it('should call the callback with a blank array', function() {
  93. return this.callback.calledWith(null, []).should.equal(true)
  94. })
  95. })
  96. })
  97. })