From fdb73cda8b32bdf88ba69b9b0425d6de8852a494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ognjen=20Jevremovi=C4=87?= Date: Mon, 8 Nov 2021 12:05:55 +0100 Subject: [PATCH] fix(git-cz.js,staging.js): check for staged files before running prompt (#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" --- src/cli/strategies/git-cz.js | 3 +- src/commitizen/staging.js | 4 +-- test/tests/commit.js | 67 ++++++++++++++++++++++++++++++++++++ test/tests/staging.js | 37 ++++++++++++++++++++ 4 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/cli/strategies/git-cz.js b/src/cli/strategies/git-cz.js index dad0ebf1..059c5f33 100644 --- a/src/cli/strategies/git-cz.js +++ b/src/cli/strategies/git-cz.js @@ -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) { @@ -67,6 +68,6 @@ function gitCz (rawGitArgs, environment, adapterConfig) { throw error; } }); - }); + }, shouldStageAllFiles); } diff --git a/src/commitizen/staging.js b/src/commitizen/staging.js index c485b5dd..c8a81c29 100644 --- a/src/commitizen/staging.js +++ b/src/commitizen/staging.js @@ -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) { diff --git a/test/tests/commit.js b/test/tests/commit.js index f9c7fa86..8048d657 100644 --- a/test/tests/commit.js +++ b/test/tests/commit.js @@ -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 () { diff --git a/test/tests/staging.js b/test/tests/staging.js index 02e8c4fd..2ff77256 100644 --- a/test/tests/staging.js +++ b/test/tests/staging.js @@ -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 () {