OutputFileFinderTests.js 2.9 KB

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