| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- /* eslint-disable
- max-len,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon')
- const chai = require('chai')
- const should = chai.should()
- const modulePath =
- '../../../../app/src/Features/Uploads/FileSystemImportManager.js'
- const SandboxedModule = require('sandboxed-module')
- describe('FileSystemImportManager', function() {
- beforeEach(function() {
- this.project_id = 'project-id-123'
- this.folder_id = 'folder-id-123'
- this.name = 'test-file.tex'
- this.path_on_disk = `/path/to/file/${this.name}`
- this.replace = 'replace-boolean-flag-mock'
- this.user_id = 'mock-user-123'
- this.callback = sinon.stub()
- this.encoding = 'latin1'
- this.DocumentHelper = {
- convertTexEncodingsToUtf8: sinon.stub().returnsArg(0)
- }
- return (this.FileSystemImportManager = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- fs: (this.fs = {}),
- '../Editor/EditorController': (this.EditorController = {}),
- './FileTypeManager': (this.FileTypeManager = {}),
- '../Project/ProjectLocator': (this.ProjectLocator = {}),
- '../Documents/DocumentHelper': this.DocumentHelper,
- 'logger-sharelatex': {
- log() {},
- err() {}
- }
- }
- }))
- })
- describe('addDoc', function() {
- beforeEach(function() {
- this.docContent = 'one\ntwo\nthree'
- this.docLines = this.docContent.split('\n')
- this.fs.readFile = sinon.stub().callsArgWith(2, null, this.docContent)
- return (this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, true))
- })
- describe('when path is symlink', function() {
- beforeEach(function() {
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, false)
- this.EditorController.addDoc = sinon.stub()
- return this.FileSystemImportManager.addDoc(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.encoding,
- false,
- this.callback
- )
- })
- it('should not read the file from disk', function() {
- return this.fs.readFile.called.should.equal(false)
- })
- it('should not insert the doc', function() {
- return this.EditorController.addDoc.called.should.equal(false)
- })
- })
- describe('with replace set to false', function() {
- beforeEach(function() {
- this.EditorController.addDoc = sinon.stub().callsArg(6)
- return this.FileSystemImportManager.addDoc(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.encoding,
- false,
- this.callback
- )
- })
- it('should read the file from disk', function() {
- return this.fs.readFile.calledWith(this.path_on_disk).should.equal(true)
- })
- it('should insert the doc', function() {
- return this.EditorController.addDoc
- .calledWith(
- this.project_id,
- this.folder_id,
- this.name,
- this.docLines,
- 'upload',
- this.user_id
- )
- .should.equal(true)
- })
- })
- describe('with windows line ending', function() {
- beforeEach(function() {
- this.docContent = 'one\r\ntwo\r\nthree'
- this.docLines = ['one', 'two', 'three']
- this.fs.readFile = sinon.stub().callsArgWith(2, null, this.docContent)
- this.EditorController.addDoc = sinon.stub().callsArg(6)
- return this.FileSystemImportManager.addDoc(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.encoding,
- false,
- this.callback
- )
- })
- it('should strip the \\r characters before adding', function() {
- return this.EditorController.addDoc
- .calledWith(
- this.project_id,
- this.folder_id,
- this.name,
- this.docLines,
- 'upload',
- this.user_id
- )
- .should.equal(true)
- })
- })
- describe('with \r line endings', function() {
- beforeEach(function() {
- this.docContent = 'one\rtwo\rthree'
- this.docLines = ['one', 'two', 'three']
- this.fs.readFile = sinon.stub().callsArgWith(2, null, this.docContent)
- this.EditorController.addDoc = sinon.stub().callsArg(6)
- return this.FileSystemImportManager.addDoc(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.encoding,
- false,
- this.callback
- )
- })
- it('should treat the \\r characters as newlines', function() {
- return this.EditorController.addDoc
- .calledWith(
- this.project_id,
- this.folder_id,
- this.name,
- this.docLines,
- 'upload',
- this.user_id
- )
- .should.equal(true)
- })
- })
- describe('with replace set to true', function() {
- beforeEach(function() {
- this.EditorController.upsertDoc = sinon.stub().yields()
- return this.FileSystemImportManager.addDoc(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.encoding,
- true,
- this.callback
- )
- })
- it('should upsert the doc', function() {
- return this.EditorController.upsertDoc
- .calledWith(
- this.project_id,
- this.folder_id,
- this.name,
- this.docLines,
- 'upload',
- this.user_id
- )
- .should.equal(true)
- })
- it('should read the file with the correct encoding', function() {
- return sinon.assert.calledWith(
- this.fs.readFile,
- this.path_on_disk,
- this.encoding
- )
- })
- })
- })
- describe('addFile with replace set to false', function() {
- beforeEach(function() {
- this.EditorController.addFile = sinon.stub().yields()
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, true)
- return this.FileSystemImportManager.addFile(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- false,
- this.callback
- )
- })
- it('should add the file', function() {
- return this.EditorController.addFile
- .calledWith(
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- null,
- 'upload',
- this.user_id
- )
- .should.equal(true)
- })
- })
- describe('addFile with symlink', function() {
- beforeEach(function() {
- this.EditorController.addFile = sinon.stub()
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, false)
- this.EditorController.replaceFile = sinon.stub()
- return this.FileSystemImportManager.addFile(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- false,
- this.callback
- )
- })
- it('should node add the file', function() {
- this.EditorController.addFile.called.should.equal(false)
- return this.EditorController.replaceFile.called.should.equal(false)
- })
- })
- describe('addFile with replace set to true', function() {
- beforeEach(function() {
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, true)
- this.EditorController.upsertFile = sinon.stub().yields()
- return this.FileSystemImportManager.addFile(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- true,
- this.callback
- )
- })
- it('should add the file', function() {
- return this.EditorController.upsertFile
- .calledWith(
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- null,
- 'upload',
- this.user_id
- )
- .should.equal(true)
- })
- })
- describe('addFolder', function() {
- beforeEach(function() {
- this.new_folder_id = 'new-folder-id'
- this.EditorController.addFolder = sinon
- .stub()
- .callsArgWith(4, null, { _id: this.new_folder_id })
- return (this.FileSystemImportManager.addFolderContents = sinon
- .stub()
- .callsArg(5))
- })
- describe('successfully', function() {
- beforeEach(function() {
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, true)
- return this.FileSystemImportManager.addFolder(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.replace,
- this.callback
- )
- })
- it('should add a folder to the project', function() {
- return this.EditorController.addFolder
- .calledWith(this.project_id, this.folder_id, this.name, 'upload')
- .should.equal(true)
- })
- it('should add the folders contents', function() {
- return this.FileSystemImportManager.addFolderContents
- .calledWith(
- this.user_id,
- this.project_id,
- this.new_folder_id,
- this.path_on_disk,
- this.replace
- )
- .should.equal(true)
- })
- })
- describe('with symlink', function() {
- beforeEach(function() {
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, false)
- return this.FileSystemImportManager.addFolder(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.replace,
- this.callback
- )
- })
- it('should not add a folder to the project', function() {
- this.EditorController.addFolder.called.should.equal(false)
- return this.FileSystemImportManager.addFolderContents.called.should.equal(
- false
- )
- })
- })
- })
- describe('addFolderContents', function() {
- beforeEach(function() {
- this.folderEntries = ['path1', 'path2', 'path3']
- this.ignoredEntries = ['.DS_Store']
- this.fs.readdir = sinon
- .stub()
- .callsArgWith(1, null, this.folderEntries.concat(this.ignoredEntries))
- this.FileSystemImportManager.addEntity = sinon.stub().callsArg(6)
- this.FileTypeManager.shouldIgnore = (path, callback) => {
- return callback(
- null,
- this.ignoredEntries.indexOf(require('path').basename(path)) !== -1
- )
- }
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, true)
- return this.FileSystemImportManager.addFolderContents(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.path_on_disk,
- this.replace,
- this.callback
- )
- })
- it('should call addEntity for each file in the folder which is not ignored', function() {
- return Array.from(this.folderEntries).map(name =>
- this.FileSystemImportManager.addEntity
- .calledWith(
- this.user_id,
- this.project_id,
- this.folder_id,
- name,
- `${this.path_on_disk}/${name}`,
- this.replace
- )
- .should.equal(true)
- )
- })
- it('should not call addEntity for the ignored files', function() {
- return Array.from(this.ignoredEntries).map(name =>
- this.FileSystemImportManager.addEntity
- .calledWith(
- this.user_id,
- this.project_id,
- this.folder_id,
- name,
- `${this.path_on_disk}/${name}`,
- this.replace
- )
- .should.equal(false)
- )
- })
- it('should look in the correct directory', function() {
- return this.fs.readdir.calledWith(this.path_on_disk).should.equal(true)
- })
- })
- describe('addEntity', function() {
- describe('with directory', function() {
- beforeEach(function() {
- this.FileTypeManager.isDirectory = sinon
- .stub()
- .callsArgWith(1, null, true)
- this.FileSystemImportManager.addFolder = sinon.stub().callsArg(6)
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, true)
- return this.FileSystemImportManager.addEntity(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.replace,
- this.callback
- )
- })
- it('should call addFolder', function() {
- return this.FileSystemImportManager.addFolder
- .calledWith(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.replace
- )
- .should.equal(true)
- })
- })
- describe('with binary file', function() {
- beforeEach(function() {
- this.FileTypeManager.isDirectory = sinon
- .stub()
- .callsArgWith(1, null, false)
- this.FileTypeManager.getType = sinon.stub().callsArgWith(2, null, true)
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, true)
- this.FileSystemImportManager.addFile = sinon.stub().callsArg(6)
- return this.FileSystemImportManager.addEntity(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.replace,
- this.callback
- )
- })
- it('should call addFile', function() {
- return this.FileSystemImportManager.addFile
- .calledWith(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.replace
- )
- .should.equal(true)
- })
- })
- describe('with text file', function() {
- beforeEach(function() {
- this.FileTypeManager.isDirectory = sinon
- .stub()
- .callsArgWith(1, null, false)
- this.FileTypeManager.getType = sinon
- .stub()
- .callsArgWith(2, null, false, 'latin1')
- this.FileSystemImportManager.addDoc = sinon.stub().callsArg(7)
- this.FileSystemImportManager._isSafeOnFileSystem = sinon
- .stub()
- .callsArgWith(1, null, true)
- return this.FileSystemImportManager.addEntity(
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- this.replace,
- this.callback
- )
- })
- it('should call addFile', function() {
- return sinon.assert.calledWith(
- this.FileSystemImportManager.addDoc,
- this.user_id,
- this.project_id,
- this.folder_id,
- this.name,
- this.path_on_disk,
- 'latin1',
- this.replace
- )
- })
- })
- })
- })
|