From 2d2a070d1e0e78ae92d6e7ec0704b17dcfb3217e Mon Sep 17 00:00:00 2001 From: Seongjae Lee Date: Wed, 7 Oct 2015 07:55:36 -0700 Subject: [PATCH] Fix a bug that utility.isNote does not handle symlinks properly --- lib/notational-velocity.coffee | 4 ++-- lib/utility.coffee | 11 ++++++++++- spec/interlink-spec.coffee | 2 ++ spec/notational-velocity-spec.coffee | 2 ++ spec/utility-spec.coffee | 29 ++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/notational-velocity.coffee b/lib/notational-velocity.coffee index 049a2c0..46e385b 100644 --- a/lib/notational-velocity.coffee +++ b/lib/notational-velocity.coffee @@ -73,8 +73,8 @@ module.exports = autodelete: (paneItem) -> return false unless paneItem?.getURI?()? uri = paneItem.getURI() - return unless Utility.isNote(uri) - return false unless paneItem?.isEmpty() + return false unless Utility.isNote(uri) + return false unless paneItem.isEmpty() fs.unlinkSync(uri) noteName = uri.substring(@rootDirectory.length + 1) atom.notifications.addInfo("Empty note #{noteName} is deleted.") diff --git a/lib/utility.coffee b/lib/utility.coffee index e305680..cb1ec05 100644 --- a/lib/utility.coffee +++ b/lib/utility.coffee @@ -11,7 +11,16 @@ class Utility @getPrimaryNoteExtension: -> if atom.config.get('nvatom.extensions').length then atom.config.get('nvatom.extensions')[0] else '.md' @isNote: (filePath) -> + return false unless path.extname(filePath) in atom.config.get('nvatom.extensions') + filePath = fs.normalize(filePath) - return filePath.startsWith(Utility.getNoteDirectory()) and path.extname(filePath) in atom.config.get('nvatom.extensions') + return true if filePath.startsWith(Utility.getNoteDirectory()) + return true if filePath.startsWith(fs.realpathSync(Utility.getNoteDirectory())) + return false unless fs.existsSync(filePath) + + filePath = fs.realpathSync(filePath) + return true if filePath.startsWith(Utility.getNoteDirectory()) + return true if filePath.startsWith(fs.realpathSync(Utility.getNoteDirectory())) + return false @trim: (str) -> str?.replace /^\s+|\s+$/g, '' diff --git a/spec/interlink-spec.coffee b/spec/interlink-spec.coffee index bd8a2f3..7e5e032 100644 --- a/spec/interlink-spec.coffee +++ b/spec/interlink-spec.coffee @@ -3,6 +3,8 @@ path = require 'path' temp = require 'temp' Interlink = require '../lib/interlink' +temp.track() + describe 'Interlink', -> defaultDirectory = atom.config.get('nvatom.directory') noteDirectory = null diff --git a/spec/notational-velocity-spec.coffee b/spec/notational-velocity-spec.coffee index bd0c268..a162ba6 100644 --- a/spec/notational-velocity-spec.coffee +++ b/spec/notational-velocity-spec.coffee @@ -1,6 +1,8 @@ path = require 'path' temp = require 'temp' +temp.track() + describe "nvAtom", -> defaultDirectory = atom.config.get('nvatom.directory') activationPromise = null diff --git a/spec/utility-spec.coffee b/spec/utility-spec.coffee index f54c025..669e666 100644 --- a/spec/utility-spec.coffee +++ b/spec/utility-spec.coffee @@ -1,5 +1,10 @@ +fs = require 'fs-plus' +path = require 'path' +temp = require 'temp' Utility = require '../lib/utility' +temp.track() + describe "utility", -> defaultNoteDirectory = atom.config.get('nvatom.directory') defaultNoteExtensions = atom.config.get('nvatom.extensions') @@ -23,3 +28,27 @@ describe "utility", -> expect(Utility.getPrimaryNoteExtension()).toBe('.markdown') atom.config.set('nvatom.extensions', []) expect(Utility.getPrimaryNoteExtension()).toBe('.md') + + describe 'isNote handles symlinks correctly', -> + atom.config.set('nvatom.extensions', ['.md', '.markdown']) + + tempDirectoryPath = path.join(temp.mkdirSync()) + noteDirectoryPath = path.join(temp.mkdirSync()) + noteDirectoryPathSymlink = path.join(tempDirectoryPath, 'note book') + notePath = path.join(noteDirectoryPath, 'note.md') + notePathSymlink = path.join(noteDirectoryPathSymlink, 'note symlink.md') + + fs.writeFileSync(notePath, 'dummy') + fs.symlinkSync(noteDirectoryPath, noteDirectoryPathSymlink) + fs.symlinkSync(notePath, notePathSymlink) + + expect(fs.existsSync(notePath)).toBe(true) + expect(fs.existsSync(fs.normalize(notePath))).toBe(true) + + atom.config.set('nvatom.directory', noteDirectoryPath) + expect(Utility.isNote(notePath)).toBe(true) + expect(Utility.isNote(notePathSymlink)).toBe(true) + + atom.config.set('nvatom.directory', noteDirectoryPathSymlink) + expect(Utility.isNote(notePath)).toBe(true) + expect(Utility.isNote(notePathSymlink)).toBe(true)