OutputFileFinderTests.js 2.7 KB

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