Skip to content

Commit

Permalink
Merge pull request #56 from cksource/t/55
Browse files Browse the repository at this point in the history
Other: Improved UI of the statuses table. Closes #55.

* If current branch is other than specified in `mgit.json` – the branch will be prefixed with `!`,
* If current branch is other than `master` – the whole row will be highlighted (in pink).
  • Loading branch information
Reinmar authored Aug 17, 2017
2 parents ad804d3 + 62b7ee0 commit 35349d0
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 16 deletions.
11 changes: 8 additions & 3 deletions lib/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const gitStatusParser = require( '../utils/gitstatusparser' );

module.exports = {
beforeExecute() {
console.log( chalk.blue( 'Collecting statuses from all found packages...' ) );
console.log( chalk.blue( 'Collecting statuses...' ) );
},

/**
Expand Down Expand Up @@ -86,9 +86,13 @@ module.exports = {
const wholeRow = [];
const statusColumn = [];

const wholeRowColor = mgitBranch !== status.branch ? 'magenta' : null;
const wholeRowColor = status.branch !== 'master' ? 'magenta' : null;
let branch = status.branch;

if ( mgitBranch !== status.branch ) {
branch = `${ color( 'cyan', '!' ) } ${ branch }`;
}

if ( status.ahead ) {
branch += color( 'yellow', ` ↑${ status.ahead }` );
}
Expand Down Expand Up @@ -122,7 +126,8 @@ module.exports = {
`${ color( 'yellow', '↑' ) } branch is ahead ${ color( 'yellow', '↓' ) } or behind`,
`${ color( 'green', '+' ) } staged files`,
`${ color( 'red', 'M' ) } modified files`,
`${ color( 'blue', '?' ) } untracked files`
`${ color( 'blue', '?' ) } untracked files`,
`\n${ color( 'cyan', '!' ) } not on branch specified in "mgit.json"`
];

return `${ chalk.bold( 'Legend:' ) }\n${ legend.join( ', ' ) }.`;
Expand Down
127 changes: 114 additions & 13 deletions tests/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ describe( 'commands/status', () => {
constructor: sandbox.stub(),
push: sandbox.stub(),
toString: sandbox.stub()
},
chalk: {
cyan: sandbox.stub(),
bold: sandbox.stub(),
yellow: sandbox.stub(),
green: sandbox.stub(),
red: sandbox.stub(),
blue: sandbox.stub(),
magenta: sandbox.stub(),
}
};

Expand All @@ -48,13 +57,13 @@ describe( 'commands/status', () => {

// Do not modify the color.
mockery.registerMock( 'chalk', {
cyan: text => text,
bold: text => text,
yellow: text => text,
green: text => text,
red: text => text,
blue: text => text,
magenta: text => text
cyan: stubs.chalk.cyan.callsFake( chalkCallsFake() ),
bold: stubs.chalk.bold.callsFake( chalkCallsFake() ),
yellow: stubs.chalk.yellow.callsFake( chalkCallsFake() ),
green: stubs.chalk.green.callsFake( chalkCallsFake() ),
red: stubs.chalk.red.callsFake( chalkCallsFake() ),
blue: stubs.chalk.blue.callsFake( chalkCallsFake() ),
magenta: stubs.chalk.magenta.callsFake( chalkCallsFake() )
} );
mockery.registerMock( 'cli-table', class Table {
constructor( ...args ) {
Expand All @@ -73,6 +82,10 @@ describe( 'commands/status', () => {
mockery.registerMock( '../utils/gitstatusparser', stubs.gitStatusParser );

statusCommand = require( '../../lib/commands/status' );

function chalkCallsFake() {
return ( ...args ) => args.join( ',' );
}
} );

afterEach( () => {
Expand Down Expand Up @@ -184,7 +197,7 @@ describe( 'commands/status', () => {
} );

describe( 'afterExecute()', () => {
it( 'do not display any thing if processed packages list is empty', () => {
it( 'do not display anything if processed packages list is empty', () => {
const logStub = sandbox.stub( console, 'log' );

const processedPackages = new Set();
Expand All @@ -198,7 +211,7 @@ describe( 'commands/status', () => {
logStub.restore();
} );

it( 'do not display any thing if command responses list is empty', () => {
it( 'do not display anything if command responses list is empty', () => {
const logStub = sandbox.stub( console, 'log' );

const processedPackages = new Set();
Expand Down Expand Up @@ -245,7 +258,7 @@ describe( 'commands/status', () => {
modified: [],
untracked: [ 'CHANGELOG.md' ],
},
mgitBranch: 'master',
mgitBranch: 't/1',
commit: 'ef45678'
} );

Expand All @@ -270,11 +283,99 @@ describe( 'commands/status', () => {

expect( logStub.calledTwice ).to.equal( true );
expect( logStub.firstCall.args[ 0 ] ).to.equal( '┻━┻' );
expect( logStub.secondCall.args[ 0 ] ).to.equal(
'Legend:\n' +
'↑ branch is ahead ↓ or behind, + staged files, M modified files, ? untracked files.'
expect( logStub.secondCall.args[ 0 ] ).to.match( /^Legend:/ );
expect( stubs.chalk.cyan.calledOnce ).to.equal( true );

logStub.restore();
} );

it( 'highlights a row if current branch is other than master', () => {
const logStub = sandbox.stub( console, 'log' );

const processedPackages = new Set();
const commandResponses = new Set();

processedPackages.add( '@ckeditor/ckeditor5-foo' );

commandResponses.add( {
packageName: 'foo',
status: {
branch: 't/ckeditor5-bar/1',
ahead: 0,
behind: 0,
staged: [],
modified: [],
untracked: [],
},
mgitBranch: 'master',
commit: 'abcd123'
} );

statusCommand.afterExecute( processedPackages, commandResponses );
expect( stubs.chalk.magenta.called ).to.equal( true );

logStub.restore();
} );

it( 'does not highlight a row if current branch is equal to master', () => {
const logStub = sandbox.stub( console, 'log' );

const processedPackages = new Set();
const commandResponses = new Set();

processedPackages.add( '@ckeditor/ckeditor5-foo' );

commandResponses.add( {
packageName: 'foo',
status: {
branch: 'master',
ahead: 0,
behind: 0,
staged: [],
modified: [],
untracked: [],
},
mgitBranch: 'master',
commit: 'abcd123'
} );

statusCommand.afterExecute( processedPackages, commandResponses );
expect( stubs.chalk.magenta.called ).to.equal( false );

logStub.restore();
} );

it( 'adds "!" before the branch name if current branch is other than defined in "mgit.json"', () => {
const logStub = sandbox.stub( console, 'log' );

const processedPackages = new Set();
const commandResponses = new Set();

processedPackages.add( '@ckeditor/ckeditor5-bar' );

commandResponses.add( {
packageName: 'bar',
status: {
branch: 't/1',
ahead: 0,
behind: 0,
staged: [],
modified: [],
untracked: [],
},
mgitBranch: 'master',
commit: 'ef45678'
} );

statusCommand.afterExecute( processedPackages, commandResponses );

expect( stubs.table.push.firstCall.args[ 0 ] ).to.deep.equal(
[ 'bar', '! t/1', 'ef45678', '' ]
);

// First - in the table, second - in the legend.
expect( stubs.chalk.cyan.calledTwice ).to.equal( true );

logStub.restore();
} );
} );
Expand Down

0 comments on commit 35349d0

Please sign in to comment.