From 68c8b47ffbe53cb0bbb95a322f6ab15be64085b2 Mon Sep 17 00:00:00 2001 From: Seongjae Lee Date: Sun, 3 Jul 2016 10:44:32 -0700 Subject: [PATCH] Fix a note-watcher error during deactivation --- lib/note-watcher.coffee | 3 +- spec/notational-velocity-spec.coffee | 6 +- spec/note-watcher-spec.coffee | 180 +++++++++++++-------------- 3 files changed, 95 insertions(+), 94 deletions(-) diff --git a/lib/note-watcher.coffee b/lib/note-watcher.coffee index a4bf000..6b4a7e6 100644 --- a/lib/note-watcher.coffee +++ b/lib/note-watcher.coffee @@ -19,6 +19,7 @@ class NoteWatcher extends EventEmitter @_startWatcher() save: -> + return unless @_state == 'ready' cache = { baseDirectory: @_baseDirectory, extensions: @_extensions, @@ -31,7 +32,7 @@ class NoteWatcher extends EventEmitter zlib.deflateSync(JSON.stringify(cache))) close: -> - @_watcher.close() + @_watcher?.close() search: (query) -> if query? and query.length > 0 diff --git a/spec/notational-velocity-spec.coffee b/spec/notational-velocity-spec.coffee index a162ba6..6114c34 100644 --- a/spec/notational-velocity-spec.coffee +++ b/spec/notational-velocity-spec.coffee @@ -35,14 +35,14 @@ describe "nvAtom", -> it "checks if we banned the default directory under packages directory", -> noteDirectory = path.join(process.env.ATOM_HOME, 'packages', 'nvatom', 'notebook') atom.config.set('nvatom.directory', noteDirectory) - + waitsForPromise -> atom.packages.activatePackage('nvatom') - + runs -> waitsForPromise -> atom.packages.activatePackage('notifications') - + runs -> notificationContainer = workspaceElement.querySelector('atom-notifications') notification = notificationContainer.querySelector('atom-notification.fatal') diff --git a/spec/note-watcher-spec.coffee b/spec/note-watcher-spec.coffee index 895c310..4fff7a2 100644 --- a/spec/note-watcher-spec.coffee +++ b/spec/note-watcher-spec.coffee @@ -25,98 +25,98 @@ describe 'note-watcher', -> afterEach -> watcher.close() - # it 'handles a symbolic link note properly', -> - # spy = jasmine.createSpy() - # watcher.on 'update', spy - # - # anotherDirectoryPath = temp.mkdirSync() - # fs.writeFileSync(path.join(anotherDirectoryPath, 'baz.md'), 'A common name for the foobar, also foobaz.') - # fs.symlinkSync(path.join(anotherDirectoryPath, 'baz.md'), path.join(directoryPath, 'baz.md')) - # - # waitsFor -> spy.wasCalled - # - # runs -> - # result = watcher.search('baz') - # expect(result.length).toBe(1) - - # it 'handles a symbolic link directory properly', -> - # spy = jasmine.createSpy() - # watcher.on 'update', spy - # - # anotherDirectoryPath = temp.mkdirSync() - # fs.writeFileSync(path.join(anotherDirectoryPath, 'baz.md'), 'A common name for the foobar, also foobaz.') - # fs.symlinkSync(anotherDirectoryPath, path.join(directoryPath, 'baz')) - # - # waitsFor -> spy.wasCalled - # - # runs -> - # result = watcher.search('baz') - # expect(result.length).toBe(1) - # expect(result[0].filePath).toBe(path.join(directoryPath, 'baz', 'baz.md')) + it 'handles a symbolic link note properly', -> + spy = jasmine.createSpy() + watcher.on 'update', spy + + anotherDirectoryPath = temp.mkdirSync() + fs.writeFileSync(path.join(anotherDirectoryPath, 'baz.md'), 'A common name for the foobar, also foobaz.') + fs.symlinkSync(path.join(anotherDirectoryPath, 'baz.md'), path.join(directoryPath, 'baz.md')) + + waitsFor -> spy.wasCalled + + runs -> + result = watcher.search('baz') + expect(result.length).toBe(1) - # it 'handles paired symbolic links properly without falling into an infinite loop', -> - # spy = jasmine.createSpy() - # watcher.on 'update', spy - # - # anotherDirectoryPath = temp.mkdirSync() - # fs.writeFileSync(path.join(anotherDirectoryPath, 'baz.md'), 'A common name for the foobar, also foobaz.') - # fs.symlinkSync(anotherDirectoryPath, path.join(directoryPath, 'bazDir')) - # fs.symlinkSync(directoryPath, path.join(anotherDirectoryPath, 'fooDir')) - # - # waitsFor -> spy.wasCalled - # - # runs -> - # result = watcher.search('baz') - # expect(result.length).toBe(1) - # expect(result[0].filePath).toBe(path.join(directoryPath, 'bazDir', 'baz.md')) + it 'handles a symbolic link directory properly', -> + spy = jasmine.createSpy() + watcher.on 'update', spy - # it 'watches adding a file', -> - # spy = jasmine.createSpy() - # watcher.on 'update', spy - # fs.writeFileSync(path.join(directoryPath, 'note.md'), 'hello world') - # - # waitsFor -> spy.wasCalled - # - # it 'watches changing a file', -> - # spy = jasmine.createSpy() - # watcher.on 'update', spy - # filePath = path.join(directoryPath, 'note.md') - # fs.writeFileSync(filePath, 'hello world') - # waitsFor -> spy.callCount == 1 - # - # runs -> fs.writeFileSync(filePath, 'hello world 2') - # - # waitsFor -> spy.callCount == 2 - # - # it 'watches removing a file', -> - # spy = jasmine.createSpy() - # watcher.on('update', spy) - # filePath = path.join(directoryPath, 'note.md') - # fs.writeFileSync(filePath, 'hello world') - # waitsFor -> spy.callCount == 1 - # - # runs -> fs.unlinkSync(filePath) - # - # waitsFor -> spy.callCount == 2 - # - # it 'searches', -> - # result = watcher.search('programming') - # expect(result.length).toBe(1) - # expect(result[0].title).toBe('foo') - # - # it 'saves and loads', -> - # watcher.save() - # watcher.close() - # watcher = new NoteWatcher(directoryPath) - # spy = jasmine.createSpy() - # watcher.on 'ready', spy - # - # waitsFor -> spy.wasCalled - # - # runs -> - # result = watcher.search('programming') - # expect(result.length).toBe(1) - # expect(result[0].title).toBe('foo') + anotherDirectoryPath = temp.mkdirSync() + fs.writeFileSync(path.join(anotherDirectoryPath, 'baz.md'), 'A common name for the foobar, also foobaz.') + fs.symlinkSync(anotherDirectoryPath, path.join(directoryPath, 'baz')) + + waitsFor -> spy.wasCalled + + runs -> + result = watcher.search('baz') + expect(result.length).toBe(1) + expect(result[0].filePath).toBe(path.join(directoryPath, 'baz', 'baz.md')) + + it 'handles paired symbolic links properly without falling into an infinite loop', -> + spy = jasmine.createSpy() + watcher.on 'update', spy + + anotherDirectoryPath = temp.mkdirSync() + fs.writeFileSync(path.join(anotherDirectoryPath, 'baz.md'), 'A common name for the foobar, also foobaz.') + fs.symlinkSync(anotherDirectoryPath, path.join(directoryPath, 'bazDir')) + fs.symlinkSync(directoryPath, path.join(anotherDirectoryPath, 'fooDir')) + + waitsFor -> spy.wasCalled + + runs -> + result = watcher.search('baz') + expect(result.length).toBe(1) + expect(result[0].filePath).toBe(path.join(directoryPath, 'bazDir', 'baz.md')) + + it 'watches adding a file', -> + spy = jasmine.createSpy() + watcher.on 'update', spy + fs.writeFileSync(path.join(directoryPath, 'note.md'), 'hello world') + + waitsFor -> spy.wasCalled + + it 'watches changing a file', -> + spy = jasmine.createSpy() + watcher.on 'update', spy + filePath = path.join(directoryPath, 'note.md') + fs.writeFileSync(filePath, 'hello world') + waitsFor -> spy.callCount == 1 + + runs -> fs.writeFileSync(filePath, 'hello world 2') + + waitsFor -> spy.callCount == 2 + + it 'watches removing a file', -> + spy = jasmine.createSpy() + watcher.on('update', spy) + filePath = path.join(directoryPath, 'note.md') + fs.writeFileSync(filePath, 'hello world') + waitsFor -> spy.callCount == 1 + + runs -> fs.unlinkSync(filePath) + + waitsFor -> spy.callCount == 2 + + it 'searches', -> + result = watcher.search('programming') + expect(result.length).toBe(1) + expect(result[0].title).toBe('foo') + + it 'saves and loads', -> + watcher.save() + watcher.close() + watcher = new NoteWatcher(directoryPath) + spy = jasmine.createSpy() + watcher.on 'ready', spy + + waitsFor -> spy.wasCalled + + runs -> + result = watcher.search('programming') + expect(result.length).toBe(1) + expect(result[0].title).toBe('foo') it 'loads the modified contents correctly', -> watcher.save()