From 47c013f817a1b0c4084908da6482d24e4ac81163 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 26 Apr 2012 15:54:54 -0600 Subject: [PATCH] After each spec runs, make sure there are no subscriptions on directories (which would leak memory) --- spec/app/directory-spec.coffee | 3 +++ spec/extensions/tree-view-spec.coffee | 9 +++++++-- spec/spec-helper.coffee | 22 ++++++++++++++++++++-- src/extensions/tree-view.coffee | 4 ++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/spec/app/directory-spec.coffee b/spec/app/directory-spec.coffee index a2cf87a0f..7689ffd96 100644 --- a/spec/app/directory-spec.coffee +++ b/spec/app/directory-spec.coffee @@ -7,6 +7,9 @@ describe "Directory", -> beforeEach -> directory = new Directory(require.resolve('fixtures')) + afterEach -> + directory.off() + describe "when the contents of the directory change on disk", -> temporaryFilePath = null diff --git a/spec/extensions/tree-view-spec.coffee b/spec/extensions/tree-view-spec.coffee index 8dbd3af8a..e6fcf5010 100644 --- a/spec/extensions/tree-view-spec.coffee +++ b/spec/extensions/tree-view-spec.coffee @@ -14,6 +14,11 @@ describe "TreeView", -> sampleJs = treeView.find('.file:contains(sample.js)') sampleTxt = treeView.find('.file:contains(sample.txt)') + expect(rootDirectoryView.directory.subscriptionCount()).toBeGreaterThan 0 + + afterEach -> + treeView.deactivate() + describe ".initialize(project)", -> it "renders the root of the project and its contents alphabetically with subdirectories first in a collapsed state", -> expect(rootDirectoryView.find('> .header .disclosure-arrow')).toHaveText('▾') @@ -299,8 +304,8 @@ describe "TreeView", -> expect(rootDirectoryView.entries.find('.entry').length).toBe entriesCountBefore + 1 expect(rootDirectoryView.entries.find('.file:contains(temporary)')).toExist() - rootDirectoryView.remove() - describe "when a file is renamed in an expanded directory", -> + + diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 5ed7221a7..868770482 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -3,22 +3,40 @@ $ = require 'jquery' _ = require 'underscore' Keymap = require 'keymap' Point = require 'point' - +Directory = require 'directory' require 'window' window.showConsole() defaultTitle = document.title +directoriesWithSubscriptions = null beforeEach -> window.resetTimeouts() + directoriesWithSubscriptions = [] afterEach -> $('#jasmine-content').empty() document.title = defaultTitle + ensureNoDirectorySubscriptions() + window.keymap.bindKeys '*', 'meta-w': 'close' $(document).on 'close', -> window.close() +Directory.prototype.originalOn = Directory.prototype.on +Directory.prototype.on = (args...) -> + directoriesWithSubscriptions.push(this) if @subscriptionCount() == 0 + @originalOn(args...) + +ensureNoDirectorySubscriptions = -> + totalSubscriptionCount = 0 + for directory in directoriesWithSubscriptions + totalSubscriptionCount += directory.subscriptionCount() + console.log "Non-zero subscription count on", directory if directory.subscriptionCount() > 0 + + if totalSubscriptionCount > 0 + throw new Error("Total directory subscription count was #{totalSubscriptionCount}, when it should have been 0.\nSee console for details.") + # Use underscore's definition of equality for toEqual assertions jasmine.Env.prototype.equals_ = _.isEqual @@ -132,4 +150,4 @@ $.fn.textInput = (data) -> $(this).trigger(event) $.fn.simulateDomAttachment = -> - $('').append(this) \ No newline at end of file + $('').append(this) diff --git a/src/extensions/tree-view.coffee b/src/extensions/tree-view.coffee index 91342a3f0..f33abc1a4 100644 --- a/src/extensions/tree-view.coffee +++ b/src/extensions/tree-view.coffee @@ -27,6 +27,10 @@ class TreeView extends View @on 'tree-view:open-selected-entry', => @openSelectedEntry() @rootView.on 'active-editor-path-change', => @selectActiveFile() + deactivate: -> + @find('.expanded.directory').each -> + $(this).view().unwatchEntries() + selectActiveFile: -> activeFilePath = @rootView.activeEditor()?.buffer.path @selectEntry(@find(".file[path='#{activeFilePath}']"))