ProjectRootDocManagerTests.coffee 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. chai = require('chai')
  2. should = chai.should()
  3. expect = chai.expect
  4. sinon = require("sinon")
  5. modulePath = "../../../../app/js/Features/Project/ProjectRootDocManager.js"
  6. SandboxedModule = require('sandboxed-module')
  7. describe 'ProjectRootDocManager', ->
  8. beforeEach ->
  9. @project_id = "project-123"
  10. @docPaths =
  11. "doc-id-1": "/chapter1.tex"
  12. "doc-id-2": "/main.tex"
  13. "doc-id-3": "/nested/chapter1a.tex"
  14. "doc-id-4": "/nested/chapter1b.tex"
  15. @sl_req_id = "sl-req-id-123"
  16. @callback = sinon.stub()
  17. @globbyFiles = ['a.tex', 'b.tex', 'main.tex']
  18. @globby = sinon.stub().returns(new Promise (resolve) =>
  19. resolve(@globbyFiles)
  20. )
  21. @fs =
  22. readFile: sinon.stub().callsArgWith(2, new Error('file not found'))
  23. stat: sinon.stub().callsArgWith(1, null, {size: 100})
  24. @ProjectRootDocManager = SandboxedModule.require modulePath, requires:
  25. "./ProjectEntityHandler" : @ProjectEntityHandler = {}
  26. "./ProjectEntityUpdateHandler" : @ProjectEntityUpdateHandler = {}
  27. "./ProjectGetter" : @ProjectGetter = {}
  28. "globby" : @globby
  29. "fs" : @fs
  30. describe "setRootDocAutomatically", ->
  31. describe "when there is a suitable root doc", ->
  32. beforeEach (done)->
  33. @docs =
  34. "/chapter1.tex":
  35. _id: "doc-id-1"
  36. lines: ["something else","\\begin{document}", "Hello world", "\\end{document}"]
  37. "/main.tex":
  38. _id: "doc-id-2"
  39. lines: ["different line","\\documentclass{article}", "\\input{chapter1}"]
  40. "/nested/chapter1a.tex":
  41. _id: "doc-id-3"
  42. lines: ["Hello world"]
  43. "/nested/chapter1b.tex":
  44. _id: "doc-id-4"
  45. lines: ["Hello world"]
  46. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  47. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  48. @ProjectRootDocManager.setRootDocAutomatically @project_id, done
  49. it "should check the docs of the project", ->
  50. @ProjectEntityHandler.getAllDocs.calledWith(@project_id)
  51. .should.equal true
  52. it "should set the root doc to the doc containing a documentclass", ->
  53. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  54. .should.equal true
  55. describe "when the root doc is an Rtex file", ->
  56. beforeEach ->
  57. @docs =
  58. "/chapter1.tex":
  59. _id: "doc-id-1"
  60. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  61. "/main.Rtex":
  62. _id: "doc-id-2"
  63. lines: ["\\documentclass{article}", "\\input{chapter1}"]
  64. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  65. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  66. @ProjectRootDocManager.setRootDocAutomatically @project_id, @callback
  67. it "should set the root doc to the doc containing a documentclass", ->
  68. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  69. .should.equal true
  70. describe "when there is no suitable root doc", ->
  71. beforeEach (done)->
  72. @docs =
  73. "/chapter1.tex":
  74. _id: "doc-id-1"
  75. lines: ["\\begin{document}", "Hello world", "\\end{document}"]
  76. "/style.bst":
  77. _id: "doc-id-2"
  78. lines: ["%Example: \\documentclass{article}"]
  79. @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
  80. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  81. @ProjectRootDocManager.setRootDocAutomatically @project_id, done
  82. it "should not set the root doc to the doc containing a documentclass", ->
  83. @ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
  84. describe "findRootDocFileFromDirectory", ->
  85. beforeEach ->
  86. @fs.readFile.withArgs('/foo/a.tex').callsArgWith(2, null, 'Hello World!')
  87. @fs.readFile.withArgs('/foo/b.tex').callsArgWith(2, null, "I'm a little teapot, get me out of here.")
  88. @fs.readFile.withArgs('/foo/main.tex').callsArgWith(2, null, "Help, I'm trapped in a unit testing factory")
  89. @fs.readFile.withArgs('/foo/c.tex').callsArgWith(2, null, 'Tomato, tomahto.')
  90. @fs.readFile.withArgs('/foo/a/a.tex').callsArgWith(2, null, 'Potato? Potahto. Potootee!')
  91. @documentclassContent = "% test\n\\documentclass\n\% test"
  92. describe "when there is a file in a subfolder", ->
  93. beforeEach ->
  94. # have to splice globbyFiles weirdly because of the way the stubbed globby method handles references
  95. @globbyFiles.splice(0, @globbyFiles.length, 'c.tex', 'a.tex', 'a/a.tex', 'b.tex')
  96. it "processes the root folder files first, and then the subfolder, in alphabetical order", (done) ->
  97. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo', (error, path) =>
  98. expect(error).not.to.exist
  99. expect(path).not.to.exist
  100. sinon.assert.callOrder(
  101. @fs.readFile.withArgs('/foo/a.tex')
  102. @fs.readFile.withArgs('/foo/b.tex')
  103. @fs.readFile.withArgs('/foo/c.tex')
  104. @fs.readFile.withArgs('/foo/a/a.tex')
  105. )
  106. done()
  107. it "processes smaller files first", (done) ->
  108. @fs.stat.withArgs('/foo/c.tex').callsArgWith(1, null, {size: 1})
  109. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo',(error, path) =>
  110. expect(error).not.to.exist
  111. expect(path).not.to.exist
  112. sinon.assert.callOrder(
  113. @fs.readFile.withArgs('/foo/c.tex')
  114. @fs.readFile.withArgs('/foo/a.tex')
  115. @fs.readFile.withArgs('/foo/b.tex')
  116. @fs.readFile.withArgs('/foo/a/a.tex')
  117. )
  118. done()
  119. describe "when main.tex contains a documentclass", ->
  120. beforeEach ->
  121. @fs.readFile.withArgs('/foo/main.tex').callsArgWith(2, null, @documentclassContent)
  122. it "returns main.tex", (done) ->
  123. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo', (error, path, content) =>
  124. expect(error).not.to.exist
  125. expect(path).to.equal 'main.tex'
  126. expect(content).to.equal @documentclassContent
  127. done()
  128. it "processes main.text first and stops processing when it finds the content", (done) ->
  129. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo', =>
  130. expect(@fs.readFile).to.be.calledWith('/foo/main.tex')
  131. expect(@fs.readFile).not.to.be.calledWith('/foo/a.tex')
  132. done()
  133. describe "when a.tex contains a documentclass", ->
  134. beforeEach ->
  135. @fs.readFile.withArgs('/foo/a.tex').callsArgWith(2, null, @documentclassContent)
  136. it "returns a.tex", (done) ->
  137. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo', (error, path, content) =>
  138. expect(error).not.to.exist
  139. expect(path).to.equal 'a.tex'
  140. expect(content).to.equal @documentclassContent
  141. done()
  142. it "processes main.text first and stops processing when it finds the content", (done) ->
  143. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo', =>
  144. expect(@fs.readFile).to.be.calledWith('/foo/main.tex')
  145. expect(@fs.readFile).to.be.calledWith('/foo/a.tex')
  146. expect(@fs.readFile).not.to.be.calledWith('/foo/b.tex')
  147. done()
  148. describe "when there is no documentclass", ->
  149. it "returns null with no error", (done) ->
  150. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo', (error, path, content) =>
  151. expect(error).not.to.exist
  152. expect(path).not.to.exist
  153. expect(content).not.to.exist
  154. done()
  155. it "processes all the files", (done) ->
  156. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo', =>
  157. expect(@fs.readFile).to.be.calledWith('/foo/main.tex')
  158. expect(@fs.readFile).to.be.calledWith('/foo/a.tex')
  159. expect(@fs.readFile).to.be.calledWith('/foo/b.tex')
  160. done()
  161. describe "when there is an error reading a file", ->
  162. beforeEach ->
  163. @fs.readFile.withArgs('/foo/a.tex').callsArgWith(2, new Error('something went wrong'))
  164. it "returns an error", (done) ->
  165. @ProjectRootDocManager.findRootDocFileFromDirectory '/foo', (error, path, content) =>
  166. expect(error).to.exist
  167. expect(path).not.to.exist
  168. expect(content).not.to.exist
  169. done()
  170. describe "setRootDocFromName", ->
  171. describe "when there is a suitable root doc", ->
  172. beforeEach (done)->
  173. @docPaths =
  174. "doc-id-1": "/chapter1.tex"
  175. "doc-id-2": "/main.tex"
  176. "doc-id-3": "/nested/chapter1a.tex"
  177. "doc-id-4": "/nested/chapter1b.tex"
  178. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  179. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  180. @ProjectRootDocManager.setRootDocFromName @project_id, '/main.tex', done
  181. it "should check the docs of the project", ->
  182. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  183. .should.equal true
  184. it "should set the root doc to main.tex", ->
  185. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  186. .should.equal true
  187. describe "when there is a suitable root doc but the leading slash is missing", ->
  188. beforeEach (done)->
  189. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  190. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  191. @ProjectRootDocManager.setRootDocFromName @project_id, 'main.tex', done
  192. it "should check the docs of the project", ->
  193. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  194. .should.equal true
  195. it "should set the root doc to main.tex", ->
  196. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  197. .should.equal true
  198. describe "when there is a suitable root doc with a basename match", ->
  199. beforeEach (done)->
  200. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  201. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  202. @ProjectRootDocManager.setRootDocFromName @project_id, 'chapter1a.tex', done
  203. it "should check the docs of the project", ->
  204. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  205. .should.equal true
  206. it "should set the root doc using the basename", ->
  207. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-3")
  208. .should.equal true
  209. describe "when there is a suitable root doc but the filename is in quotes", ->
  210. beforeEach (done)->
  211. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  212. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  213. @ProjectRootDocManager.setRootDocFromName @project_id, "'main.tex'", done
  214. it "should check the docs of the project", ->
  215. @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id)
  216. .should.equal true
  217. it "should set the root doc to main.tex", ->
  218. @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
  219. .should.equal true
  220. describe "when there is no suitable root doc", ->
  221. beforeEach (done)->
  222. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  223. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
  224. @ProjectRootDocManager.setRootDocFromName @project_id, "other.tex", done
  225. it "should not set the root doc", ->
  226. @ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
  227. describe "ensureRootDocumentIsSet", ->
  228. beforeEach ->
  229. @project = {}
  230. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  231. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArgWith(1, null)
  232. describe "when the root doc is set", ->
  233. beforeEach ->
  234. @project.rootDoc_id = "root-doc-id"
  235. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  236. it "should find the project fetching only the rootDoc_id field", ->
  237. @ProjectGetter.getProject
  238. .calledWith(@project_id, rootDoc_id: 1)
  239. .should.equal true
  240. it "should not try to update the project rootDoc_id", ->
  241. @ProjectRootDocManager.setRootDocAutomatically
  242. .called.should.equal false
  243. it "should call the callback", ->
  244. @callback.called.should.equal true
  245. describe "when the root doc is not set", ->
  246. beforeEach ->
  247. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  248. it "should find the project with only the rootDoc_id fiel", ->
  249. @ProjectGetter.getProject
  250. .calledWith(@project_id, rootDoc_id: 1)
  251. .should.equal true
  252. it "should update the project rootDoc_id", ->
  253. @ProjectRootDocManager.setRootDocAutomatically
  254. .calledWith(@project_id)
  255. .should.equal true
  256. it "should call the callback", ->
  257. @callback.called.should.equal true
  258. describe "when the project does not exist", ->
  259. beforeEach ->
  260. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
  261. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  262. it "should call the callback with an error", ->
  263. @callback.calledWith(new Error("project not found")).should.equal true
  264. describe "ensureRootDocumentIsValid", ->
  265. beforeEach ->
  266. @project = {}
  267. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project)
  268. @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().yields()
  269. @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths)
  270. @ProjectRootDocManager.setRootDocAutomatically = sinon.stub().callsArgWith(1, null)
  271. describe "when the root doc is set", ->
  272. describe "when the root doc is valid", ->
  273. beforeEach ->
  274. @project.rootDoc_id = "doc-id-2"
  275. @ProjectRootDocManager.ensureRootDocumentIsValid(@project_id, @callback)
  276. it "should find the project fetching only the rootDoc_id field", ->
  277. @ProjectGetter.getProject
  278. .calledWith(@project_id, rootDoc_id: 1)
  279. .should.equal true
  280. it "should not try to update the project rootDoc_id", ->
  281. @ProjectRootDocManager.setRootDocAutomatically
  282. .called.should.equal false
  283. it "should call the callback", ->
  284. @callback.called.should.equal true
  285. describe "when the root doc is not valid", ->
  286. beforeEach ->
  287. @project.rootDoc_id = "bogus-doc-id"
  288. @ProjectRootDocManager.ensureRootDocumentIsValid(@project_id, @callback)
  289. it "should find the project fetching only the rootDoc_id field", ->
  290. @ProjectGetter.getProject
  291. .calledWith(@project_id, rootDoc_id: 1)
  292. .should.equal true
  293. it "should null the rootDoc_id field", ->
  294. @ProjectEntityUpdateHandler.setRootDoc
  295. .calledWith(@project_id, null)
  296. .should.equal true
  297. it "should try to find a new rootDoc", ->
  298. @ProjectRootDocManager.setRootDocAutomatically
  299. .called.should.equal true
  300. it "should call the callback", ->
  301. @callback.called.should.equal true
  302. describe "when the root doc is not set", ->
  303. beforeEach ->
  304. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  305. it "should find the project fetching only the rootDoc_id fiel", ->
  306. @ProjectGetter.getProject
  307. .calledWith(@project_id, rootDoc_id: 1)
  308. .should.equal true
  309. it "should update the project rootDoc_id", ->
  310. @ProjectRootDocManager.setRootDocAutomatically
  311. .calledWith(@project_id)
  312. .should.equal true
  313. it "should call the callback", ->
  314. @callback.called.should.equal true
  315. describe "when the project does not exist", ->
  316. beforeEach ->
  317. @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null)
  318. @ProjectRootDocManager.ensureRootDocumentIsSet(@project_id, @callback)
  319. it "should call the callback with an error", ->
  320. @callback.calledWith(new Error("project not found")).should.equal true