-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
T/43: Added tests #44
Changes from 10 commits
638e5fb
584f341
8f48c06
e42e16d
890dc8f
1a0ff0a
284376e
cc8e24f
8407aae
1ef53da
3fdc2e7
8806286
f271fb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,10 +13,9 @@ const exec = require( '../utils/exec' ); | |
module.exports = { | ||
/** | ||
* @param {Object} data | ||
* @param {Object} data.parameters Additional arguments provided by the user. | ||
* @param {String} data.packageName Name of current package to process. | ||
* @param {Options} data.options The options object. | ||
* @param {Repository|null} data.repository | ||
* @param {Repository} data.repository | ||
* @returns {Promise} | ||
*/ | ||
execute( data ) { | ||
|
@@ -25,19 +24,22 @@ module.exports = { | |
return new Promise( ( resolve, reject ) => { | ||
const destinationPath = path.join( data.options.packages, data.repository.directory ); | ||
|
||
// Package is already cloned. | ||
if ( fs.existsSync( destinationPath ) ) { | ||
log.info( `Package "${ data.packageName }" is already cloned. Skipping.` ); | ||
let promise = Promise.resolve(); | ||
|
||
return resolve( { logs: log.all() } ); | ||
// Package is not cloned. | ||
if ( fs.existsSync( destinationPath ) ) { | ||
log.info( `Package "${ data.packageName }" is already cloned.` ); | ||
} else { | ||
const command = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this condition sets the promise to |
||
`git clone --progress ${ data.repository.url } ${ destinationPath }`, | ||
`cd ${ destinationPath }`, | ||
`git checkout --quiet ${ data.repository.branch }` | ||
].join( ' && ' ); | ||
|
||
promise = exec( command ); | ||
} | ||
|
||
const command = | ||
`git clone --progress ${ data.repository.url } ${ destinationPath } && ` + | ||
`cd ${ destinationPath } && ` + | ||
`git checkout --quiet ${ data.repository.branch }`; | ||
|
||
exec( command ) | ||
promise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing return statement (?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see now where resolve and reject parts are. But it looks a little bit strange for me to return a promise which has another promise inside and which is even not returned ;) Maybe it could be written step by step with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A promise inside another promise can not be returned because you need to call OTOH maybe the "wrapper" Promise is not necessary. Who knows? :D |
||
.then( ( output ) => { | ||
log.info( output ); | ||
|
||
|
@@ -47,14 +49,14 @@ module.exports = { | |
|
||
if ( data.options.recursive ) { | ||
const packageJson = require( path.join( destinationPath, 'package.json' ) ); | ||
let packages = []; | ||
const packages = []; | ||
|
||
if ( packageJson.dependencies ) { | ||
packages = packages.concat( Object.keys( packageJson.dependencies ) ); | ||
packages.push( ...Object.keys( packageJson.dependencies ) ); | ||
} | ||
|
||
if ( packageJson.devDependencies ) { | ||
packages = packages.concat( Object.keys( packageJson.devDependencies ) ); | ||
packages.push( ...Object.keys( packageJson.devDependencies ) ); | ||
} | ||
|
||
commandOutput.packages = packages; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/** | ||
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/* jshint mocha:true */ | ||
|
||
'use strict'; | ||
|
||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
const sinon = require( 'sinon' ); | ||
const mockery = require( 'mockery' ); | ||
const expect = require( 'chai' ).expect; | ||
|
||
describe( 'commands/bootstrap', () => { | ||
let bootstrapCommand, sandbox, stubs, data; | ||
|
||
beforeEach( () => { | ||
sandbox = sinon.sandbox.create(); | ||
|
||
mockery.enable( { | ||
useCleanCache: true, | ||
warnOnReplace: false, | ||
warnOnUnregistered: false | ||
} ); | ||
|
||
stubs = { | ||
exec: sandbox.stub(), | ||
fs: { | ||
existsSync: sandbox.stub( fs, 'existsSync' ) | ||
}, | ||
path: { | ||
join: sandbox.stub( path, 'join', ( ...chunks ) => chunks.join( '/' ) ) | ||
} | ||
}; | ||
|
||
data = { | ||
packageName: 'test-package', | ||
options: { | ||
cwd: __dirname, | ||
packages: 'packages' | ||
}, | ||
repository: { | ||
directory: 'test-package', | ||
url: 'git@github.com/organization/test-package.git', | ||
branch: 'master' | ||
} | ||
}; | ||
|
||
mockery.registerMock( '../utils/exec', stubs.exec ); | ||
|
||
bootstrapCommand = require( '../../lib/commands/bootstrap' ); | ||
} ); | ||
|
||
afterEach( () => { | ||
sandbox.restore(); | ||
mockery.disable(); | ||
} ); | ||
|
||
describe( 'execute()', () => { | ||
it( 'rejects promise if something went wrong', () => { | ||
const error = new Error( 'Unexpected error.' ); | ||
|
||
stubs.fs.existsSync.returns( false ); | ||
stubs.exec.returns( Promise.reject( error ) ); | ||
|
||
return bootstrapCommand.execute( data ) | ||
.then( | ||
() => { | ||
throw new Error( 'Supposed to be rejected.' ); | ||
}, | ||
( response ) => { | ||
expect( response.logs.error[ 0 ].split( '\n' )[ 0 ] ).to.equal( `Error: ${ error.message }` ); | ||
} | ||
); | ||
} ); | ||
|
||
it( 'clones a repository if is not available', () => { | ||
stubs.fs.existsSync.returns( false ); | ||
stubs.exec.returns( Promise.resolve( 'Git clone log.' ) ); | ||
|
||
return bootstrapCommand.execute( data ) | ||
.then( ( response ) => { | ||
expect( stubs.exec.calledOnce ).to.equal( true ); | ||
|
||
const cloneCommand = stubs.exec.firstCall.args[ 0 ].split( ' && ' ); | ||
|
||
// Clone the repository. | ||
expect( cloneCommand[ 0 ] ).to.equal( 'git clone --progress git@github.com/organization/test-package.git packages/test-package' ); | ||
// Change the directory to cloned package. | ||
expect( cloneCommand[ 1 ] ).to.equal( 'cd packages/test-package' ); | ||
// And check out to proper branch. | ||
expect( cloneCommand[ 2 ] ).to.equal( 'git checkout --quiet master' ); | ||
|
||
expect( response.logs.info[ 0 ] ).to.equal( 'Git clone log.' ); | ||
} ); | ||
} ); | ||
|
||
it( 'does not clone a repository if is available', () => { | ||
stubs.fs.existsSync.returns( true ); | ||
|
||
return bootstrapCommand.execute( data ) | ||
.then( ( response ) => { | ||
expect( stubs.exec.called ).to.equal( false ); | ||
|
||
expect( response.logs.info[ 0 ] ).to.equal( 'Package "test-package" is already cloned.' ); | ||
} ); | ||
} ); | ||
|
||
it( 'installs dependencies of cloned package', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicated test name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry. The other one is devDeps. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in cc8e24f. |
||
data.options.recursive = true; | ||
data.options.packages = __dirname + '/../fixtures'; | ||
data.repository.directory = 'project-a'; | ||
|
||
stubs.fs.existsSync.returns( true ); | ||
|
||
return bootstrapCommand.execute( data ) | ||
.then( ( response ) => { | ||
expect( response.packages ).is.an( 'array' ); | ||
expect( response.packages ).to.deep.equal( [ 'test-foo' ] ); | ||
} ); | ||
} ); | ||
|
||
it( 'installs devDependencies of cloned package', () => { | ||
data.options.recursive = true; | ||
data.options.packages = __dirname + '/../fixtures'; | ||
data.repository.directory = 'project-with-options-in-mgitjson'; | ||
|
||
stubs.fs.existsSync.returns( true ); | ||
|
||
return bootstrapCommand.execute( data ) | ||
.then( ( response ) => { | ||
expect( response.packages ).is.an( 'array' ); | ||
expect( response.packages ).to.deep.equal( [ 'test-bar' ] ); | ||
} ); | ||
} ); | ||
} ); | ||
|
||
describe( 'afterExecute()', () => { | ||
it( 'informs about number of processed packages', () => { | ||
const consoleLog = sandbox.stub( console, 'log' ); | ||
|
||
const processedPackages = new Set(); | ||
processedPackages.add( 'package-1' ); | ||
processedPackages.add( 'package-2' ); | ||
|
||
bootstrapCommand.afterExecute( processedPackages ); | ||
|
||
expect( consoleLog.calledOnce ).to.equal( true ); | ||
expect( consoleLog.firstCall.args[ 0 ] ).to.match( /2 packages have been processed\./ ); | ||
|
||
consoleLog.restore(); | ||
} ); | ||
} ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong comment.