Only require packages that have a main module

This commit is contained in:
Corey Johnson
2013-02-08 14:34:47 -08:00
committed by Corey Johnson & Kevin Sawicki
parent e04ecf836f
commit 14cfa22d22
2 changed files with 37 additions and 16 deletions

View File

@@ -4,18 +4,19 @@ fs = require 'fs'
describe "AtomPackage", ->
describe ".load()", ->
[packageMainModule, pack, rootView] = []
beforeEach ->
rootView = new RootView(fixturesProject.getPath())
pack = new AtomPackage(fs.resolve(config.packageDirPaths..., 'package-with-activation-events'))
packageMainModule = require 'fixtures/packages/package-with-activation-events/main'
spyOn(packageMainModule, 'activate').andCallThrough()
pack.load()
afterEach ->
rootView.deactivate()
describe "when the package metadata includes activation events", ->
[packageMainModule, pack] = []
beforeEach ->
new RootView(fixturesProject.getPath())
pack = new AtomPackage(fs.resolve(config.packageDirPaths..., 'package-with-activation-events'))
packageMainModule = require 'fixtures/packages/package-with-activation-events/main'
spyOn(packageMainModule, 'activate').andCallThrough()
pack.load()
it "defers activating the package until an activation event bubbles to the root view", ->
expect(packageMainModule.activate).not.toHaveBeenCalled()
rootView.trigger 'activation-event'
@@ -34,3 +35,26 @@ describe "AtomPackage", ->
expect(packageMainModule.activationEventCallCount).toBe 2
expect(eventHandler.callCount).toBe 2
expect(packageMainModule.activate.callCount).toBe 1
describe "when the package does not specify a main module", ->
describe "when the package has an index.coffee", ->
it "uses index.coffee as the main module", ->
new RootView(fixturesProject.getPath())
pack = new AtomPackage(fs.resolve(config.packageDirPaths..., 'package-with-module'))
packageMainModule = require 'fixtures/packages/package-with-module'
spyOn(packageMainModule, 'activate').andCallThrough()
expect(packageMainModule.activate).not.toHaveBeenCalled()
pack.load()
expect(packageMainModule.activate).toHaveBeenCalled()
describe "when the package doesn't have an index.coffee", ->
it "does not throw an exception or log an error", ->
spyOn(console, "error")
spyOn(console, "warn")
new RootView(fixturesProject.getPath())
pack = new AtomPackage(fs.resolve(config.packageDirPaths..., 'package-with-keymaps-manifest'))
expect(-> pack.load()).not.toThrow()
expect(console.error).not.toHaveBeenCalled()
expect(console.warn).not.toHaveBeenCalled()

View File

@@ -64,16 +64,13 @@ class AtomPackage extends Package
rootView.command(event, selector, activateHandler) for event, selector of activationEvents
activatePackageMain: ->
if @packageMain = @getPackageMain()
mainPath = @path
mainPath = fs.join(mainPath, @metadata.main) if @metadata.main
mainPath = require.resolve(mainPath)
if fs.isFile(mainPath)
@packageMain = require(mainPath)
rootView?.activatePackage(@name, @packageMain)
getPackageMain: ->
mainPath = require.resolve(fs.join(@path, @metadata.main)) if @metadata.main
if mainPath
require(mainPath)
else if require.resolve(@path)
this
getActivationEvents: -> @metadata.activationEvents
loadMetadata: ->