Skip to content
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

[Fix] Check for staged files prior to spawning the prompt #818

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cli/strategies/git-cz.js
Expand Up @@ -41,6 +41,7 @@ function gitCz (rawGitArgs, environment, adapterConfig) {
let resolvedAdapterConfigPath = resolveAdapterPath(adapterConfig.path);
let resolvedAdapterRootPath = findRoot(resolvedAdapterConfigPath);
let prompter = getPrompter(adapterConfig.path);
let shouldStageAllFiles = rawGitArgs.includes('-a') || rawGitArgs.includes('--all');

isClean(process.cwd(), function (error, stagingIsClean) {
if (error) {
Expand All @@ -67,6 +68,6 @@ function gitCz (rawGitArgs, environment, adapterConfig) {
throw error;
}
});
});
}, shouldStageAllFiles);

}
4 changes: 2 additions & 2 deletions src/commitizen/staging.js
Expand Up @@ -5,8 +5,8 @@ export { isClean };
/**
* Asynchrounously determines if the staging area is clean
*/
function isClean (repoPath, done) {
exec('git diff --no-ext-diff --name-only && git diff --no-ext-diff --cached --name-only', {
function isClean (repoPath, done, stageAllFiles) {
exec(`git diff --cached --no-ext-diff --name-only ${!!stageAllFiles ? '&& git diff --no-ext-diff --name-only' : ''}`, {
maxBuffer: Infinity,
cwd: repoPath
}, function (error, stdout) {
Expand Down
67 changes: 67 additions & 0 deletions test/tests/commit.js
Expand Up @@ -311,6 +311,73 @@ ${(os.platform === 'win32') ? '' : ' '}
done();
});
});

it('should throw error if staging area is empty', function (done) {

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

// SETUP

let dummyCommitMessage = `one does not simply ignore the tests`;

// Describe a repo and some files to add and commit
let repoConfig = {
path: config.paths.endUserRepo,
files: {
dummyfile: {
contents: `duck-duck-gray-duck`,
filename: `mydummiestfile.txt`,
},
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'
};

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

// Make an initial commit
commitizenCommit(inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function (error) {
// Should pass, as the files are added to the staging area
expect(error).to.be.null;

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);

// Make changes and don't add them to the staging area
writeFilesToPath({
dummymodified: {
contents: repoConfig.files.dummyfile.contents + '-modified',
filename: repoConfig.files.dummyfile.filename,
add: false,
}
}, repoConfig.path);
// Define a dummy prompter
let prompter = function (cz, commit) {
commit(`${dummyCommitMessage} #2`, {});
};

commitizenCommit(inquirer, repoConfig.path, prompter, { disableAppendPaths: true, quiet: true, emitData: true }, function (error) {
// Should fail, as staging are is empty
expect(error).to.be.instanceOf(Error);
done();
});
});
});

});
});

afterEach(function () {
Expand Down
37 changes: 37 additions & 0 deletions test/tests/staging.js
Expand Up @@ -78,6 +78,43 @@ describe('staging', function () {
});
});

it('should determine if --all flag adds files to staging area', function (done) {

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

// SETUP

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

gitInit(repoConfig.path);

staging.isClean(repoConfig.path, function (stagingIsCleanError, stagingIsClean) {
expect(stagingIsCleanError).to.be.null;
expect(stagingIsClean).to.be.true;

writeFilesToPath(repoConfig.files, repoConfig.path);

staging.isClean(repoConfig.path, function (afterWriteStagingIsCleanError, afterWriteStagingIsClean) {
expect(afterWriteStagingIsCleanError).to.be.null;
expect(afterWriteStagingIsClean).to.be.true;

done();
});
}, true);
});
});

afterEach(function () {
Expand Down