Skip to content

Commit

Permalink
Sync command prints out directories which are not listed in mgit.json…
Browse files Browse the repository at this point in the history
… but are located in packages directory.
  • Loading branch information
Kamil Piechaczek committed Sep 11, 2018
1 parent a11080f commit eab13b9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
31 changes: 30 additions & 1 deletion lib/commands/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,37 @@ module.exports = {
/**
* @param {Set} parsedPackages Collection of processed packages.
*/
afterExecute( parsedPackages ) {
afterExecute( parsedPackages, commandResponses, mgitOptions ) {
console.log( chalk.cyan( `${ parsedPackages.size } packages have been processed.` ) );

const repositoryResolver = require( mgitOptions.resolverPath );

const repositoryDirectories = Object.keys( mgitOptions.dependencies )
.map( packageName => {
const repository = repositoryResolver( packageName, mgitOptions );

return path.join( mgitOptions.packages, repository.directory );
} );

const skippedPackages = fs.readdirSync( mgitOptions.packages )
.map( pathOrDirectory => {
return path.join( mgitOptions.packages, pathOrDirectory );
} )
.filter( pathOrDirectory => {
if ( !fs.lstatSync( pathOrDirectory ).isDirectory() ) {
return false;
}

return !repositoryDirectories.includes( pathOrDirectory );
} );

console.log(
chalk.yellow( 'Paths to directories listed below are skipped by mgit because they are not defined in "mgit.json":' )
);

skippedPackages.forEach( absolutePath => {
console.log( chalk.yellow( ` - ${ absolutePath }` ) );
} );
},

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ module.exports = function( args, options ) {
return forkPool.killAll()
.then( () => {
if ( command.afterExecute ) {
command.afterExecute( processedPackages, commandResponses );
command.afterExecute( processedPackages, commandResponses, mgitOptions );
}

const endTime = process.hrtime( startTime );
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/getcommandinstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ module.exports = function getCommandInstance( commandName ) {
* - `packages` - an array of packages that mgit should process as well.
*
* @property {Function} [afterExecute] A function that is called by mgit automatically after executing the main command's method.
* This function is called once. It receives two parameters:
* This function is called once. It receives three parameters:
* - a collection (`Set`) that contains all processed packages by mgit.
* - a collection (`Set`) that contains responses returned by `#execute` function.
* - an options object (`Options`) which contains options resolved by mgit.
*/

/**
Expand Down
65 changes: 52 additions & 13 deletions tests/commands/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const mockery = require( 'mockery' );
const expect = require( 'chai' ).expect;

describe( 'commands/sync', () => {
let syncCommand, stubs, commandData;
let syncCommand, stubs, mgitOptions, commandData;

beforeEach( () => {
mockery.enable( {
Expand All @@ -27,7 +27,9 @@ describe( 'commands/sync', () => {
shell: sinon.stub(),
exec: sinon.stub(),
fs: {
existsSync: sinon.stub( fs, 'existsSync' )
existsSync: sinon.stub( fs, 'existsSync' ),
lstatSync: sinon.stub( fs, 'lstatSync' ),
readdirSync: sinon.stub( fs, 'readdirSync' )
},
path: {
join: sinon.stub( path, 'join' ).callsFake( ( ...chunks ) => chunks.join( '/' ) )
Expand All @@ -37,16 +39,20 @@ describe( 'commands/sync', () => {
},
execCommand: {
execute: sinon.stub()
}
},
repositoryResolver: sinon.stub()
};

mgitOptions = {
cwd: __dirname,
packages: __dirname + '/packages',
resolverPath: 'PATH_TO_RESOLVER'
};

commandData = {
arguments: [],
packageName: 'test-package',
mgitOptions: {
cwd: __dirname,
packages: 'packages'
},
mgitOptions,
repository: {
directory: 'test-package',
url: 'git@github.com/organization/test-package.git',
Expand All @@ -56,6 +62,7 @@ describe( 'commands/sync', () => {

mockery.registerMock( './exec', stubs.execCommand );
mockery.registerMock( '../utils/shell', stubs.shell );
mockery.registerMock( 'PATH_TO_RESOLVER', stubs.repositoryResolver );

syncCommand = require( '../../lib/commands/sync' );
} );
Expand Down Expand Up @@ -85,10 +92,11 @@ describe( 'commands/sync', () => {
const cloneCommand = stubs.shell.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"' );
expect( cloneCommand[ 0 ] ).to.equal(
`git clone --progress "git@github.com/organization/test-package.git" "${ __dirname }/packages/test-package"`
);
// Change the directory to cloned package.
expect( cloneCommand[ 1 ] ).to.equal( 'cd "packages/test-package"' );
expect( cloneCommand[ 1 ] ).to.equal( `cd "${ __dirname }/packages/test-package"` );
// And check out to proper branch.
expect( cloneCommand[ 2 ] ).to.equal( 'git checkout --quiet master' );

Expand Down Expand Up @@ -315,17 +323,48 @@ describe( 'commands/sync', () => {
} );

describe( 'afterExecute()', () => {
it( 'informs about number of processed packages', () => {
it( 'informs about number of processed packages and differences between packages in directory and defined in mgit.json', () => {
const consoleLog = sinon.stub( console, 'log' );

const processedPackages = new Set();
processedPackages.add( 'package-1' );
processedPackages.add( 'package-2' );

syncCommand.afterExecute( processedPackages );
mgitOptions.dependencies = {
'package-1': 'foo/package-1',
'package-2': 'foo/package-2',
};

stubs.repositoryResolver.onFirstCall().returns( { directory: 'package-1' } );
stubs.repositoryResolver.onSecondCall().returns( { directory: 'package-2' } );

stubs.fs.readdirSync.returns( [
'package-1',
'package-2',
'package-3',
'.DS_Store'
] );

stubs.fs.lstatSync.returns( {
isDirectory() {
return true;
}
} );

stubs.fs.lstatSync.withArgs( __dirname + '/packages/.DS_Store' ).returns( {
isDirectory() {
return false;
}
} );

syncCommand.afterExecute( processedPackages, null, mgitOptions );

expect( consoleLog.calledOnce ).to.equal( true );
expect( consoleLog.callCount ).to.equal( 3 );
expect( consoleLog.firstCall.args[ 0 ] ).to.match( /2 packages have been processed\./ );
expect( consoleLog.secondCall.args[ 0 ] ).to.match(
/Paths to directories listed below are skipped by mgit because they are not defined in "mgit\.json":/
);
expect( consoleLog.thirdCall.args[ 0 ] ).to.match( / {2}- .*\/packages\/package-3/ );

consoleLog.restore();
} );
Expand Down

0 comments on commit eab13b9

Please sign in to comment.