OutputFileFinderTests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. const modulePath = require('path').join(
  16. __dirname,
  17. '../../../app/js/OutputFileFinder'
  18. )
  19. const path = require('path')
  20. const { expect } = require('chai')
  21. const { EventEmitter } = require('events')
  22. describe('OutputFileFinder', function () {
  23. beforeEach(function () {
  24. this.OutputFileFinder = SandboxedModule.require(modulePath, {
  25. requires: {
  26. fs: (this.fs = {}),
  27. child_process: { spawn: (this.spawn = sinon.stub()) }
  28. },
  29. globals: {
  30. Math // used by lodash
  31. }
  32. })
  33. this.directory = '/test/dir'
  34. return (this.callback = sinon.stub())
  35. })
  36. describe('findOutputFiles', function () {
  37. beforeEach(function () {
  38. this.resource_path = 'resource/path.tex'
  39. this.output_paths = ['output.pdf', 'extra/file.tex']
  40. this.all_paths = this.output_paths.concat([this.resource_path])
  41. this.resources = [{ path: (this.resource_path = 'resource/path.tex') }]
  42. this.OutputFileFinder._getAllFiles = sinon
  43. .stub()
  44. .callsArgWith(1, null, this.all_paths)
  45. return this.OutputFileFinder.findOutputFiles(
  46. this.resources,
  47. this.directory,
  48. (error, outputFiles) => {
  49. this.outputFiles = outputFiles
  50. }
  51. )
  52. })
  53. return it('should only return the output files, not directories or resource paths', function () {
  54. return expect(this.outputFiles).to.deep.equal([
  55. {
  56. path: 'output.pdf',
  57. type: 'pdf'
  58. },
  59. {
  60. path: 'extra/file.tex',
  61. type: 'tex'
  62. }
  63. ])
  64. })
  65. })
  66. return describe('_getAllFiles', function () {
  67. beforeEach(function () {
  68. this.proc = new EventEmitter()
  69. this.proc.stdout = new EventEmitter()
  70. this.proc.stdout.setEncoding = sinon.stub().returns(this.proc.stdout)
  71. this.spawn.returns(this.proc)
  72. this.directory = '/base/dir'
  73. return this.OutputFileFinder._getAllFiles(this.directory, this.callback)
  74. })
  75. describe('successfully', function () {
  76. beforeEach(function () {
  77. this.proc.stdout.emit(
  78. 'data',
  79. ['/base/dir/main.tex', '/base/dir/chapters/chapter1.tex'].join('\n') +
  80. '\n'
  81. )
  82. return this.proc.emit('close', 0)
  83. })
  84. return it('should call the callback with the relative file paths', function () {
  85. return this.callback
  86. .calledWith(null, ['main.tex', 'chapters/chapter1.tex'])
  87. .should.equal(true)
  88. })
  89. })
  90. return describe("when the directory doesn't exist", function () {
  91. beforeEach(function () {
  92. return this.proc.emit('close', 1)
  93. })
  94. return it('should call the callback with a blank array', function () {
  95. return this.callback.calledWith(null, []).should.equal(true)
  96. })
  97. })
  98. })
  99. })