Skip to content

Commit

Permalink
fix(cli): Respect git commit -a original behavior (#376) (#471)
Browse files Browse the repository at this point in the history
* fix(cli): Respect git commit -a original behavior (#376)

* test(cli): Add test for -a option behavior (#376)
  • Loading branch information
pawamoy authored and jimthedev committed May 24, 2018
1 parent 5f8902e commit d10c94f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 10 deletions.
5 changes: 0 additions & 5 deletions src/cli/strategies/git-cz.js
Expand Up @@ -25,11 +25,6 @@ function gitCz (rawGitArgs, environment, adapterConfig) {
// commit strategy than git-cz. For example, in the case of --amend
let parsedCommitizenArgs = commitizenParser.parse(rawGitArgs);

if (parsedCommitizenArgs.a) {
// console.log('override -a in place');
addPath(sh, process.cwd());
}

if (parsedCommitizenArgs.amend) {
// console.log('override --amend in place');
gitStrategy.default(rawGitArgs, environment);
Expand Down
7 changes: 5 additions & 2 deletions src/git.js
@@ -1,11 +1,14 @@
import {addPath} from './git/add';
import {addPath, addFile} from './git/add';
import {commit} from './git/commit';
import {init} from './git/init';
import {log} from './git/log';
import {whatChanged} from './git/whatChanged';

export {
addPath,
addFile,
commit,
init,
log
log,
whatChanged
};
13 changes: 12 additions & 1 deletion src/git/add.js
@@ -1,4 +1,7 @@
export { addPath };
export {
addPath,
addFile
}

/**
* Synchronously adds a path to git staging
Expand All @@ -7,3 +10,11 @@ function addPath (sh, repoPath) {
sh.cd(repoPath);
sh.exec('git add .');
}

/**
* Synchronously adds a file to git staging
*/
function addFile (sh, repoPath, filename) {
sh.cd(repoPath);
sh.exec('git add ' + filename)
}
18 changes: 18 additions & 0 deletions src/git/whatChanged.js
@@ -0,0 +1,18 @@
import { exec } from 'child_process';

export { whatChanged };

/**
* Asynchronously gets the git whatchanged output
*/
function whatChanged (repoPath, done) {
exec('git whatchanged', {
maxBuffer: Infinity,
cwd: repoPath
}, function (error, stdout, stderr) {
if (error) {
throw error;
}
done(stdout);
});
}
66 changes: 64 additions & 2 deletions test/tests/commit.js
Expand Up @@ -12,7 +12,7 @@ import inquirer from 'inquirer';
import {bootstrap} from '../tester';

// Get our source files
import {addPath as gitAdd, commit as gitCommit, init as gitInit, log} from '../../src/git';
import {addPath as gitAdd, addFile as gitAddFile, commit as gitCommit, init as gitInit, log, whatChanged} from '../../src/git';
import {commit as commitizenCommit, init as commitizenInit, adapter} from '../../src/commitizen';

// Destructure some things for cleaner tests
Expand Down Expand Up @@ -232,6 +232,63 @@ describe('commit', function () {

});

it('should respect original behavior of -a option', function (done) {

this.timeout(config.maxTimeout); // this could take a while

// SETUP

let dummyCommitMessage = `sip sip sippin on some sizzurp`;

// Describe a repo and some files to add and commit
let repoConfig = {
path: config.paths.endUserRepo,
files: {
dummyfile: {
contents: `duck-duck-goose`,
filename: `mydummyfile.txt`,
},
dummyfilecopy: {
contents: `duck-duck-goose`,
filename: `mydummyfilecopy.txt`,
add: false,
},
gitignore: {
contents: `node_modules/`,
filename: `.gitignore`
}
}
};

// Describe an adapter
let adapterConfig = {
path: path.join(repoConfig.path, '/node_modules/cz-jira-smart-commit'),
npmName: 'cz-jira-smart-commit'
};

let options = {
args: ['-a']
};

// Quick setup the repos, adapter, and grab a simple prompter
let prompter = quickPrompterSetup(sh, repoConfig, adapterConfig, dummyCommitMessage, options);
// TEST

// Pass in inquirer but it never gets used since we've mocked out a different
// version of prompter.
commitizenCommit(sh, inquirer, repoConfig.path, prompter, {disableAppendPaths: true, quiet: true, emitData: true}, function () {
log(repoConfig.path, function (logOutput) {
expect(logOutput).to.have.string(dummyCommitMessage);
});
whatChanged(repoConfig.path, function (whatChangedOutput) {
expect(whatChangedOutput).to.have.string('A\t' + repoConfig.files.dummyfile.filename);
expect(whatChangedOutput).to.not.have.string('A\t' + repoConfig.files.dummyfilecopy.filename);
done();
});
});

});

});

afterEach(function () {
Expand Down Expand Up @@ -267,7 +324,12 @@ function quickPrompterSetup (sh, repoConfig, adapterConfig, commitMessage, optio

writeFilesToPath(repoConfig.files, repoConfig.path);

gitAdd(sh, repoConfig.path);
for (let key in repoConfig.files) {
let file = repoConfig.files[key];
if (file.add !== false) {
gitAddFile(sh, repoConfig.path, file.filename);
}
}

// NOTE: In the real world we would not be returning
// this we would instead be just making the commented
Expand Down

0 comments on commit d10c94f

Please sign in to comment.