2015-06-06 10:36:47 -06:00
Beautifiers = require " ../src/beautifiers "
2017-05-28 15:21:10 -06:00
Executable = require " ../src/beautifiers/executable "
2015-06-06 10:36:47 -06:00
beautifiers = new Beautifiers ( )
Beautifier = require " ../src/beautifiers/beautifier "
2016-03-03 17:06:23 -07:00
Languages = require ( ' ../src/languages/ ' )
_ = require ( ' lodash ' )
2016-04-01 15:59:07 -06:00
fs = require ( ' fs ' )
path = require ( ' path ' )
Promise = require ( " bluebird " )
temp = require ( ' temp ' )
temp . track ( )
2014-03-04 00:07:45 -07:00
# Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
#
# To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
# or `fdescribe`). Remove the `f` to unfocus the block.
2015-06-11 19:07:56 -06:00
# Check if Windows
isWindows = process . platform is ' win32 ' or
process . env . OSTYPE is ' cygwin ' or
process . env . OSTYPE is ' msys '
2015-06-06 10:36:47 -06:00
describe " Atom-Beautify " , ->
2014-03-04 00:07:45 -07:00
2015-06-11 18:59:00 -06:00
beforeEach ->
# Activate package
waitsForPromise ->
activationPromise = atom . packages . activatePackage ( ' atom-beautify ' )
# Force activate package
pack = atom . packages . getLoadedPackage ( " atom-beautify " )
pack . activateNow ( )
# Change logger level
2017-06-11 18:36:43 -06:00
atom . config . set ( ' atom-beautify.general.loggerLevel ' , ' info ' )
2015-06-11 18:59:00 -06:00
# Return promise
return activationPromise
2016-04-01 15:59:07 -06:00
afterEach ->
temp . cleanupSync ( )
2015-06-11 18:59:00 -06:00
describe " Beautifiers " , ->
2016-03-29 22:28:48 -06:00
beautifier = null
2015-06-11 18:59:00 -06:00
2016-03-29 22:28:48 -06:00
beforeEach ->
beautifier = new Beautifier ( )
2015-06-11 18:59:00 -06:00
2016-03-29 22:28:48 -06:00
describe " Beautifier::run " , ->
2015-06-11 18:59:00 -06:00
it " should error when beautifier ' s program not found " , ->
expect ( beautifier ) . not . toBe ( null )
expect ( beautifier instanceof Beautifier ) . toBe ( true )
# waitsForRuns = (fn, message, timeout) ->
# isCompleted = false
# completed = ->
# console.log('completed')
# isCompleted = true
# runs ->
# console.log('runs')
# fn(completed)
# waitsFor(->
# console.log('waitsFor', isCompleted)
# isCompleted
# , message, timeout)
#
# waitsForRuns((cb) ->
# console.log('waitsForRuns', cb)
# setTimeout(cb, 2000)
# , "Waiting for beautification to complete", 5000)
waitsForPromise shouldReject: true , ->
p = beautifier . run ( " program " , [ ] )
expect ( p ) . not . toBe ( null )
expect ( p instanceof beautifier . Promise ) . toBe ( true )
cb = (v) ->
# console.log(v)
expect ( v ) . not . toBe ( null )
expect ( v instanceof Error ) . toBe ( true )
expect ( v . code ) . toBe ( " CommandNotFound " )
expect ( v . description ) . toBe ( undefined , \
' Error should not have a description. ' )
return v
p . then ( cb , cb )
return p
it " should error with help description \
2015-06-11 14:37:23 -06:00
when beautifier ' s program not found " , ->
2015-06-11 18:59:00 -06:00
expect ( beautifier ) . not . toBe ( null )
expect ( beautifier instanceof Beautifier ) . toBe ( true )
waitsForPromise shouldReject: true , ->
help = {
link: " http://test.com "
program: " test-program "
pathOption: " Lang - Test Program Path "
}
p = beautifier . run ( " program " , [ ] , help: help )
expect ( p ) . not . toBe ( null )
expect ( p instanceof beautifier . Promise ) . toBe ( true )
cb = (v) ->
# console.log(v)
expect ( v ) . not . toBe ( null )
expect ( v instanceof Error ) . toBe ( true )
expect ( v . code ) . toBe ( " CommandNotFound " )
expect ( v . description ) . not . toBe ( null )
expect ( v . description . indexOf ( help . link ) ) . not . toBe ( - 1 )
expect ( v . description . indexOf ( help . program ) ) . not . toBe ( - 1 )
expect ( v . description
. indexOf ( help . pathOption ) ) . not . toBe ( - 1 , \
" Error should have a description. " )
return v
p . then ( cb , cb )
return p
it " should error with Windows-specific help description \
2015-06-11 14:37:23 -06:00
when beautifier ' s program not found " , ->
2015-06-11 18:59:00 -06:00
expect ( beautifier ) . not . toBe ( null )
expect ( beautifier instanceof Beautifier ) . toBe ( true )
waitsForPromise shouldReject: true , ->
help = {
link: " http://test.com "
program: " test-program "
pathOption: " Lang - Test Program Path "
}
# Force to be Windows
2017-05-28 15:21:10 -06:00
Executable.isWindows = () -> true
2015-06-11 18:59:00 -06:00
terminal = ' CMD prompt '
whichCmd = " where.exe "
# Process
p = beautifier . run ( " program " , [ ] , help: help )
expect ( p ) . not . toBe ( null )
expect ( p instanceof beautifier . Promise ) . toBe ( true )
cb = (v) ->
2017-05-28 15:21:10 -06:00
console . log ( " error " , v , v . description )
2015-06-11 18:59:00 -06:00
expect ( v ) . not . toBe ( null )
expect ( v instanceof Error ) . toBe ( true )
expect ( v . code ) . toBe ( " CommandNotFound " )
expect ( v . description ) . not . toBe ( null )
expect ( v . description . indexOf ( help . link ) ) . not . toBe ( - 1 )
expect ( v . description . indexOf ( help . program ) ) . not . toBe ( - 1 )
expect ( v . description
. indexOf ( help . pathOption ) ) . not . toBe ( - 1 , \
" Error should have a description. " )
expect ( v . description
. indexOf ( terminal ) ) . not . toBe ( - 1 , \
" Error should have a description including \
2015-06-11 14:37:23 -06:00
' # {terminal} ' in message . " )
2015-06-11 18:59:00 -06:00
expect ( v . description
. indexOf ( whichCmd ) ) . not . toBe ( - 1 , \
" Error should have a description including \
2015-06-11 14:37:23 -06:00
' # {whichCmd} ' in message . " )
2015-06-11 18:59:00 -06:00
return v
p . then ( cb , cb )
return p
2015-06-11 14:37:23 -06:00
2015-06-11 19:07:56 -06:00
unless isWindows
it " should error with Mac/Linux-specific help description \
when beautifier ' s program not found " , ->
expect ( beautifier ) . not . toBe ( null )
expect ( beautifier instanceof Beautifier ) . toBe ( true )
waitsForPromise shouldReject: true , ->
help = {
link: " http://test.com "
program: " test-program "
pathOption: " Lang - Test Program Path "
}
# Force to be Mac/Linux (not Windows)
2017-05-28 15:21:10 -06:00
Executable.isWindows = () -> false
2015-06-11 19:07:56 -06:00
terminal = " Terminal "
whichCmd = " which "
# Process
p = beautifier . run ( " program " , [ ] , help: help )
expect ( p ) . not . toBe ( null )
expect ( p instanceof beautifier . Promise ) . toBe ( true )
cb = (v) ->
# console.log(v)
expect ( v ) . not . toBe ( null )
expect ( v instanceof Error ) . toBe ( true )
expect ( v . code ) . toBe ( " CommandNotFound " )
expect ( v . description ) . not . toBe ( null )
expect ( v . description . indexOf ( help . link ) ) . not . toBe ( - 1 )
expect ( v . description . indexOf ( help . program ) ) . not . toBe ( - 1 )
expect ( v . description
. indexOf ( terminal ) ) . not . toBe ( - 1 , \
" Error should have a description including \
' # {terminal} ' in message . " )
expect ( v . description
. indexOf ( whichCmd ) ) . not . toBe ( - 1 , \
" Error should have a description including \
' # {whichCmd} ' in message . " )
return v
p . then ( cb , cb )
return p
2016-03-03 17:06:23 -07:00
2016-03-29 22:28:48 -06:00
describe " Options " , ->
editor = null
beautifier = null
workspaceElement = atom . views . getView ( atom . workspace )
beforeEach ->
beautifier = new Beautifiers ( )
waitsForPromise ->
atom . workspace . open ( ) . then (e) ->
editor = e
expect ( editor . getText ( ) ) . toEqual ( " " )
2016-04-01 13:57:30 -06:00
describe " Migrate Settings " , ->
migrateSettings = (beforeKey, afterKey, val) ->
# set old options
atom . config . set ( " atom-beautify. #{ beforeKey } " , val )
atom . commands . dispatch workspaceElement , " atom-beautify:migrate-settings "
# Check resulting config
expect ( _ . has ( atom . config . get ( ' atom-beautify ' ) , beforeKey ) ) . toBe ( false )
expect ( atom . config . get ( " atom-beautify. #{ afterKey } " ) ) . toBe ( val )
it " should migrate js_indent_size to js.indent_size " , ->
2016-04-07 08:35:15 -06:00
migrateSettings ( " js_indent_size " , " js.indent_size " , 1 )
2016-04-01 13:57:30 -06:00
migrateSettings ( " js_indent_size " , " js.indent_size " , 10 )
it " should migrate analytics to general.analytics " , ->
migrateSettings ( " analytics " , " general.analytics " , true )
2016-04-07 08:35:15 -06:00
migrateSettings ( " analytics " , " general.analytics " , false )
2016-04-01 13:57:30 -06:00
it " should migrate _analyticsUserId to general._analyticsUserId " , ->
migrateSettings ( " _analyticsUserId " , " general._analyticsUserId " , " userid " )
2016-04-07 08:35:15 -06:00
migrateSettings ( " _analyticsUserId " , " general._analyticsUserId " , " userid2 " )
it " should migrate language_js_disabled to js.disabled " , ->
migrateSettings ( " language_js_disabled " , " js.disabled " , false )
migrateSettings ( " language_js_disabled " , " js.disabled " , true )
it " should migrate language_js_default_beautifier to js.default_beautifier " , ->
migrateSettings ( " language_js_default_beautifier " , " js.default_beautifier " , " Pretty Diff " )
migrateSettings ( " language_js_default_beautifier " , " js.default_beautifier " , " JS Beautify " )
it " should migrate language_js_beautify_on_save to js.beautify_on_save " , ->
migrateSettings ( " language_js_beautify_on_save " , " js.beautify_on_save " , true )
migrateSettings ( " language_js_beautify_on_save " , " js.beautify_on_save " , false )
2016-04-01 13:57:30 -06:00
2016-03-29 22:28:48 -06:00
beautifyEditor = (callback) ->
isComplete = false
beforeText = null
delay = 500
runs ->
beforeText = editor . getText ( )
atom . commands . dispatch workspaceElement , " atom-beautify:beautify-editor "
setTimeout ( ->
isComplete = true
, delay )
waitsFor ->
isComplete
runs ->
afterText = editor . getText ( )
expect ( typeof beforeText ) . toBe ( ' string ' )
expect ( typeof afterText ) . toBe ( ' string ' )
return callback ( beforeText , afterText )
describe " JavaScript " , ->
beforeEach ->
waitsForPromise ->
packName = ' language-javascript '
atom . packages . activatePackage ( packName )
runs ->
# Setup Editor
code = " var hello= ' world ' ;function(){console.log( ' hello ' +hello)} "
editor . setText ( code )
# console.log(atom.grammars.grammarsByScopeName)
grammar = atom . grammars . selectGrammar ( ' source.js ' )
expect ( grammar . name ) . toBe ( ' JavaScript ' )
editor . setGrammar ( grammar )
expect ( editor . getGrammar ( ) . name ) . toBe ( ' JavaScript ' )
# See https://discuss.atom.io/t/solved-settimeout-not-working-firing-in-specs-tests/11427/17
jasmine . unspy ( window , ' setTimeout ' )
2016-04-01 13:57:30 -06:00
# afterEach ->
# atom.packages.deactivatePackages()
# atom.packages.unloadPackages()
2016-03-29 22:28:48 -06:00
describe " .jsbeautifyrc " , ->
2016-04-01 15:59:07 -06:00
it " should look at directories above file " , ->
isDone = false
cb = (err) ->
isDone = true
expect ( err ) . toBe ( undefined )
runs ->
try
# console.log('runs')
# Make top directory
temp . mkdir ( ' dir1 ' , (err, dirPath) ->
# console.log(arguments)
return cb ( err ) if err
# Add .jsbeautifyrc file
rcPath = path . join ( dirPath , ' .jsbeautifyrc ' )
myData1 = {
indent_size: 1 ,
indent_char: ' \t '
}
myData = JSON . stringify ( myData1 )
fs . writeFile ( rcPath , myData , (err) ->
# console.log(arguments)
return cb ( err ) if err
# Make next directory
dirPath = path . join ( dirPath , ' dir2 ' )
fs . mkdir ( dirPath , (err) ->
# console.log(arguments)
return cb ( err ) if err
# Add .jsbeautifyrc file
rcPath = path . join ( dirPath , ' .jsbeautifyrc ' )
myData2 = {
indent_size: 2 ,
indent_char: ' '
}
myData = JSON . stringify ( myData2 )
fs . writeFile ( rcPath , myData , (err) ->
# console.log(arguments)
return cb ( err ) if err
Promise . all ( beautifier . getOptionsForPath ( rcPath , null ) )
. then ( (allOptions) ->
# console.log('allOptions', allOptions)
# Extract options
[
editorOptions
configOptions
homeOptions
editorConfigOptions
] = allOptions
projectOptions = allOptions [ 4 . . ]
# Check that we extracted .jsbeautifyrc files
[ config1 , config2 ] = projectOptions [ - 2 . . ]
expect ( _ . get ( config1 , ' _default.indent_size ' ) ) . toBe ( myData1 . indent_size )
expect ( _ . get ( config2 , ' _default.indent_size ' ) ) . toBe ( myData2 . indent_size )
expect ( _ . get ( config1 , ' _default.indent_char ' ) ) . toBe ( myData1 . indent_char )
expect ( _ . get ( config2 , ' _default.indent_char ' ) ) . toBe ( myData2 . indent_char )
cb ( )
)
)
)
)
)
catch err
cb ( err )
waitsFor ->
isDone
2016-03-29 22:28:48 -06:00
describe " Package settings " , ->
getOptions = (callback) ->
options = null
waitsForPromise ->
2016-04-01 13:57:30 -06:00
# console.log('beautifier', beautifier.getOptionsForPath, beautifier)
2016-03-29 22:28:48 -06:00
allOptions = beautifier . getOptionsForPath ( null , null )
# Resolve options with promises
return Promise . all ( allOptions )
. then ( (allOptions) ->
options = allOptions
)
runs ->
callback ( options )
it " should change indent_size to 1 " , ->
atom . config . set ( ' atom-beautify.js.indent_size ' , 1 )
getOptions (allOptions) ->
expect ( typeof allOptions ) . toBe ( ' object ' )
configOptions = allOptions [ 1 ]
expect ( typeof configOptions ) . toBe ( ' object ' )
expect ( configOptions . js . indent_size ) . toBe ( 1 )
beautifyEditor (beforeText, afterText) ->
# console.log(beforeText, afterText, editor)
expect ( afterText ) . toBe ( """ var hello = ' world ' ;
function ( ) {
console . log ( ' hello ' + hello )
} """ )
it " should change indent_size to 10 " , ->
atom . config . set ( ' atom-beautify.js.indent_size ' , 10 )
getOptions (allOptions) ->
expect ( typeof allOptions ) . toBe ( ' object ' )
configOptions = allOptions [ 1 ]
expect ( typeof configOptions ) . toBe ( ' object ' )
expect ( configOptions . js . indent_size ) . toBe ( 10 )
beautifyEditor (beforeText, afterText) ->
# console.log(beforeText, afterText, editor)
expect ( afterText ) . toBe ( """ var hello = ' world ' ;
function ( ) {
console . log ( ' hello ' + hello )
} """ )
2016-03-03 17:06:23 -07:00
describe " Languages " , ->
languages = null
beforeEach ->
languages = new Languages ( )
describe " Languages::namespace " , ->
it " should verify that multiple languages do not share the same namespace " , ->
namespaceGroups = _ . groupBy ( languages . languages , " namespace " )
namespacePairs = _ . toPairs ( namespaceGroups )
namespaceOverlap = _ . filter ( namespacePairs , ([namespace, group]) -> group . length > 1 )
2016-04-01 13:57:30 -06:00
# console.log('namespaces', namespaceGroups, namespacePairs, namespaceOverlap)
2016-03-03 17:06:23 -07:00
expect ( namespaceOverlap . length ) . toBe ( 0 , \
" Language namespaces are overlapping. \n \
Namespaces are unique: only one language for each namespace . \ n " +
_ . map ( namespaceOverlap , ([namespace, group]) -> " - ' #{ namespace } ' : Check languages #{ _ . map ( group , ' name ' ) . join ( ' , ' ) } for using namespace ' #{ namespace } ' . " ) . join ( ' \n ' )
)