1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-13 17:56:25 +00:00

Merge pull request #1963 from ehhc/OnBlur_Throws_Exceptions_On_Snippet_Notes

OnBlur throws exceptions if the notetype is snippet -> Fixes #1962
This commit is contained in:
Junyoung Choi (Sai)
2018-06-06 11:11:27 +09:00
committed by GitHub
2 changed files with 41 additions and 5 deletions

View File

@@ -271,6 +271,9 @@ function deleteAttachmentFolder (storageKey, noteKey) {
* @param noteKey NoteKey of the current note. Is used to determine the belonging attachment folder. * @param noteKey NoteKey of the current note. Is used to determine the belonging attachment folder.
*/ */
function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey) { function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey) {
if (storageKey == null || noteKey == null || markdownContent == null) {
return
}
const targetStorage = findStorage.findStorage(storageKey) const targetStorage = findStorage.findStorage(storageKey)
const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey) const attachmentFolder = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey)
const attachmentsInNote = getAttachmentsInContent(markdownContent) 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'), '')) attachmentsInNoteOnlyFileNames.push(attachmentsInNote[i].replace(new RegExp(STORAGE_FOLDER_PLACEHOLDER + escapeStringRegexp(path.sep) + noteKey + escapeStringRegexp(path.sep), 'g'), ''))
} }
} }
if (fs.existsSync(attachmentFolder)) { if (fs.existsSync(attachmentFolder)) {
fs.readdir(attachmentFolder, (err, files) => { fs.readdir(attachmentFolder, (err, files) => {
if (err) { if (err) {
console.error("Error reading directory '" + attachmentFolder + "'. Error:") console.error('Error reading directory \'' + attachmentFolder + '\'. Error:')
console.error(err) console.error(err)
return return
} }
@@ -293,17 +295,17 @@ function deleteAttachmentsNotPresentInNote (markdownContent, storageKey, noteKey
const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file) const absolutePathOfFile = path.join(targetStorage.path, DESTINATION_FOLDER, noteKey, file)
fs.unlink(absolutePathOfFile, (err) => { fs.unlink(absolutePathOfFile, (err) => {
if (err) { if (err) {
console.error("Could not delete '%s'", absolutePathOfFile) console.error('Could not delete \'%s\'', absolutePathOfFile)
console.error(err) console.error(err)
return 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 { } else {
console.info("Attachment folder ('" + attachmentFolder + "') did not exist..") console.debug('Attachment folder (\'' + attachmentFolder + '\') did not exist..')
} }
} }

View File

@@ -339,6 +339,38 @@ it('should test that deleteAttachmentsNotPresentInNote does not delete reference
expect(fsUnlinkCallArguments.includes(path.join(attachmentFolderPath, dummyFilesInFolder[0]))).toBe(false) 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 () { it('should test that moveAttachments moves attachments only if the source folder existed', function () {
fse.existsSync = jest.fn(() => false) fse.existsSync = jest.fn(() => false)
fse.moveSync = jest.fn() 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 + 'image.jpg](imageName}) \n' +
'[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'pdf.pdf](pdf})' '[' + systemUnderTest.STORAGE_FOLDER_PLACEHOLDER + path.sep + oldNote.key + path.sep + 'pdf.pdf](pdf})'
newNote.content = testInput newNote.content = testInput
findStorage.findStorage = jest.fn()
findStorage.findStorage.mockReturnValue({path: 'dummyStoragePath'})
const expectedOutput = const expectedOutput =
'Test input' + 'Test input' +