Skip to content

Commit

Permalink
fix: import URL object & handle cross-platform file separator (#65)
Browse files Browse the repository at this point in the history
* fix: import URL object & handle cross-platform file separator
  • Loading branch information
Chih-Ying authored Mar 25, 2020
1 parent b303741 commit 83ab50e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 32 deletions.
10 changes: 3 additions & 7 deletions lib/clients/git-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ const path = require('path');
const CLiError = require('@src/exceptions/cli-error');
const Messenger = require('@src/view/messenger');

const isWindows = process.platform === 'win32';
// eslint-disable-next-line quotes
const QUOTES = isWindows ? `"` : `'`;

/**
* Class for git commands management
*/
Expand All @@ -28,7 +24,7 @@ module.exports = class GitClient {
* Execute git-init to create an empty Git repository or reinitialize an existing one
*/
init() {
const commands = [`git init ${QUOTES}${this.projectPath}${QUOTES}${this.verbosityOptions.showOutput === false ? ' --quiet' : ''}`];
const commands = [`git init "${this.projectPath}"${this.verbosityOptions.showOutput === false ? ' --quiet' : ''}`];
const options = {
showStdOut: this.verbosityOptions.showOutput,
showStdErr: true,
Expand All @@ -43,7 +39,7 @@ module.exports = class GitClient {
*/
configureCredentialHelper(credentialHelperPath) {
const commands = [
`git config --local --replace-all credential.helper ${QUOTES}!${credentialHelperPath}${QUOTES}`,
`git config --local --replace-all credential.helper "!${credentialHelperPath}"`,
'git config --local credential.UseHttpPath true'];
const options = {
showStdOut: this.verbosityOptions.showOutput,
Expand Down Expand Up @@ -138,7 +134,7 @@ module.exports = class GitClient {
* @param {string} file the file to add content from
*/
add(file) {
const commands = [`git add ${QUOTES}${file}${QUOTES}`];
const commands = [`git add "${file}"`];
const options = {
showStdOut: this.verbosityOptions.showOutput,
showStdErr: true,
Expand Down
1 change: 1 addition & 0 deletions lib/commands/v2new/hosted-skill-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const opn = require('opn');
const path = require('path');
const portScanner = require('portscanner');
const { URL, URLSearchParams } = require('url');

const CONSTANTS = require('@src/utils/constants');
const LocalHostServer = require('@src/utils/local-host-server');
Expand Down
6 changes: 3 additions & 3 deletions test/unit/clients/git-client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Clients test - cli git client', () => {

it('| test git init succeed', () => {
// setup
const TEST_COMMAND = `git init '${TEST_PROJECT_PATH}'${TEST_VERBOSITY_OPTIONS_DEBUG.showOutput === false ? ' --quiet' : ''}`;
const TEST_COMMAND = `git init "${TEST_PROJECT_PATH}"${TEST_VERBOSITY_OPTIONS_DEBUG.showOutput === false ? ' --quiet' : ''}`;
const gitClient = new GitClient(TEST_PROJECT_PATH, TEST_VERBOSITY_OPTIONS_DEBUG);
sinon.stub(gitClient, '_execChildProcessSync');
// call
Expand All @@ -64,7 +64,7 @@ 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 --replace-all credential.helper "!${TEST_CREDENTIAL_HELPER_PATH}"`,
'git config --local credential.UseHttpPath true'];

afterEach(() => {
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('Clients test - cli git client', () => {

describe('# git add', () => {
const TEST_REPO_DIR = 'repoDir';
const TEST_COMMAND = [`git add '${TEST_REPO_DIR}'`];
const TEST_COMMAND = [`git add "${TEST_REPO_DIR}"`];

afterEach(() => {
sinon.restore();
Expand Down
22 changes: 11 additions & 11 deletions test/unit/commands/util/upgrade-project/helper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Commands upgrade-project test - helper test', () => {
const TEST_SKILL_ID = 'skillId';
const TEST_SKILL_STAGE = 'development';
const TEST_CODE_URI = 'codeUri';
const TEST_V2_CODE_URI = `${CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA}/${TEST_CODE_URI}`;
const TEST_V2_CODE_URI = path.join(CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA, TEST_CODE_URI);
const TEST_RUNTIME = 'runtime';
const TEST_HANDLER = 'handler';
const TEST_REVISION_ID = 'revisionId';
Expand All @@ -46,15 +46,15 @@ describe('Commands upgrade-project test - helper test', () => {
codeUri: TEST_CODE_URI,
runtime: TEST_RUNTIME,
handler: TEST_HANDLER,
v2CodeUri: `./${TEST_V2_CODE_URI}`,
v2CodeUri: `.${path.sep}${TEST_V2_CODE_URI}`,
revisionId: TEST_REVISION_ID
},
[TEST_REGION_NA]: {
arn: undefined,
codeUri: TEST_CODE_URI,
runtime: undefined,
handler: undefined,
v2CodeUri: `./${TEST_V2_CODE_URI}`,
v2CodeUri: `.${path.sep}${TEST_V2_CODE_URI}`,
revisionId: undefined
}
};
Expand Down Expand Up @@ -291,7 +291,7 @@ You have multiple Lambda codebases for region ${TEST_REGION}, we will use "${TES
helper.moveOldProjectToLegacyFolder(TEST_ROOT_PATH);
// verify
expect(moveStub.args[0][0]).eq(TEST_FILE);
expect(moveStub.args[0][1]).eq(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.LEGACY_PATH}/${TEST_FILE}`);
expect(moveStub.args[0][1]).eq(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.LEGACY_PATH, TEST_FILE));
});
});

Expand All @@ -307,9 +307,9 @@ You have multiple Lambda codebases for region ${TEST_REGION}, we will use "${TES
sinon.stub(R, 'clone').returns({ profiles: {} });
// call
helper.createV2ProjectSkeleton(TEST_ROOT_PATH, TEST_SKILL_ID, TEST_PROFILE);
expect(ensureDirStub.args[0][0]).eq(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.SKILL_PACKAGE.PACKAGE}`);
expect(ensureDirStub.args[1][0]).eq(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA}`);
expect(writeStub.args[0][0]).eq(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG}`);
expect(ensureDirStub.args[0][0]).eq(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.SKILL_PACKAGE.PACKAGE));
expect(ensureDirStub.args[1][0]).eq(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA));
expect(writeStub.args[0][0]).eq(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG));
expect(writeStub.args[0][1]).deep.equal({
profiles: {
[TEST_PROFILE]: {
Expand Down Expand Up @@ -401,8 +401,8 @@ You have multiple Lambda codebases for region ${TEST_REGION}, we will use "${TES
// verify
expect(ResourcesConfig.getInstance().getSkillInfraType(TEST_PROFILE)).deep.equal(CONSTANTS.DEPLOYER_TYPE.LAMBDA.NAME);
expect(ResourcesConfig.getInstance().getSkillInfraUserConfig(TEST_PROFILE)).deep.equal(TEST_CONFIG);
expect(copyStub.args[0][0]).equal(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.LEGACY_PATH}/${TEST_CODE_URI}`);
expect(copyStub.args[0][1]).equal(`${TEST_ROOT_PATH}/${TEST_V2_CODE_URI}`);
expect(copyStub.args[0][0]).equal(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.LEGACY_PATH, TEST_CODE_URI));
expect(copyStub.args[0][1]).equal(path.join(TEST_ROOT_PATH, TEST_V2_CODE_URI));
expect(ResourcesConfig.getInstance().getSkillInfraDeployState(TEST_PROFILE)[TEST_PROFILE].lambda.arn).deep.equal(TEST_ARN);
});

Expand Down Expand Up @@ -465,8 +465,8 @@ You have multiple Lambda codebases for region ${TEST_REGION}, we will use "${TES
helper.handleExistingLambdaCode(TEST_ROOT_PATH, TEST_RESOURCE_MAP_MULTIPLE_REGION, TEST_PROFILE);
// verify
expect(ResourcesConfig.getInstance().getSkillInfraType(TEST_PROFILE)).deep.equal(CONSTANTS.DEPLOYER_TYPE.LAMBDA.NAME);
expect(copyStub.args[0][0]).equal(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.LEGACY_PATH}/${TEST_CODE_URI}`);
expect(copyStub.args[0][1]).equal(`${TEST_ROOT_PATH}/${TEST_V2_CODE_URI}`);
expect(copyStub.args[0][0]).equal(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.LEGACY_PATH, TEST_CODE_URI));
expect(copyStub.args[0][1]).equal(path.join(TEST_ROOT_PATH, TEST_V2_CODE_URI));
expect(ResourcesConfig.getInstance().getSkillInfraDeployState(TEST_PROFILE)[TEST_PROFILE].lambda.arn).deep.equal(TEST_ARN);
expect(ResourcesConfig.getInstance().getSkillInfraUserConfig(TEST_PROFILE)).deep.equal(TEST_CONFIG);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const ResourcesConfig = require('@src/model/resources-config');
const CLiError = require('@src/exceptions/cli-error');
const CONSTANTS = require('@src/utils/constants');

//
describe('Commands upgrade-project test - hosted skill helper test', () => {
const TEST_ERROR = 'testError';
const TEST_PROFILE = 'default';
Expand Down Expand Up @@ -84,9 +85,9 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {
sinon.stub(R, 'clone').returns({ profiles: {} });
// call
hostedSkillHelper.createV2ProjectSkeleton(TEST_ROOT_PATH, TEST_SKILL_ID, TEST_PROFILE);
expect(ensureDirStub.args[0][0]).eq(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.SKILL_PACKAGE.PACKAGE}`);
expect(ensureDirStub.args[1][0]).eq(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA}`);
expect(writeStub.args[0][0]).eq(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG}`);
expect(ensureDirStub.args[0][0]).eq(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.SKILL_PACKAGE.PACKAGE));
expect(ensureDirStub.args[1][0]).eq(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA));
expect(writeStub.args[0][0]).eq(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.ASK_RESOURCES_JSON_CONFIG));
expect(writeStub.args[0][1]).deep.equal({
profiles: {
[TEST_PROFILE]: {
Expand Down Expand Up @@ -150,8 +151,8 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {
// verify
expect(ResourcesConfig.getInstance().getSkillInfraType(TEST_PROFILE)).equal(CONSTANTS.DEPLOYER_TYPE.HOSTED.NAME);
expect(ResourcesConfig.getInstance().getSkillInfraDeployState(TEST_PROFILE).repository.url).equal(TEST_REPO_URL);
expect(copyStub.args[0][0]).equal(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.LEGACY_PATH}/${CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA}`);
expect(copyStub.args[0][1]).equal(`${TEST_ROOT_PATH}/${CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA}`);
expect(copyStub.args[0][0]).equal(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.LEGACY_PATH, CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA));
expect(copyStub.args[0][1]).equal(path.join(TEST_ROOT_PATH, CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA));
});
});

Expand Down Expand Up @@ -195,7 +196,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {
sinon.stub(gitClient, 'configureCredentialHelper');
sinon.stub(gitClient, 'checkoutBranch');
sinon.stub(gitClient, 'merge');
sinon.stub(gitClient, 'deleteBranch')
sinon.stub(gitClient, 'deleteBranch');
sinon.stub(HostedSkillController.prototype, 'downloadGitHooksTemplate').callsArgWith(2, TEST_ERROR);
// call
hostedSkillHelper.postUpgradeGitSetup(TEST_PROFILE, TEST_DO_DEBUG, gitClient, (err) => {
Expand All @@ -210,7 +211,7 @@ describe('Commands upgrade-project test - hosted skill helper test', () => {
sinon.stub(gitClient, 'configureCredentialHelper');
sinon.stub(gitClient, 'checkoutBranch');
sinon.stub(gitClient, 'merge');
sinon.stub(gitClient, 'deleteBranch')
sinon.stub(gitClient, 'deleteBranch');
sinon.stub(HostedSkillController.prototype, 'downloadGitHooksTemplate').callsArgWith(2, null);
// call
hostedSkillHelper.postUpgradeGitSetup(TEST_PROFILE, TEST_DO_DEBUG, gitClient, (err) => {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/commands/util/upgrade-project/ui-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ describe('Commands upgrade-project test - UI test', () => {
codeUri: TEST_CODE_URI,
runtime: TEST_RUNTIME,
handler: TEST_HANDLER,
v2CodeUri: `./${TEST_V2_CODE_URI}`,
v2CodeUri: `.${path.sep}${TEST_V2_CODE_URI}`,
revisionId: TEST_REVISION_ID
},
[TEST_REGION_NA]: {
codeUri: TEST_CODE_URI,
runtime: TEST_RUNTIME,
handler: TEST_HANDLER,
v2CodeUri: `./${TEST_V2_CODE_URI}`,
v2CodeUri: `.${path.sep}${TEST_V2_CODE_URI}`,
revisionId: TEST_REVISION_ID
}
}
};
const TEST_PART_2 = `- Existing Lambda codebase will be moved into "${CONSTANTS.FILE_PATH.SKILL_CODE.LAMBDA}" folder`;
const TEST_PART_2_1 = `\n - Region ${TEST_REGION}: v1 "${TEST_CODE_URI}"\
-> v2 "./${TEST_V2_CODE_URI}" for existing Lambda ARN ${TEST_ARN}`;
-> v2 ".${path.sep}${TEST_V2_CODE_URI}" for existing Lambda ARN ${TEST_ARN}`;
const TEST_PART_2_2 = `\n - Region ${TEST_REGION_NA}: v1 "${TEST_CODE_URI}"\
-> v2 "./${TEST_V2_CODE_URI}" and will create new Lambda`;
-> v2 ".${path.sep}${TEST_V2_CODE_URI}" and will create new Lambda`;
// call
ui.displayPreview(TEST_USER_INPUT);
// verify
Expand Down
5 changes: 5 additions & 0 deletions test/unit/commands/v2new/hosted-skill-helper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const portScanner = require('portscanner');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const { URL, URLSearchParams } = require('url');

const hostedSkillHelper = require('@src/commands/v2new/hosted-skill-helper');
const HostedSkillController = require('@src/controllers/hosted-skill-controller/index');
Expand Down Expand Up @@ -35,6 +36,8 @@ describe('Commands new test - hosted skill helper test', () => {
let proxyHelper;
let endStub;
let response;
let loginUrl;
let urlSearchParams;

beforeEach(() => {
infoStub = sinon.stub();
Expand All @@ -50,6 +53,8 @@ describe('Commands new test - hosted skill helper test', () => {
on: sinon.stub().callsArgWith(1),
end: endStub
};
loginUrl = new URL(CONSTANTS.LWA.SIGNIN_URL);
urlSearchParams = new URLSearchParams();
});

afterEach(() => {
Expand Down

0 comments on commit 83ab50e

Please sign in to comment.