2013-09-16 23:15:09 +04:00
path = require ' path '
2014-07-26 02:19:16 +04:00
Package = require ' ../src/package '
2014-02-18 00:59:03 +04:00
ThemePackage = require ' ../src/theme-package '
2015-11-24 04:35:38 +03:00
{ mockLocalStorage } = require ' ./spec-helper '
2013-09-16 23:15:09 +04:00
2014-02-18 00:59:03 +04:00
describe " Package " , ->
2015-10-06 19:53:50 +03:00
build = (constructor, path) ->
2015-10-10 03:26:19 +03:00
new constructor (
path: path , packageManager: atom . packages , config: atom . config ,
styleManager: atom . styles , notificationManager: atom . notifications ,
keymapManager: atom . keymaps , commandRegistry: atom . command ,
grammarRegistry: atom . grammars , themeManager: atom . themes ,
menuManager: atom . menu , contextMenuManager: atom . contextMenu ,
2017-03-28 13:26:03 +03:00
deserializerManager: atom . deserializers , viewRegistry: atom . views
2015-10-10 03:26:19 +03:00
)
2015-10-06 19:53:50 +03:00
buildPackage = (packagePath) -> build ( Package , packagePath )
buildThemePackage = (themePath) -> build ( ThemePackage , themePath )
2014-07-26 02:19:16 +04:00
describe " when the package contains incompatible native modules " , ->
2014-07-31 19:42:26 +04:00
beforeEach ->
2017-03-28 13:26:03 +03:00
atom.packages.devMode = false
2015-11-24 04:35:38 +03:00
mockLocalStorage ( )
2014-07-31 19:42:26 +04:00
2017-03-28 13:26:03 +03:00
afterEach ->
atom.packages.devMode = true
2014-07-26 02:19:16 +04:00
it " does not activate it " , ->
2016-08-02 22:14:00 +03:00
packagePath = atom . project . getDirectories ( ) [ 0 ] . resolve ( ' packages/package-with-incompatible-native-module ' )
2015-10-06 19:53:50 +03:00
pack = buildPackage ( packagePath )
2014-07-26 02:19:16 +04:00
expect ( pack . isCompatible ( ) ) . toBe false
expect ( pack . incompatibleModules [ 0 ] . name ) . toBe ' native-module '
expect ( pack . incompatibleModules [ 0 ] . path ) . toBe path . join ( packagePath , ' node_modules ' , ' native-module ' )
2015-09-18 02:21:34 +03:00
it " utilizes _atomModuleCache if present to determine the package ' s native dependencies " , ->
2016-08-02 22:14:00 +03:00
packagePath = atom . project . getDirectories ( ) [ 0 ] . resolve ( ' packages/package-with-ignored-incompatible-native-module ' )
2015-10-06 19:53:50 +03:00
pack = buildPackage ( packagePath )
2015-09-18 02:21:34 +03:00
expect ( pack . getNativeModuleDependencyPaths ( ) . length ) . toBe ( 1 ) # doesn't see the incompatible module
2015-09-17 09:54:44 +03:00
expect ( pack . isCompatible ( ) ) . toBe true
2015-09-18 02:21:34 +03:00
packagePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/package-with-cached-incompatible-native-module ' )
2015-10-06 19:53:50 +03:00
pack = buildPackage ( packagePath )
2015-09-18 02:21:34 +03:00
expect ( pack . isCompatible ( ) ) . toBe false
2015-09-17 09:54:44 +03:00
2014-07-26 02:34:03 +04:00
it " caches the incompatible native modules in local storage " , ->
2016-08-02 22:14:00 +03:00
packagePath = atom . project . getDirectories ( ) [ 0 ] . resolve ( ' packages/package-with-incompatible-native-module ' )
2015-10-06 19:53:50 +03:00
expect ( buildPackage ( packagePath ) . isCompatible ( ) ) . toBe false
2014-07-26 02:34:03 +04:00
expect ( global . localStorage . getItem . callCount ) . toBe 1
expect ( global . localStorage . setItem . callCount ) . toBe 1
2015-10-06 19:53:50 +03:00
expect ( buildPackage ( packagePath ) . isCompatible ( ) ) . toBe false
2014-07-26 02:34:03 +04:00
expect ( global . localStorage . getItem . callCount ) . toBe 2
expect ( global . localStorage . setItem . callCount ) . toBe 1
2016-08-02 22:14:00 +03:00
it " logs an error to the console describing the problem " , ->
packagePath = atom . project . getDirectories ( ) [ 0 ] . resolve ( ' packages/package-with-incompatible-native-module ' )
spyOn ( console , ' warn ' )
spyOn ( atom . notifications , ' addFatalError ' )
buildPackage ( packagePath ) . activateNow ( )
expect ( atom . notifications . addFatalError ) . not . toHaveBeenCalled ( )
expect ( console . warn . callCount ) . toBe ( 1 )
expect ( console . warn . mostRecentCall . args [ 0 ] ) . toContain ( ' it requires one or more incompatible native modules (native-module) ' )
2015-08-22 01:14:22 +03:00
describe " ::rebuild() " , ->
2015-08-22 02:59:28 +03:00
beforeEach ->
2017-03-28 13:26:03 +03:00
atom.packages.devMode = false
2015-11-24 04:35:38 +03:00
mockLocalStorage ( )
2015-08-22 02:59:28 +03:00
2017-03-28 13:26:03 +03:00
afterEach ->
atom.packages.devMode = true
2015-08-22 01:14:22 +03:00
it " returns a promise resolving to the results of `apm rebuild` " , ->
packagePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/package-with-index ' )
2015-10-06 19:53:50 +03:00
pack = buildPackage ( packagePath )
2015-08-22 01:14:22 +03:00
rebuildCallbacks = [ ]
spyOn ( pack , ' runRebuildProcess ' ) . andCallFake ( (callback) -> rebuildCallbacks . push ( callback ) )
promise = pack . rebuild ( )
rebuildCallbacks [ 0 ] ( { code: 0 , stdout: ' stdout output ' , stderr: ' stderr output ' } )
waitsFor (done) ->
promise . then (result) ->
expect ( result ) . toEqual { code: 0 , stdout: ' stdout output ' , stderr: ' stderr output ' }
done ( )
it " persists build failures in local storage " , ->
packagePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/package-with-index ' )
2015-10-06 19:53:50 +03:00
pack = buildPackage ( packagePath )
2015-08-22 01:14:22 +03:00
expect ( pack . isCompatible ( ) ) . toBe true
expect ( pack . getBuildFailureOutput ( ) ) . toBeNull ( )
rebuildCallbacks = [ ]
spyOn ( pack , ' runRebuildProcess ' ) . andCallFake ( (callback) -> rebuildCallbacks . push ( callback ) )
pack . rebuild ( )
rebuildCallbacks [ 0 ] ( { code: 13 , stderr: ' It is broken ' } )
expect ( pack . getBuildFailureOutput ( ) ) . toBe ' It is broken '
expect ( pack . getIncompatibleNativeModules ( ) ) . toEqual [ ]
expect ( pack . isCompatible ( ) ) . toBe false
2015-08-22 02:59:28 +03:00
# A different package instance has the same failure output (simulates reload)
2015-10-06 19:53:50 +03:00
pack2 = buildPackage ( packagePath )
2015-08-22 01:14:22 +03:00
expect ( pack2 . getBuildFailureOutput ( ) ) . toBe ' It is broken '
expect ( pack2 . isCompatible ( ) ) . toBe false
2015-08-22 02:59:28 +03:00
# Clears the build failure after a successful build
pack . rebuild ( )
rebuildCallbacks [ 1 ] ( { code: 0 , stdout: ' It worked ' } )
expect ( pack . getBuildFailureOutput ( ) ) . toBeNull ( )
expect ( pack2 . getBuildFailureOutput ( ) ) . toBeNull ( )
it " sets cached incompatible modules to an empty array when the rebuild completes (there may be a build error, but rebuilding *deletes* native modules) " , ->
packagePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/package-with-incompatible-native-module ' )
2015-10-06 19:53:50 +03:00
pack = buildPackage ( packagePath )
2015-08-22 02:59:28 +03:00
expect ( pack . getIncompatibleNativeModules ( ) . length ) . toBeGreaterThan ( 0 )
rebuildCallbacks = [ ]
spyOn ( pack , ' runRebuildProcess ' ) . andCallFake ( (callback) -> rebuildCallbacks . push ( callback ) )
pack . rebuild ( )
expect ( pack . getIncompatibleNativeModules ( ) . length ) . toBeGreaterThan ( 0 )
rebuildCallbacks [ 0 ] ( { code: 0 , stdout: ' It worked ' } )
expect ( pack . getIncompatibleNativeModules ( ) . length ) . toBe ( 0 )
2015-08-22 01:14:22 +03:00
2013-09-16 23:15:09 +04:00
describe " theme " , ->
2015-09-04 01:59:37 +03:00
[ editorElement , theme ] = [ ]
2013-09-16 23:15:09 +04:00
beforeEach ->
2015-09-04 01:59:37 +03:00
editorElement = document . createElement ( ' atom-text-editor ' )
jasmine . attachToDOM ( editorElement )
2013-09-16 23:15:09 +04:00
afterEach ->
2017-09-07 00:52:08 +03:00
waitsForPromise ->
2017-09-08 23:19:36 +03:00
Promise . resolve ( theme . deactivate ( ) ) if theme ?
2013-09-16 23:15:09 +04:00
describe " when the theme contains a single style file " , ->
it " loads and applies css " , ->
2015-09-04 01:59:37 +03:00
expect ( getComputedStyle ( editorElement ) . paddingBottom ) . not . toBe " 1234px "
2015-01-10 01:54:54 +03:00
themePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/theme-with-index-css ' )
2015-10-06 19:53:50 +03:00
theme = buildThemePackage ( themePath )
2013-09-16 23:15:09 +04:00
theme . activate ( )
2015-09-04 01:59:37 +03:00
expect ( getComputedStyle ( editorElement ) . paddingTop ) . toBe " 1234px "
2013-09-16 23:15:09 +04:00
it " parses, loads and applies less " , ->
2015-09-04 01:59:37 +03:00
expect ( getComputedStyle ( editorElement ) . paddingBottom ) . not . toBe " 1234px "
2015-01-10 01:54:54 +03:00
themePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/theme-with-index-less ' )
2015-10-06 19:53:50 +03:00
theme = buildThemePackage ( themePath )
2013-09-16 23:15:09 +04:00
theme . activate ( )
2015-09-04 01:59:37 +03:00
expect ( getComputedStyle ( editorElement ) . paddingTop ) . toBe " 4321px "
2013-09-16 23:15:09 +04:00
describe " when the theme contains a package.json file " , ->
it " loads and applies stylesheets from package.json in the correct order " , ->
2015-09-04 01:59:37 +03:00
expect ( getComputedStyle ( editorElement ) . paddingTop ) . not . toBe ( " 101px " )
expect ( getComputedStyle ( editorElement ) . paddingRight ) . not . toBe ( " 102px " )
expect ( getComputedStyle ( editorElement ) . paddingBottom ) . not . toBe ( " 103px " )
2013-09-16 23:15:09 +04:00
2015-01-10 01:54:54 +03:00
themePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/theme-with-package-file ' )
2015-10-06 19:53:50 +03:00
theme = buildThemePackage ( themePath )
2013-09-16 23:15:09 +04:00
theme . activate ( )
2015-09-04 01:59:37 +03:00
expect ( getComputedStyle ( editorElement ) . paddingTop ) . toBe ( " 101px " )
expect ( getComputedStyle ( editorElement ) . paddingRight ) . toBe ( " 102px " )
expect ( getComputedStyle ( editorElement ) . paddingBottom ) . toBe ( " 103px " )
2013-09-16 23:15:09 +04:00
describe " when the theme does not contain a package.json file and is a directory " , ->
it " loads all stylesheet files in the directory " , ->
2015-09-04 01:59:37 +03:00
expect ( getComputedStyle ( editorElement ) . paddingTop ) . not . toBe " 10px "
expect ( getComputedStyle ( editorElement ) . paddingRight ) . not . toBe " 20px "
expect ( getComputedStyle ( editorElement ) . paddingBottom ) . not . toBe " 30px "
2013-09-16 23:15:09 +04:00
2015-01-10 01:54:54 +03:00
themePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/theme-without-package-file ' )
2015-10-06 19:53:50 +03:00
theme = buildThemePackage ( themePath )
2013-09-16 23:15:09 +04:00
theme . activate ( )
2015-09-04 01:59:37 +03:00
expect ( getComputedStyle ( editorElement ) . paddingTop ) . toBe " 10px "
expect ( getComputedStyle ( editorElement ) . paddingRight ) . toBe " 20px "
expect ( getComputedStyle ( editorElement ) . paddingBottom ) . toBe " 30px "
2013-09-16 23:15:09 +04:00
describe " reloading a theme " , ->
beforeEach ->
2015-01-10 01:54:54 +03:00
themePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/theme-with-package-file ' )
2015-10-06 19:53:50 +03:00
theme = buildThemePackage ( themePath )
2013-09-16 23:15:09 +04:00
theme . activate ( )
it " reloads without readding to the stylesheets list " , ->
expect ( theme . getStylesheetPaths ( ) . length ) . toBe 3
2014-10-16 23:16:57 +04:00
theme . reloadStylesheets ( )
2013-09-16 23:15:09 +04:00
expect ( theme . getStylesheetPaths ( ) . length ) . toBe 3
describe " events " , ->
beforeEach ->
2015-01-10 01:54:54 +03:00
themePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/theme-with-package-file ' )
2015-10-06 19:53:50 +03:00
theme = buildThemePackage ( themePath )
2013-09-16 23:15:09 +04:00
theme . activate ( )
it " deactivated event fires on .deactivate() " , ->
2014-09-10 02:09:18 +04:00
theme . onDidDeactivate spy = jasmine . createSpy ( )
2017-09-07 00:52:08 +03:00
waitsForPromise ->
2017-09-08 23:19:36 +03:00
Promise . resolve ( theme . deactivate ( ) )
2017-09-07 00:52:08 +03:00
runs ->
expect ( spy ) . toHaveBeenCalled ( )
2015-06-09 03:57:35 +03:00
describe " .loadMetadata() " , ->
2016-09-26 20:52:13 +03:00
[ packagePath , metadata ] = [ ]
2015-06-09 03:57:35 +03:00
beforeEach ->
packagePath = atom . project . getDirectories ( ) [ 0 ] ? . resolve ( ' packages/package-with-different-directory-name ' )
2015-10-01 04:42:37 +03:00
metadata = atom . packages . loadPackageMetadata ( packagePath , true )
2015-06-09 03:57:35 +03:00
it " uses the package name defined in package.json " , ->
expect ( metadata . name ) . toBe ' package-with-a-totally-different-name '
2016-11-30 03:31:57 +03:00
describe " the initialize() hook " , ->
it " gets called when the package is activated " , ->
packagePath = atom . project . getDirectories ( ) [ 0 ] . resolve ( ' packages/package-with-deserializers ' )
pack = buildPackage ( packagePath )
pack . requireMainModule ( )
mainModule = pack . mainModule
spyOn ( mainModule , ' initialize ' )
expect ( mainModule . initialize ) . not . toHaveBeenCalled ( )
pack . activate ( )
expect ( mainModule . initialize ) . toHaveBeenCalled ( )
expect ( mainModule . initialize . callCount ) . toBe ( 1 )
it " gets called when a deserializer is used " , ->
packagePath = atom . project . getDirectories ( ) [ 0 ] . resolve ( ' packages/package-with-deserializers ' )
pack = buildPackage ( packagePath )
pack . requireMainModule ( )
mainModule = pack . mainModule
spyOn ( mainModule , ' initialize ' )
pack . load ( )
expect ( mainModule . initialize ) . not . toHaveBeenCalled ( )
atom . deserializers . deserialize ( { deserializer: ' Deserializer1 ' , a: ' b ' } )
expect ( mainModule . initialize ) . toHaveBeenCalled ( )