Skip to content

Commit

Permalink
chore: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvain-hamel authored and dignifiedquire committed Jun 9, 2015
1 parent 1f8c009 commit cbfbe4e
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .jscs.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@
"requireLineFeedAtFileEnd": true,
"maximumLineLength": 100,
"requireCapitalizedConstructors": true,
"requireDotNotation": true
"requireDotNotation": true,
"disallowSpacesInsideParentheses" : true


}
35 changes: 31 additions & 4 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ JSHINT_NODE =

module.exports = (grunt) ->

allSrc = ['index.js']
allTests = ['test/mocha-globals.coffee', 'test/*.spec.coffee']
all = [].concat(allSrc).concat(allTests).concat(['Gruntfile.coffee'])

# Project configuration.
grunt.initConfig
pkgFile: 'package.json'
Expand All @@ -26,7 +30,7 @@ module.exports = (grunt) ->
jshint:
default:
files:
src: ['index.js']
src: allSrc
options: JSHINT_NODE

options:
Expand Down Expand Up @@ -54,21 +58,44 @@ module.exports = (grunt) ->
globals: {}

jscs:
default: files: src: ['index.js']
default: files: src: allSrc
options:
config: '.jscs.json'

simplemocha:
options:
ui: 'bdd'
reporter: 'dot'
unit:
src: allTests

watch:
files: all
tasks:['default']

# CoffeeLint options
# http://www.coffeelint.org/#options
coffeelint:
unittests: files: src: ['Gruntfile.coffee', 'test/**/*.coffee']
options:
max_line_length:
value: 100

grunt.loadNpmTasks 'grunt-contrib-watch'
grunt.loadNpmTasks 'grunt-npm'
grunt.loadNpmTasks 'grunt-bump'
grunt.loadNpmTasks 'grunt-auto-release'
grunt.loadNpmTasks 'grunt-contrib-jshint'
grunt.loadNpmTasks 'grunt-jscs-checker'
grunt.loadNpmTasks 'grunt-simple-mocha'
grunt.loadNpmTasks 'grunt-coffeelint'

grunt.registerTask 'release', 'Bump the version and publish to NPM.', (type) ->
grunt.task.run [
'npm-contributors',
"bump:#{type||'patch'}",
'npm-publish'
]
grunt.registerTask 'default', ['lint']
grunt.registerTask 'lint', ['jshint', 'jscs']
grunt.registerTask 'test', ['simplemocha']
grunt.registerTask 'default', ['lint', 'test']
grunt.registerTask 'lint', ['jshint', 'jscs', 'coffeelint']
36 changes: 20 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ var exec = require('child_process').exec;

var processName = 'iexplore.exe';

function getInternetExplorerExe() {
var suffix = '\\Internet Explorer\\' + processName,
prefixes = [process.env['' + 'PROGRAMW6432'], // '' + ' trick to keep jscs happy
process.env['' + 'PROGRAMFILES(X86)'],
process.env['' + 'PROGRAMFILES']],
prefix, i;

for (i = 0; i < prefixes.length; i++) {
prefix = prefixes[i];
if (prefix && fs.existsSync(prefix + suffix)) {
return prefix + suffix;
}
}
}

var IEBrowser = function(baseBrowserDecorator, logger, args) {
baseBrowserDecorator(this);

Expand Down Expand Up @@ -76,27 +91,16 @@ var IEBrowser = function(baseBrowserDecorator, logger, args) {
this._onProcessExit = function(code, errorOutput) {
var pid = this._process.pid;
killExtraIEProcess(pid, function() {
baseOnProcessExit(code, errorOutput);
if (baseOnProcessExit) {
baseOnProcessExit(code, errorOutput);
}
});
};

// this is to expose the function for unit testing
this._getInternetExplorerExe = getInternetExplorerExe;
};

function getInternetExplorerExe() {
var suffix = '\\Internet Explorer\\' + processName,
prefixes = [process.env['' + 'PROGRAMW6432'], // '' + ' trick to keep jscs happy
process.env['' + 'PROGRAMFILES(X86)'],
process.env['' + 'PROGRAMFILES']],
prefix, i;

for (i = 0; i < prefixes.length; i++) {
prefix = prefixes[i];
if (prefix && fs.existsSync(prefix + suffix)) {
return prefix + suffix;
}
}
}

IEBrowser.prototype = {
name: 'IE',
DEFAULT_CMD: {
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@
"license": "MIT",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-simple-mocha": "~0.4.0",
"grunt-bump": "~0.0.7",
"grunt-npm": "~0.0.2",
"grunt-auto-release": "~0.0.2",
"grunt-contrib-jshint": "~0.7.2",
"grunt-jscs-checker": "~0.4.0"
"grunt-jscs-checker": "~0.4.0",
"mocks": "0.0.11",
"sinon": "~1.7.3",
"chai": "~1.7.2",
"sinon-chai": "~2.4.0",
"grunt-coffeelint": "~0.0.6",
"karma": "~0.12.0",
"di": "~0.0.1",
"grunt-contrib-watch" : "~0.6.1"
},
"contributors": [
"sylvain-hamel <sylvainhamel0@gmail.com>",
Expand Down
159 changes: 159 additions & 0 deletions test/launcher.spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
util = require 'util'
di = require 'di'
mocks = require 'mocks'
fs = require 'fs'

describe 'launcher', ->
injector = null
launcher = null
module = null
IELauncher = null
EventEmitter = null

beforeEach ->
EventEmitter = require('../node_modules/karma/lib/events').EventEmitter
IELauncher = mocks.loadFile(__dirname + '/../index').module.exports
module = {
'baseBrowserDecorator': ['value', (->)],
'emitter': ['value', new EventEmitter]
'logger': ['value', {
create : ->
return {
error : (->),
debug : (->)
}
}]
'args': ['value', []]
}

afterEach ->
injector = null
launcher = null

describe 'exports', ->
it 'should export launcher:IE', (done) ->
expect(IELauncher['launcher:IE']).to.defined
done()

describe 'initialization', ->

beforeEach ->
injector = new di.Injector([module, IELauncher])
launcher = injector.get('launcher:IE')

it 'should initialize name', (done) ->
expect(launcher.name).to.equal "IE"
done()

it 'should initialize ENV_CMD', (done) ->
expect(launcher.ENV_CMD).to.equal "IE_BIN"
done()

it 'should initialize DEFAULT_CMD.win32', (done) ->
expect(launcher.DEFAULT_CMD.win32).to.beDefined
done()

describe '_getOptions', ->

getOptions = null

beforeEach ->
getOptions = (url, module) ->
injector = new di.Injector([module, IELauncher])
launcher = injector.get('launcher:IE')
launcher._getOptions('url')

it 'should add -extoff', (done) ->
options = getOptions('url', module)
expect(options[0]).to.equal '-extoff'
done()

it 'should include args.flags', (done) ->
module.args[1] = {flags: ['-flag1', '-flag2']}
options = getOptions('url', module)
expect(options[1]).to.equal '-flag1'
expect(options[2]).to.equal '-flag2'
done()

it 'should return url as the last flag', (done) ->
options = getOptions('url', module)
expect(options[options.length-1]).to.equal 'url'
done()

it 'should convert x-ua-compatible arg to encoded url', (done) ->
module.args[1] = {'x-ua-compatible':'browser=mode'}
options = getOptions('url', module)
expect(options[options.length-1]).to.equal 'url?x-ua-compatible=browser%3Dmode'
done()

describe 'locating iexplore.exe', ->

win32Location = null
fsMock = null

beforeEach ->
process.env['' + 'PROGRAMW6432'] = '\\fake\\PROGRAMW6432'
process.env['' + 'PROGRAMFILES(X86)'] = '\\fake\\PROGRAMFILES(X86)'
process.env['' + 'PROGRAMFILES'] = '\\fake\\PROGRAMFILES'

fsMock = mocks.fs.create
'folder1':
'Internet Explorer':
'iexplore.exe' : 1

IELauncher = mocks.loadFile(__dirname + '/../index', {
fs:fsMock
}).module.exports

win32Location = () ->
injector = new di.Injector([module, IELauncher])
launcher = injector.get('launcher:IE')
launcher._getInternetExplorerExe()

it 'should locate in PROGRAMW6432', (done) ->
process.env['' + 'PROGRAMW6432'] = '\\folder1'
expect(win32Location()).to.equal '\\folder1\\Internet Explorer\\iexplore.exe'
done()

it 'should locate in PROGRAMFILES(X86)', (done) ->
process.env['' + 'PROGRAMFILES(X86)'] = '\\folder1'
expect(win32Location()).to.equal '\\folder1\\Internet Explorer\\iexplore.exe'
done()

it 'should locate in PROGRAMFILES', (done) ->
process.env['' + 'PROGRAMFILES'] = '\\folder1'
expect(win32Location()).to.equal '\\folder1\\Internet Explorer\\iexplore.exe'
done()

it 'should return undefined when not found', (done) ->
expect(win32Location()).to.equal undefined
done()

describe '_onProcessExit', ->

onProcessExit = null
child_processCmd = null

beforeEach ->
onProcessExit = () ->

child_processMock = {
exec : (cmd, cb)->
child_processCmd = cmd
cb()
}

IELauncher = mocks.loadFile(__dirname + '/../index', {
child_process:child_processMock
}).module.exports

injector = new di.Injector([module, IELauncher])
launcher = injector.get('launcher:IE')
launcher._process = { pid : 10 }
launcher._onProcessExit(1, 2)

it 'should call wmic with process ID', (done) ->
onProcessExit()
expect(child_processCmd).to.equal 'wmic.exe Path win32_Process where ' +
'\"Name=\'iexplore.exe\' and CommandLine Like \'%SCODEF:10%\'\" call Terminate'
done()
16 changes: 16 additions & 0 deletions test/mocha-globals.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sinon = require 'sinon'
chai = require 'chai'

# publish globals that all specs can use
global.expect = chai.expect
global.should = chai.should()
global.sinon = sinon

# chai plugins
chai.use(require 'sinon-chai')

beforeEach ->
global.sinon = sinon.sandbox.create()

afterEach ->
global.sinon.restore()

0 comments on commit cbfbe4e

Please sign in to comment.