FileSystemImportManagerTests.coffee 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/Features/Uploads/FileSystemImportManager.js"
  5. SandboxedModule = require('sandboxed-module')
  6. describe "FileSystemImportManager", ->
  7. beforeEach ->
  8. @project_id = "project-id-123"
  9. @folder_id = "folder-id-123"
  10. @name = "test-file.tex"
  11. @path_on_disk = "/path/to/file/#{@name}"
  12. @replace = "replace-boolean-flag-mock"
  13. @FileSystemImportManager = SandboxedModule.require modulePath, requires:
  14. "fs" : @fs = {}
  15. "../Editor/EditorController": @EditorController = {}
  16. "./FileTypeManager": @FileTypeManager = {}
  17. "../Project/ProjectLocator": @ProjectLocator = {}
  18. describe "addDoc", ->
  19. beforeEach ->
  20. @docContent = "one\ntwo\nthree"
  21. @docLines = @docContent.split("\n")
  22. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  23. @EditorController.addDoc = sinon.stub().callsArg(5)
  24. @FileSystemImportManager.addDoc @project_id, @folder_id, @name, @path_on_disk, false, @callback
  25. it "should read the file from disk", ->
  26. @fs.readFile.calledWith(@path_on_disk, "utf8").should.equal true
  27. it "should insert the doc", ->
  28. @EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload")
  29. .should.equal true
  30. describe "addDoc with windows line ending", ->
  31. beforeEach ->
  32. @docContent = "one\r\ntwo\r\nthree"
  33. @docLines = ["one", "two", "three"]
  34. @fs.readFile = sinon.stub().callsArgWith(2, null, @docContent)
  35. @EditorController.addDoc = sinon.stub().callsArg(5)
  36. @FileSystemImportManager.addDoc @project_id, @folder_id, @name, @path_on_disk, false, @callback
  37. it "should strip the \\r characters before adding", ->
  38. @EditorController.addDoc.calledWith(@project_id, @folder_id, @name, @docLines, "upload")
  39. .should.equal true
  40. describe "addFile with replace set to false", ->
  41. beforeEach ->
  42. @EditorController.addFile = sinon.stub().callsArg(5)
  43. @FileSystemImportManager.addFile @project_id, @folder_id, @name, @path_on_disk, false, @callback
  44. it "should add the file", ->
  45. @EditorController.addFile.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload")
  46. .should.equal true
  47. describe "addFile with replace set to true", ->
  48. describe "when the file doesn't exist", ->
  49. beforeEach ->
  50. @folder = {
  51. fileRefs: [{
  52. _id: "file-id-2"
  53. name: "not-the-right-file.tex"
  54. }]
  55. }
  56. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  57. @EditorController.addFile = sinon.stub().callsArg(5)
  58. @FileSystemImportManager.addFile @project_id, @folder_id, @name, @path_on_disk, true, @callback
  59. it "should look up the folder", ->
  60. @ProjectLocator.findElement
  61. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  62. .should.equal true
  63. it "should add the file", ->
  64. @EditorController.addFile.calledWith(@project_id, @folder_id, @name, @path_on_disk, "upload")
  65. .should.equal true
  66. describe "when the file does exist", ->
  67. beforeEach ->
  68. @folder = {
  69. fileRefs: [{
  70. _id: @file_id = "file-id-1"
  71. name: @name
  72. }, {
  73. _id: "file-id-2"
  74. name: "not-the-right-file.tex"
  75. }]
  76. }
  77. @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @folder)
  78. @EditorController.replaceFile = sinon.stub().callsArg(4)
  79. @FileSystemImportManager.addFile @project_id, @folder_id, @name, @path_on_disk, true, @callback
  80. it "should look up the folder", ->
  81. @ProjectLocator.findElement
  82. .calledWith(project_id: @project_id, element_id: @folder_id, type: "folder")
  83. .should.equal true
  84. it "should replace the file", ->
  85. @EditorController.replaceFile.calledWith(@project_id, @file_id, @path_on_disk, "upload")
  86. .should.equal true
  87. describe "addFolder", ->
  88. beforeEach ->
  89. @new_folder_id = "new-folder-id"
  90. @EditorController.addFolder = sinon.stub().callsArgWith(4, null, _id: @new_folder_id)
  91. @FileSystemImportManager.addFolderContents = sinon.stub().callsArg(4)
  92. @FileSystemImportManager.addFolder @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  93. it "should add a folder to the project", ->
  94. @EditorController.addFolder.calledWith(@project_id, @folder_id, @name, "upload")
  95. .should.equal true
  96. it "should add the folders contents", ->
  97. @FileSystemImportManager.addFolderContents.calledWith(@project_id, @new_folder_id, @path_on_disk, @replace)
  98. .should.equal true
  99. describe "addFolderContents", ->
  100. beforeEach ->
  101. @folderEntries = ["path1", "path2", "path3"]
  102. @ignoredEntries = [".DS_Store"]
  103. @fs.readdir = sinon.stub().callsArgWith(1, null, @folderEntries.concat @ignoredEntries)
  104. @FileSystemImportManager.addEntity = sinon.stub().callsArg(5)
  105. @FileTypeManager.shouldIgnore = (path, callback) =>
  106. callback null, @ignoredEntries.indexOf(require("path").basename(path)) != -1
  107. @FileSystemImportManager.addFolderContents @project_id, @folder_id, @path_on_disk, @replace, @callback
  108. it "should call addEntity for each file in the folder which is not ignored", ->
  109. for name in @folderEntries
  110. @FileSystemImportManager.addEntity.calledWith(@project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  111. .should.equal true
  112. it "should not call addEntity for the ignored files", ->
  113. for name in @ignoredEntries
  114. @FileSystemImportManager.addEntity.calledWith(@project_id, @folder_id, name, "#{@path_on_disk}/#{name}", @replace)
  115. .should.equal false
  116. it "should look in the correct directory", ->
  117. @fs.readdir.calledWith(@path_on_disk).should.equal true
  118. describe "addEntity", ->
  119. describe "with directory", ->
  120. beforeEach ->
  121. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, true)
  122. @FileSystemImportManager.addFolder = sinon.stub().callsArg(5)
  123. @FileSystemImportManager.addEntity @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  124. it "should call addFolder", ->
  125. @FileSystemImportManager.addFolder.calledWith(@project_id, @folder_id, @name, @path_on_disk, @replace, @callback)
  126. .should.equal true
  127. describe "with binary file", ->
  128. beforeEach ->
  129. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  130. @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, true)
  131. @FileSystemImportManager.addFile = sinon.stub().callsArg(5)
  132. @FileSystemImportManager.addEntity @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  133. it "should call addFile", ->
  134. @FileSystemImportManager.addFile.calledWith(@project_id, @folder_id, @name, @path_on_disk, @replace, @callback)
  135. .should.equal true
  136. describe "with text file", ->
  137. beforeEach ->
  138. @FileTypeManager.isDirectory = sinon.stub().callsArgWith(1, null, false)
  139. @FileTypeManager.isBinary = sinon.stub().callsArgWith(2, null, false)
  140. @FileSystemImportManager.addDoc = sinon.stub().callsArg(5)
  141. @FileSystemImportManager.addEntity @project_id, @folder_id, @name, @path_on_disk, @replace, @callback
  142. it "should call addFile", ->
  143. @FileSystemImportManager.addDoc.calledWith(@project_id, @folder_id, @name, @path_on_disk, @replace, @callback)
  144. .should.equal true