ProjectRootDocManagerTests.coffee 15 KB

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