diff --git a/browser/main/lib/dataApi/attachmentManagement.js b/browser/main/lib/dataApi/attachmentManagement.js index 8cdc164b..e1469f38 100644 --- a/browser/main/lib/dataApi/attachmentManagement.js +++ b/browser/main/lib/dataApi/attachmentManagement.js @@ -271,6 +271,9 @@ function deleteAttachmentFolder (storageKey, noteKey) { * @param noteKey NoteKey of the current note. Is used to determine the belonging attachment folder. */ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey) { + if (storageKey == null || noteKey == null || markdownContent == null) { + return + } const targetStorage = findStorage.findStorage(storageKey) const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey) const attachmentsInNote = getAttachmentsInContent(markdownContent) @@ -280,11 +283,10 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey attachmentsInNoteOnlyFileNames.push(attachmentsInNote[i].replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey + escapeStringRegexp(path.sep), 'g'), '')) } } - if (fs.existsSync(attachmentFolder)) { fs.readdir(attachmentFolder, (err, files) => { if (err) { - console.error("Error reading directory '" + attachmentFolder + "'. Error:") + console.error('Error reading directory \'' + attachmentFolder + '\'. Error:') console.error(err) return } @@ -293,17 +295,17 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file) fs.unlink(absolutePathOfFile, (err) => { if (err) { - console.error("Could not delete '%s'", absolutePathOfFile) + console.error('Could not delete \'%s\'', absolutePathOfFile) console.error(err) return } - console.info("File '" + absolutePathOfFile + "' deleted because it was not included in the content of the note") + console.info('File \'' + absolutePathOfFile + '\' deleted because it was not included in the content of the note') }) } }) }) } else { - console.info("Attachment folder ('" + attachmentFolder + "') did not exist..") + console.debug('Attachment folder (\'' + attachmentFolder + '\') did not exist..') } } diff --git a/tests/dataApi/attachmentManagement.test.js b/tests/dataApi/attachmentManagement.test.js index b61d8bf9..439a6749 100644 --- a/tests/dataApi/attachmentManagement.test.js +++ b/tests/dataApi/attachmentManagement.test.js @@ -339,6 +339,38 @@ it('should test that deleteAttachmentsNotPresentInNote does not delete reference expect(fsUnlinkCallArguments.includes(path.join(attachmentFolderPath, dummyFilesInFolder[0]))).toBe(false) }) +it('should test that deleteAttachmentsNotPresentInNote does nothing if noteKey, storageKey or noteContent was null', function () { + const noteKey = null + const storageKey = null + const markdownContent = '' + + findStorage.findStorage = jest.fn() + fs.existsSync = jest.fn() + fs.readdir = jest.fn() + fs.unlink = jest.fn() + + systemUnderTest.deleteAttachmentsNotPresentInNote(markdownContent, storageKey, noteKey) + expect(fs.existsSync).not.toHaveBeenCalled() + expect(fs.readdir).not.toHaveBeenCalled() + expect(fs.unlink).not.toHaveBeenCalled() +}) + +it('should test that deleteAttachmentsNotPresentInNote does nothing if noteKey, storageKey or noteContent was undefined', function () { + const noteKey = undefined + const storageKey = undefined + const markdownContent = '' + + findStorage.findStorage = jest.fn() + fs.existsSync = jest.fn() + fs.readdir = jest.fn() + fs.unlink = jest.fn() + + systemUnderTest.deleteAttachmentsNotPresentInNote(markdownContent, storageKey, noteKey) + expect(fs.existsSync).not.toHaveBeenCalled() + expect(fs.readdir).not.toHaveBeenCalled() + expect(fs.unlink).not.toHaveBeenCalled() +}) + it('should test that moveAttachments moves attachments only if the source folder existed', function () { fse.existsSync = jest.fn(() => false) fse.moveSync = jest.fn() @@ -403,6 +435,8 @@ it('should test that cloneAttachments modifies the content of the new note corre '![' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'image.jpg](imageName}) \n' + '[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'pdf.pdf](pdf})' newNote.content = testInput + findStorage.findStorage = jest.fn() + findStorage.findStorage.mockReturnValue({path: 'dummyStoragePath'}) const expectedOutput = 'Test input' +