Skip to content

Commit

Permalink
fix(git-cz.js,staging.js): check for staged files before running prom…
Browse files Browse the repository at this point in the history
…pt (#818)

* fix(git-cz.js,staging.js): check for staged files before running prompt

Check for staged files before running the prompt. Running the `git-cz` command with no files staged
reports "No files added to staging". Preserve the functionality of `git -a` (--all) flag - `git-cz
-a` command with no files added to staging area, adds all files to staging and opens the prompt (no
error thrown).

"fix #785"

* test(tests/commit.js): throw error if staging is empty

Extend upon the existing commit unit tests. Assert the error is thrown when running commitizen
commit command with no files add to staging.

"re #585"

* test(tests/staging.js): preserve the functionality of the --all flag

Extend upon the existing staging unit tests. Assert that the files are added to staging area when
running commitizen commit command with -a (--all) flag, prior to spawning the prompt.

"re #785"
  • Loading branch information
ognjenjevremovic authored Nov 8, 2021
1 parent d76998e commit fdb73cd
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/cli/strategies/git-cz.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit fdb73cd

Please sign in to comment.