ResourceWriterTests.coffee 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. SandboxedModule = require('sandboxed-module')
  2. sinon = require('sinon')
  3. should = require('chai').should()
  4. modulePath = require('path').join __dirname, '../../../app/js/ResourceWriter'
  5. path = require "path"
  6. describe "ResourceWriter", ->
  7. beforeEach ->
  8. @ResourceWriter = SandboxedModule.require modulePath, requires:
  9. "fs": @fs = { mkdir: sinon.stub().callsArg(1) }
  10. "wrench": @wrench = {}
  11. "./UrlCache" : @UrlCache = {}
  12. "mkdirp" : @mkdirp = sinon.stub().callsArg(1)
  13. "./OutputFileFinder": @OutputFileFinder = {}
  14. "./Metrics": @Metrics =
  15. Timer: class Timer
  16. done: sinon.stub()
  17. @project_id = "project-id-123"
  18. @basePath = "/path/to/write/files/to"
  19. @callback = sinon.stub()
  20. describe "syncResourcesToDisk", ->
  21. beforeEach ->
  22. @resources = [
  23. "resource-1-mock"
  24. "resource-2-mock"
  25. "resource-3-mock"
  26. ]
  27. @ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
  28. @ResourceWriter._removeExtraneousFiles = sinon.stub().callsArg(2)
  29. @ResourceWriter.syncResourcesToDisk(@project_id, @resources, @basePath, @callback)
  30. it "should remove old files", ->
  31. @ResourceWriter._removeExtraneousFiles
  32. .calledWith(@resources, @basePath)
  33. .should.equal true
  34. it "should write each resource to disk", ->
  35. for resource in @resources
  36. @ResourceWriter._writeResourceToDisk
  37. .calledWith(@project_id, resource, @basePath)
  38. .should.equal true
  39. it "should call the callback", ->
  40. @callback.called.should.equal true
  41. describe "_removeExtraneousFiles", ->
  42. beforeEach ->
  43. @output_files = [{
  44. path: "output.pdf"
  45. type: "pdf"
  46. }, {
  47. path: "extra/file.tex"
  48. type: "tex"
  49. }, {
  50. path: "extra.aux"
  51. type: "aux"
  52. }, {
  53. path: "cache/_chunk1"
  54. }]
  55. @resources = "mock-resources"
  56. @OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
  57. @ResourceWriter._deleteFileIfNotDirectory = sinon.stub().callsArg(1)
  58. @ResourceWriter._removeExtraneousFiles(@resources, @basePath, @callback)
  59. it "should find the existing output files", ->
  60. @OutputFileFinder.findOutputFiles
  61. .calledWith(@resources, @basePath)
  62. .should.equal true
  63. it "should delete the output files", ->
  64. @ResourceWriter._deleteFileIfNotDirectory
  65. .calledWith(path.join(@basePath, "output.pdf"))
  66. .should.equal true
  67. it "should delete the extra files", ->
  68. @ResourceWriter._deleteFileIfNotDirectory
  69. .calledWith(path.join(@basePath, "extra/file.tex"))
  70. .should.equal true
  71. it "should not delete the extra aux files", ->
  72. @ResourceWriter._deleteFileIfNotDirectory
  73. .calledWith(path.join(@basePath, "extra.aux"))
  74. .should.equal false
  75. it "should not delete the knitr cache file", ->
  76. @ResourceWriter._deleteFileIfNotDirectory
  77. .calledWith(path.join(@basePath, "cache/_chunk1"))
  78. .should.equal false
  79. it "should call the callback", ->
  80. @callback.called.should.equal true
  81. it "should time the request", ->
  82. @Metrics.Timer::done.called.should.equal true
  83. describe "_writeResourceToDisk", ->
  84. describe "with a url based resource", ->
  85. beforeEach ->
  86. @resource =
  87. path: "main.tex"
  88. url: "http://www.example.com/main.tex"
  89. modified: Date.now()
  90. @UrlCache.downloadUrlToFile = sinon.stub().callsArgWith(4, "fake error downloading file")
  91. @ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
  92. it "should ensure the directory exists", ->
  93. @mkdirp
  94. .calledWith(path.dirname(path.join(@basePath, @resource.path)))
  95. .should.equal true
  96. it "should write the URL from the cache", ->
  97. @UrlCache.downloadUrlToFile
  98. .calledWith(@project_id, @resource.url, path.join(@basePath, @resource.path), @resource.modified)
  99. .should.equal true
  100. it "should call the callback", ->
  101. @callback.called.should.equal true
  102. it "should not return an error if the resource writer errored", ->
  103. should.not.exist @callback.args[0][0]
  104. describe "with a content based resource", ->
  105. beforeEach ->
  106. @resource =
  107. path: "main.tex"
  108. content: "Hello world"
  109. @fs.writeFile = sinon.stub().callsArg(2)
  110. @ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
  111. it "should ensure the directory exists", ->
  112. @mkdirp
  113. .calledWith(path.dirname(path.join(@basePath, @resource.path)))
  114. .should.equal true
  115. it "should write the contents to disk", ->
  116. @fs.writeFile
  117. .calledWith(path.join(@basePath, @resource.path), @resource.content)
  118. .should.equal true
  119. it "should call the callback", ->
  120. @callback.called.should.equal true
  121. describe "with a file path that breaks out of the root folder", ->
  122. beforeEach ->
  123. @resource =
  124. path: "../../main.tex"
  125. content: "Hello world"
  126. @fs.writeFile = sinon.stub().callsArg(2)
  127. @ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
  128. it "should not write to disk", ->
  129. @fs.writeFile.called.should.equal false
  130. it "should return an error", ->
  131. @callback
  132. .calledWith(new Error("resource path is outside root directory"))
  133. .should.equal true