OutputFileFinderTests.coffee 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/OutputFileFinder'
  5. path = require "path"
  6. expect = require("chai").expect
  7. EventEmitter = require("events").EventEmitter
  8. describe "OutputFileFinder", ->
  9. beforeEach ->
  10. @OutputFileFinder = SandboxedModule.require modulePath, requires:
  11. "fs": @fs = {}
  12. "child_process": spawn: @spawn = sinon.stub()
  13. "logger-sharelatex": { log: sinon.stub(), warn: sinon.stub() }
  14. @directory = "/test/dir"
  15. @callback = sinon.stub()
  16. describe "findOutputFiles", ->
  17. beforeEach ->
  18. @resource_path = "resource/path.tex"
  19. @output_paths = ["output.pdf", "extra/file.tex"]
  20. @all_paths = @output_paths.concat [@resource_path]
  21. @resources = [
  22. path: @resource_path = "resource/path.tex"
  23. ]
  24. @OutputFileFinder._getAllFiles = sinon.stub().callsArgWith(1, null, @all_paths)
  25. @OutputFileFinder.findOutputFiles @resources, @directory, (error, @outputFiles) =>
  26. it "should only return the output files, not directories or resource paths", ->
  27. expect(@outputFiles).to.deep.equal [{
  28. path: "output.pdf"
  29. type: "pdf"
  30. }, {
  31. path: "extra/file.tex",
  32. type: "tex"
  33. }]
  34. describe "_getAllFiles", ->
  35. beforeEach ->
  36. @proc = new EventEmitter()
  37. @proc.stdout = new EventEmitter()
  38. @spawn.returns @proc
  39. @directory = "/base/dir"
  40. @OutputFileFinder._getAllFiles @directory, @callback
  41. describe "successfully", ->
  42. beforeEach ->
  43. @proc.stdout.emit(
  44. "data",
  45. ["/base/dir/main.tex", "/base/dir/chapters/chapter1.tex"].join("\n") + "\n"
  46. )
  47. @proc.emit "close", 0
  48. it "should call the callback with the relative file paths", ->
  49. @callback.calledWith(
  50. null,
  51. ["main.tex", "chapters/chapter1.tex"]
  52. ).should.equal true
  53. describe "when the directory doesn't exist", ->
  54. beforeEach ->
  55. @proc.emit "close", 1
  56. it "should call the callback with a blank array", ->
  57. @callback.calledWith(
  58. null,
  59. []
  60. ).should.equal true