From 2da43269d78ec904476207cc486d0130cffb32f3 Mon Sep 17 00:00:00 2001 From: ChihYing Date: Wed, 1 Apr 2020 14:35:23 -0700 Subject: [PATCH] fix: ashx new AHS -> fails with credential problem (#98) --- lib/clients/git-client.js | 18 +++++++++ .../upgrade-project/hosted-skill-helper.js | 2 +- test/unit/clients/git-client-test.js | 37 ++++++++++++++++++- .../hosted-skill-helper-test.js | 10 ++--- 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/lib/clients/git-client.js b/lib/clients/git-client.js index 546ffbcc..19e1bcf3 100644 --- a/lib/clients/git-client.js +++ b/lib/clients/git-client.js @@ -38,6 +38,24 @@ module.exports = class GitClient { * @param {string} credentialHelperPath the path of git credential helper */ configureCredentialHelper(credentialHelperPath) { + const commands = [ + 'git config --local credential.helper ""', + `git config --local --add credential.helper "!${credentialHelperPath}"`, + 'git config --local credential.UseHttpPath true']; + const options = { + showStdOut: this.verbosityOptions.showOutput, + showStdErr: true, + showCmd: this.verbosityOptions.showCommand, + workingDir: this.projectPath + }; + this._execCommands(commands, options); + } + + /** + * Replace local git credential helper with credentialHelperPath + * @param {string} credentialHelperPath the path of git credential helper + */ + updateCredentialHelper(credentialHelperPath) { const commands = [ `git config --local --replace-all credential.helper "!${credentialHelperPath}"`, 'git config --local credential.UseHttpPath true']; diff --git a/lib/commands/util/upgrade-project/hosted-skill-helper.js b/lib/commands/util/upgrade-project/hosted-skill-helper.js index fd9d6707..5c030822 100644 --- a/lib/commands/util/upgrade-project/hosted-skill-helper.js +++ b/lib/commands/util/upgrade-project/hosted-skill-helper.js @@ -128,7 +128,7 @@ function postUpgradeGitSetup(profile, doDebug, gitClient, callback) { function _setGitCredentialHelper(profile, gitClient) { const credentialHelperPath = `askx util git-credentials-helper --profile ${profile}`; - gitClient.configureCredentialHelper(credentialHelperPath); + gitClient.updateCredentialHelper(credentialHelperPath); } function _updateGitIgnoreWithAskResourcesJson(gitClient) { diff --git a/test/unit/clients/git-client-test.js b/test/unit/clients/git-client-test.js index 56265408..87d798c6 100644 --- a/test/unit/clients/git-client-test.js +++ b/test/unit/clients/git-client-test.js @@ -64,7 +64,8 @@ describe('Clients test - cli git client', () => { describe('# test configureCredentialHelper', () => { const TEST_CREDENTIAL_HELPER_PATH = 'TEST_CREDENTIAL_HELPER_PATH'; const TEST_COMMAND = [ - `git config --local --replace-all credential.helper "!${TEST_CREDENTIAL_HELPER_PATH}"`, + 'git config --local credential.helper ""', + `git config --local --add credential.helper "!${TEST_CREDENTIAL_HELPER_PATH}"`, 'git config --local credential.UseHttpPath true']; afterEach(() => { @@ -78,9 +79,10 @@ describe('Clients test - cli git client', () => { // call gitClient.configureCredentialHelper(TEST_CREDENTIAL_HELPER_PATH); // verify - expect(gitClient._execChildProcessSync.callCount).eq(2); + expect(gitClient._execChildProcessSync.callCount).eq(3); expect(gitClient._execChildProcessSync.args[0][0]).eq(TEST_COMMAND[0]); expect(gitClient._execChildProcessSync.args[1][0]).eq(TEST_COMMAND[1]); + expect(gitClient._execChildProcessSync.args[2][0]).eq(TEST_COMMAND[2]); }); it('| test git configureCredentialHelper fails', () => { @@ -92,6 +94,37 @@ describe('Clients test - cli git client', () => { }); }); + describe('# test updateCredentialHelper', () => { + const TEST_CREDENTIAL_HELPER_PATH = 'TEST_CREDENTIAL_HELPER_PATH'; + const TEST_COMMAND = [ + `git config --local --replace-all credential.helper "!${TEST_CREDENTIAL_HELPER_PATH}"`, + 'git config --local credential.UseHttpPath true']; + + afterEach(() => { + sinon.restore(); + }); + + it('| test git updateCredentialHelper execute commands correctly ', () => { + // setup + const gitClient = new GitClient(TEST_PROJECT_PATH, TEST_VERBOSITY_OPTIONS); + sinon.stub(gitClient, '_execChildProcessSync'); + // call + gitClient.updateCredentialHelper(TEST_CREDENTIAL_HELPER_PATH); + // verify + expect(gitClient._execChildProcessSync.callCount).eq(2); + expect(gitClient._execChildProcessSync.args[0][0]).eq(TEST_COMMAND[0]); + expect(gitClient._execChildProcessSync.args[1][0]).eq(TEST_COMMAND[1]); + }); + + it('| test git updateCredentialHelper fails', () => { + // setup + const gitClient = new GitClient(TEST_PROJECT_PATH, TEST_VERBOSITY_OPTIONS_DEBUG); + sinon.stub(gitClient, '_execChildProcessSync').throws(new CLiError(`${TEST_ERROR}`)); + // call & verify + expect(() => gitClient.updateCredentialHelper(TEST_CREDENTIAL_HELPER_PATH)).throw(CLiError, `CliError: ${TEST_ERROR}`); + }); + }); + describe('# test addOrigin', () => { const TEST_REPO_URL = 'TEST_REPO_URL'; const TEST_COMMAND = [`git remote add origin ${TEST_REPO_URL}`]; diff --git a/test/unit/commands/util/upgrade-project/hosted-skill-helper-test.js b/test/unit/commands/util/upgrade-project/hosted-skill-helper-test.js index 83173b1c..83985700 100644 --- a/test/unit/commands/util/upgrade-project/hosted-skill-helper-test.js +++ b/test/unit/commands/util/upgrade-project/hosted-skill-helper-test.js @@ -183,7 +183,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => { it('| set Git Credential Helper fails, expect error thrown', (done) => { // setup - sinon.stub(gitClient, 'configureCredentialHelper').throws(new CliError(TEST_ERROR)); + sinon.stub(gitClient, 'updateCredentialHelper').throws(new CliError(TEST_ERROR)); // call hostedSkillHelper.postUpgradeGitSetup(TEST_PROFILE, TEST_DO_DEBUG, gitClient, (err) => { // verify @@ -194,7 +194,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => { it('| set Master As Default fails, expect error thrown', (done) => { // setup - sinon.stub(gitClient, 'configureCredentialHelper'); + sinon.stub(gitClient, 'updateCredentialHelper'); sinon.stub(gitClient, 'checkoutBranch'); sinon.stub(gitClient, 'merge'); sinon.stub(gitClient, 'deleteBranch').throws(new CliError(TEST_ERROR)); @@ -208,7 +208,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => { it('| update git ignore file, expect error thrown', (done) => { // setup - sinon.stub(gitClient, 'configureCredentialHelper'); + sinon.stub(gitClient, 'updateCredentialHelper'); sinon.stub(gitClient, 'checkoutBranch'); sinon.stub(gitClient, 'merge'); sinon.stub(gitClient, 'deleteBranch'); @@ -224,7 +224,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => { it('| set Pre PushHook Template fails, expect error thrown', (done) => { // setup - sinon.stub(gitClient, 'configureCredentialHelper'); + sinon.stub(gitClient, 'updateCredentialHelper'); sinon.stub(gitClient, 'checkoutBranch'); sinon.stub(gitClient, 'merge'); sinon.stub(gitClient, 'deleteBranch'); @@ -240,7 +240,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => { it('| post Upgrade Git Setup succeeds, expect no error thrown', (done) => { // setup - sinon.stub(gitClient, 'configureCredentialHelper'); + sinon.stub(gitClient, 'updateCredentialHelper'); sinon.stub(gitClient, 'checkoutBranch'); sinon.stub(gitClient, 'merge'); sinon.stub(gitClient, 'deleteBranch');