Skip to content

Commit

Permalink
"transformCommitForSubPackage()" will clone the commit object as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Piechaczek committed Mar 5, 2019
1 parent 5f8d973 commit cdec9a5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ const getChangedFilesForCommit = require( './getchangedfilesforcommit' );
* Returns `undefined` if given commit should not be added to the changelog. This behavior can be changed
* using the `context.returnInvalidCommit` option.
*
* @param {Commit} commit
* @param {Commit} rawCommit
* @param {Object} context
* @param {Object} context.packageData Content from the 'package.json' for given package.
* @returns {Commit|undefined}
*/
module.exports = function transformCommitForSubPackage( commit, context ) {
module.exports = function transformCommitForSubPackage( rawCommit, context ) {
// Let's clone the commit. We don't want to modify the reference.
const commit = Object.assign( {}, rawCommit, {
notes: rawCommit.notes.map( note => Object.assign( {}, note ) )
} );

// Skip the Lerna "Publish" commit.
if ( !commit.type && commit.header === 'Publish' && commit.body ) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ describe( 'dev-env/release-tools/utils/transform-commit', () => {
}
}
} );

stubs.transformCommitForSubRepository.returnsArg( 0 );
} );

afterEach( () => {
Expand All @@ -59,7 +61,8 @@ describe( 'dev-env/release-tools/utils/transform-commit', () => {

it( 'rejects the commit if no files were changed', () => {
const commit = {
hash: 'abcd123'
hash: 'abcd123',
notes: []
};

stubs.getChangedFilesForCommit.returns( [] );
Expand All @@ -70,7 +73,8 @@ describe( 'dev-env/release-tools/utils/transform-commit', () => {

it( 'rejects the commit when it changed files in other package', () => {
const commit = {
hash: 'abcd123'
hash: 'abcd123',
notes: []
};

stubs.getChangedFilesForCommit.returns( [
Expand Down Expand Up @@ -104,56 +108,99 @@ describe( 'dev-env/release-tools/utils/transform-commit', () => {
expect( stubs.transformCommitForSubRepository.called ).to.equal( false );
} );

it( 'returns a new instance of object instead od modifying passed one', () => {
const notes = [
{ title: 'Foo', text: 'Foo-Text' },
{ title: 'Bar', text: 'Bar-Text' }
];

const rawCommit = {
hash: '684997d0eb2eca76b9e058fb1c3fa00b50059cdc',
header: 'Fix: Simple fix.',
type: 'Fix',
subject: 'Simple fix.',
body: null,
footer: null,
notes
};

stubs.getChangedFilesForCommit.returns( [
'packages/ckeditor5-dev-env/README.md',
'packages/ckeditor5-dev-env/package.json',
] );

stubs.transformCommitForSubRepository.reset();
stubs.transformCommitForSubRepository.callsFake( commit => {
commit.hash = commit.hash.substring( 0, 7 );
return commit;
} );

const commit = transformCommitForSubPackage( rawCommit, context );

expect( stubs.transformCommitForSubRepository.calledOnce ).to.equal( true );

// `transformCommitForSubRepository` modifies `hash` of given commit and returns the same object.
// `transformCommitForSubPackage` must care about cloning the object before any operation.
expect( commit.hash ).to.not.equal( rawCommit.hash );

// Notes cannot be the same but they should be equal.
expect( commit.notes ).to.not.equal( rawCommit.notes );
expect( commit.notes ).to.deep.equal( rawCommit.notes );
} );

it( 'accepts the commit when it has changed files in proper and other packages', () => {
const commit = {
hash: 'abcd123'
const rawCommit = {
hash: 'abcd123',
notes: []
};

stubs.getChangedFilesForCommit.returns( [
'packages/ckeditor5-dev-env/README.md',
'packages/ckeditor5-dev-tests/README.md'
] );

stubs.transformCommitForSubRepository.returnsArg( 0 );
const commit = transformCommitForSubPackage( rawCommit, context );

expect( transformCommitForSubPackage( commit, context ) ).to.equal( commit );
expect( stubs.transformCommitForSubRepository.called ).to.equal( true );
expect( commit ).to.deep.equal( rawCommit );
} );

it( 'accepts the commit when it has changed files in proper packages and root of the repository', () => {
const commit = {
hash: 'abcd123'
const rawCommit = {
hash: 'abcd123',
notes: []
};

stubs.getChangedFilesForCommit.returns( [
'packages/ckeditor5-dev-env/README.md',
'README.md'
'README.md',
'packages/ckeditor5-dev-env/README.md'
] );

stubs.transformCommitForSubRepository.returnsArg( 0 );
const commit = transformCommitForSubPackage( rawCommit, context );

expect( transformCommitForSubPackage( commit, context ) ).to.equal( commit );
expect( stubs.transformCommitForSubRepository.called ).to.equal( true );
expect( commit ).to.deep.equal( rawCommit );
} );

it( 'accepts the commit when it has changed files for proper package', () => {
const commit = {
hash: 'abcd123'
const rawCommit = {
hash: 'abcd123',
notes: []
};

stubs.getChangedFilesForCommit.returns( [
'packages/ckeditor5-dev-env/README.md',
'packages/ckeditor5-dev-env/package.json',
] );

stubs.transformCommitForSubRepository.returnsArg( 0 );
const commit = transformCommitForSubPackage( rawCommit, context );

expect( transformCommitForSubPackage( commit, context ) ).to.equal( commit );
expect( stubs.transformCommitForSubRepository.calledOnce ).to.equal( true );
expect( stubs.transformCommitForSubRepository.called ).to.equal( true );
expect( commit ).to.deep.equal( rawCommit );
} );

it( 'does not crash if the merge commit does not contain the second line', () => {
const commit = {
const rawCommit = {
type: null,
subject: null,
merge: 'Merge branch \'master\' of github.com:ckeditor/ckeditor5-dev',
Expand All @@ -169,13 +216,8 @@ describe( 'dev-env/release-tools/utils/transform-commit', () => {
stubs.getChangedFilesForCommit.returns( [] );

expect( () => {
transformCommitForSubPackage( commit, context );
transformCommitForSubPackage( rawCommit, context );
} ).to.not.throw( Error );

expect( stubs.getChangedFilesForCommit.firstCall.args[ 0 ] ).to.equal( '575e00bc8ece48826adefe226c4fb1fe071c73a7' );
expect( commit.hash ).to.equal( '575e00bc8ece48826adefe226c4fb1fe071c73a7' );
expect( commit.header ).to.equal( 'Merge branch \'master\' of github.com:ckeditor/ckeditor5-dev' );
expect( commit.body ).to.equal( null );
} );
} );
} );

0 comments on commit cdec9a5

Please sign in to comment.