From 936e197d6331c92bc156af480885206604401c5b Mon Sep 17 00:00:00 2001 From: ChihYing Date: Wed, 1 Apr 2020 19:41:41 -0700 Subject: [PATCH] fix: askx upgrade-project -> remove skill.json and other v1 artifacts (#102) --- .../upgrade-project/hosted-skill-helper.js | 25 +++++++++++----- .../hosted-skill-helper-test.js | 30 +++++++++++++++---- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/commands/util/upgrade-project/hosted-skill-helper.js b/lib/commands/util/upgrade-project/hosted-skill-helper.js index 5c030822..c72a0185 100644 --- a/lib/commands/util/upgrade-project/hosted-skill-helper.js +++ b/lib/commands/util/upgrade-project/hosted-skill-helper.js @@ -4,7 +4,7 @@ const path = require('path'); const HostedSkillController = require('@src/controllers/hosted-skill-controller'); const SkillMetadataController = require('@src/controllers/skill-metadata-controller'); -const CLiError = require('@src/exceptions/cli-error'); +const CliError = require('@src/exceptions/cli-error'); const ResourcesConfig = require('@src/model/resources-config'); const AskResources = require('@src/model/resources-config/ask-resources'); const AskStates = require('@src/model/resources-config/ask-states'); @@ -27,13 +27,24 @@ function checkIfDevBranchClean(gitClient) { gitClient.checkoutBranch('dev'); const statusOutput = gitClient.shortStatus(); if (statusOutput.toString()) { - throw new CLiError(`Commit the following files in the dev branch before upgrading project:\n${statusOutput}`); + throw new CliError(`Commit the following files in the dev branch before upgrading project:\n${statusOutput}`); } - const countOutput = gitClient.countCommitDifference('origin/dev', 'dev'); - const counter = countOutput.toString().replace(/\D/g, ''); - if (stringUtils.isNonBlankString(counter) && counter !== '0') { - throw new CLiError('Upgrade project failed, as your dev branch is ahead of the remote.\nPlease follow the project upgrade instruction from ' - + 'https://github.com/alexa-labs/ask-cli/blob/develop/docs/Upgrade-Project-From-V1.md#upgrade-steps to change to ask-cli v2 structure.'); + try { + _compareCommitsWithDev(gitClient, 'origin/dev'); + _compareCommitsWithDev(gitClient, 'master'); + } catch (error) { + throw error.message; + } +} + +function _compareCommitsWithDev(gitClient, branch) { + const upgradeInstruction = 'Please follow the project upgrade instruction from ' + + 'https://github.com/alexa-labs/ask-cli/blob/develop/docs/Upgrade-Project-From-V1.md#upgrade-steps ' + + 'to clean your working branch before upgrading project.'; + let diffCount = gitClient.countCommitDifference(branch, 'dev'); + diffCount = diffCount.toString().replace(/\D/g, ''); + if (stringUtils.isNonBlankString(diffCount) && diffCount !== '0') { + throw new CliError(`Upgrade project failed. Your branch is ahead of ${branch} by ${diffCount} commit(s), ${upgradeInstruction}`); } } 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 83985700..4956210f 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 @@ -34,8 +34,10 @@ describe('Commands upgrade-project test - hosted skill helper test', () => { describe('# test helper method - checkIfDevBranchClean', () => { let gitClient; + let commitDiffStub; beforeEach(() => { gitClient = new GitClient(TEST_PROJECT_PATH, TEST_VERBOSITY_OPTIONS); + commitDiffStub = sinon.stub(gitClient, 'countCommitDifference'); }); afterEach(() => { @@ -52,24 +54,40 @@ describe('Commands upgrade-project test - hosted skill helper test', () => { .throw(CliError, `Commit the following files in the dev branch before upgrading project:\n${TEST_OUTPUT}`); }); - it('| dev branch commits difference is not zero , expect error thrown', () => { + it('| origin branch commits difference is not zero , expect error thrown', () => { // setup sinon.stub(gitClient, 'checkoutBranch'); sinon.stub(gitClient, 'shortStatus').returns(''); - sinon.stub(gitClient, 'countCommitDifference').returns('1'); + commitDiffStub.onCall(0).returns('1'); + commitDiffStub.onCall(1).returns('1'); // call & verify expect(() => hostedSkillHelper.checkIfDevBranchClean(gitClient)) - .throw(CliError, 'Upgrade project failed, as your dev branch is ahead of the remote.\n' + .throw('Upgrade project failed. Your branch is ahead of origin/dev by 1 commit(s), ' + 'Please follow the project upgrade instruction from ' + 'https://github.com/alexa-labs/ask-cli/blob/develop/docs/Upgrade-Project-From-V1.md#upgrade-steps ' - + 'to change to ask-cli v2 structure.'); + + 'to clean your working branch before upgrading project.'); }); - it('| dev branch commits difference is zero , expect noe error thrown', () => { + it('| master branch commits difference is not zero , expect error thrown', () => { // setup sinon.stub(gitClient, 'checkoutBranch'); sinon.stub(gitClient, 'shortStatus').returns(''); - sinon.stub(gitClient, 'countCommitDifference').returns('0'); + commitDiffStub.onCall(0).returns('0'); + commitDiffStub.onCall(1).returns('1'); + // call & verify + expect(() => hostedSkillHelper.checkIfDevBranchClean(gitClient)) + .throw('Upgrade project failed. Your branch is ahead of master by 1 commit(s), ' + + 'Please follow the project upgrade instruction from ' + + 'https://github.com/alexa-labs/ask-cli/blob/develop/docs/Upgrade-Project-From-V1.md#upgrade-steps ' + + 'to clean your working branch before upgrading project.'); + }); + + it('| origin and master branch commits difference is zero , expect noe error thrown', () => { + // setup + sinon.stub(gitClient, 'checkoutBranch'); + sinon.stub(gitClient, 'shortStatus').returns(''); + commitDiffStub.onCall(0).returns('0'); + commitDiffStub.onCall(1).returns('0'); // call hostedSkillHelper.checkIfDevBranchClean(gitClient); });