diff --git a/spec/app/atom-spec.coffee b/spec/app/atom-spec.coffee index 72aba8601..cdcb995cc 100644 --- a/spec/app/atom-spec.coffee +++ b/spec/app/atom-spec.coffee @@ -86,60 +86,53 @@ describe "the `atom` global", -> expect(Worker.prototype.terminate.calls.length).toBe 1 describe "package lifecycle", -> - [pack, packageModule] = [] - - beforeEach -> - pack = - name: "package" - packageMain: - activate: jasmine.createSpy("activate") - deactivate: -> - serialize: -> "it worked" - - packageModule = pack.packageMain - - describe ".activateAtomPackage(package)", -> - it "calls activate on the package", -> - atom.activateAtomPackage(pack) - expect(packageModule.activate).toHaveBeenCalledWith({}) - - it "calls activate on the package module with its previous state", -> - atom.activateAtomPackage(pack) - packageModule.activate.reset() + describe "activation", -> + it "calls activate on the package main with its previous state", -> + pack = window.loadPackage('package-with-module') + spyOn(pack.packageMain, 'activate') serializedState = rootView.serialize() rootView.deactivate() RootView.deserialize(serializedState) + window.loadPackage('package-with-module') - atom.activateAtomPackage(pack) - expect(packageModule.activate).toHaveBeenCalledWith("it worked") + expect(pack.packageMain.activate).toHaveBeenCalledWith(someNumber: 1) - describe ".deactivateAtomPackages()", -> + describe "deactivation", -> it "deactivates and removes the package module from the package module map", -> - atom.activateAtomPackage(pack) + pack = window.loadPackage('package-with-module') expect(atom.activatedAtomPackages.length).toBe 1 - spyOn(packageModule, "deactivate").andCallThrough() + spyOn(pack.packageMain, "deactivate").andCallThrough() atom.deactivateAtomPackages() - expect(packageModule.deactivate).toHaveBeenCalled() + expect(pack.packageMain.deactivate).toHaveBeenCalled() expect(atom.activatedAtomPackages.length).toBe 0 - describe ".serializeAtomPackages()", -> + describe "serialization", -> + it "uses previous serialization state on unactivated packages", -> + atom.atomPackageStates['package-with-activation-events'] = {previousData: 'exists'} + unactivatedPackage = window.loadPackage('package-with-activation-events') + activatedPackage = window.loadPackage('package-with-module') + + expect(atom.serializeAtomPackages()).toEqual + 'package-with-module': + 'someNumber': 1 + 'package-with-activation-events': + 'previousData': 'exists' + + # ensure serialization occurs when the packageis activated + unactivatedPackage.activatePackageMain() + expect(atom.serializeAtomPackages()).toEqual + 'package-with-module': + 'someNumber': 1 + 'package-with-activation-events': + 'previousData': 'overwritten' + it "absorbs exceptions that are thrown by the package module's serialize methods", -> spyOn(console, 'error') - - atom.activateAtomPackage - name: "bad-egg" - packageMain: - activate: -> - serialize: -> throw new Error("I'm broken") - - atom.activateAtomPackage - name: "good-egg" - packageMain: - activate: -> - serialize: -> "I still get called" + window.loadPackage('package-with-module') + window.loadPackage('package-with-serialize-error', activateImmediately: true) packageStates = atom.serializeAtomPackages() - expect(packageStates['good-egg']).toBe "I still get called" - expect(packageStates['bad-egg']).toBeUndefined() + expect(packageStates['package-with-module']).toEqual someNumber: 1 + expect(packageStates['package-with-serialize-error']).toBeUndefined() expect(console.error).toHaveBeenCalled() diff --git a/spec/fixtures/packages/package-with-activation-events/main.coffee b/spec/fixtures/packages/package-with-activation-events/main.coffee index fc2588ef9..9682242c7 100644 --- a/spec/fixtures/packages/package-with-activation-events/main.coffee +++ b/spec/fixtures/packages/package-with-activation-events/main.coffee @@ -4,3 +4,6 @@ module.exports = activate: -> rootView.getActiveEditor()?.command 'activation-event', => @activationEventCallCount++ + + serialize: -> + previousData: 'overwritten' \ No newline at end of file diff --git a/spec/fixtures/packages/package-with-module/index.coffee b/spec/fixtures/packages/package-with-module/index.coffee index 49287b94d..b10d0b7af 100644 --- a/spec/fixtures/packages/package-with-module/index.coffee +++ b/spec/fixtures/packages/package-with-module/index.coffee @@ -2,7 +2,12 @@ module.exports = configDefaults: numbers: { one: 1, two: 2 } - activate: -> - @activateCalled = true + someNumber: 0 - deactivate: -> \ No newline at end of file + activate: -> + @someNumber = 1 + + deactivate: -> + + serialize: -> + {@someNumber} \ No newline at end of file diff --git a/spec/fixtures/packages/package-with-serialize-error/index.coffee b/spec/fixtures/packages/package-with-serialize-error/index.coffee new file mode 100644 index 000000000..c320111a2 --- /dev/null +++ b/spec/fixtures/packages/package-with-serialize-error/index.coffee @@ -0,0 +1,7 @@ +module.exports = + activate: -> + + deactivate: -> + + serialize: -> + throw new Error("I'm no good at this.") \ No newline at end of file diff --git a/spec/fixtures/packages/package-with-serialize-error/package.cson b/spec/fixtures/packages/package-with-serialize-error/package.cson new file mode 100644 index 000000000..a44445394 --- /dev/null +++ b/spec/fixtures/packages/package-with-serialize-error/package.cson @@ -0,0 +1,5 @@ +# This package loads async, otherwise it would log errors when it +# is automatically serialized when rootView is deactivatated + +'main': 'index.coffee' +'activationEvents': ['activation-event'] diff --git a/src/app/atom.coffee b/src/app/atom.coffee index c0417fb06..b642e5ac8 100644 --- a/src/app/atom.coffee +++ b/src/app/atom.coffee @@ -26,18 +26,16 @@ _.extend atom, serializeAtomPackages: -> packageStates = {} - for pack in @activatedAtomPackages - try - packageStates[pack.name] = pack.packageMain.serialize?() - catch e - console?.error("Exception serializing '#{pack.name}' package's module\n", e.stack) + for pack in @loadedPackages + if pack in @activatedAtomPackages + try + packageStates[pack.name] = pack.packageMain.serialize?() + catch e + console?.error("Exception serializing '#{pack.name}' package's module\n", e.stack) + else + packageStates[pack.name] = @atomPackageStates[pack.name] packageStates - loadPackage: (name, options) -> - packagePath = _.find @getPackagePaths(), (packagePath) -> fs.base(packagePath) == name - pack = Package.build(packagePath) - pack?.load(options) - loadPackages: -> textMatePackages = [] for path in @getPackagePaths()