Skip to content

Commit

Permalink
removed --force option in init in favor of specifying config using -c…
Browse files Browse the repository at this point in the history
… and moved test command
  • Loading branch information
miketalley committed Apr 8, 2021
1 parent 797c443 commit 348fdfe
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 41 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,5 @@ jobs:
run: |
yarn test
- name: Test-CLI
working-directory: ./acceptance-tests
run: |
yarn test
yarn test-cli
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ context
coverage
hubspot.config.yml
hubspot.config.yaml
acceptance-test.config.yml
lerna-debug.log
npm-debug.log
npm-debug.log.*
Expand Down
4 changes: 2 additions & 2 deletions acceptance-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Note that if you are testing against a QA portal, not a PROD one, you'll need to

There are four ways to pass in necessary configuration to the script.

1. Creating a .env file within the `/acceptance-tests` folder.
1. Creating a .env file in the root of the `hubspot-cli` folder.

```bash
PORTAL_ID="9289088"
Expand Down Expand Up @@ -45,7 +45,7 @@ The priority is Local Test Overrides > CLI Arg Overrides > Environment Variables

### Running Locally

1. In `/acceptance-tests`, `yarn test`
1. In `/hubspot-cli`, `yarn test-cli`

## Why Jasmine

Expand Down
5 changes: 5 additions & 0 deletions acceptance-tests/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const CONFIG_FILE_PATH = 'acceptance-test.config.yml';

module.exports = {
CONFIG_FILE_PATH,
};
2 changes: 1 addition & 1 deletion acceptance-tests/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec_dir": "spec",
"spec_dir": "acceptance-tests/spec",
"spec_files": ["tests/*[sS]pec.js"],
"helpers": ["helpers/**/*.js"],
"stopSpecOnExpectationFailure": false,
Expand Down
7 changes: 4 additions & 3 deletions acceptance-tests/run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const { setArgsOverrides, getTestConfig } = require('./env');
const { CONFIG_FILE_PATH } = require('./constants');
const path = require('path');
const Jasmine = require('jasmine');
const rimraf = require('rimraf');
Expand Down Expand Up @@ -63,15 +64,15 @@ setArgsOverrides(
testRunner.loadConfigFile(path.join(__dirname, 'jasmine.json'));
testRunner.addReporter(reporter);
testRunner.onComplete(function() {
rimraf.sync('hubspot.config.yml');
rimraf.sync(CONFIG_FILE_PATH);
});
const cliPath = path.join(process.cwd(), '../packages/cli/bin/hs');
const cliPath = path.join(process.cwd(), './packages/cli/bin/hs');
const cli = cmd.create(cliPath, '.');
global.cli = cli;
global.config = getTestConfig();

await cli.execute(
['init', '--force'],
['init', `--c="${CONFIG_FILE_PATH}"`],
[cmd.ENTER, config.personalAccessKey, cmd.ENTER]
);

Expand Down
21 changes: 11 additions & 10 deletions acceptance-tests/spec/tests/init-oauth2.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const http = require('http');
const cmd = require('../helpers/cmd');
const { CONFIG_FILE_PATH } = require('../../constants');
const rimraf = require('rimraf');
const { existsSync, readFileSync } = require('fs');
const yaml = require('js-yaml');
Expand All @@ -8,16 +9,16 @@ describe('hs init using oauth2', () => {
const { cli, config } = global;

beforeAll(() => {
rimraf.sync('hubspot.config.yml');
rimraf.sync(CONFIG_FILE_PATH);
});

it('should begin with no config file present', async () => {
expect(existsSync('hubspot.config.yml')).toBe(false);
expect(existsSync(CONFIG_FILE_PATH)).toBe(false);
});

it('should create a new config file', async () => {
await cli.execute(
['init', '--auth=oauth2', '--force'],
['init', '--auth=oauth2', `--c="${CONFIG_FILE_PATH}"`],
[
cmd.ENTER,
'Oauth2',
Expand Down Expand Up @@ -49,43 +50,43 @@ describe('hs init using oauth2', () => {
]
);

expect(existsSync('hubspot.config.yml')).toBe(true);
expect(existsSync(CONFIG_FILE_PATH)).toBe(true);
}, 20000);

it('should populate the config file with the correct name', async () => {
const portalConfig = yaml.load(readFileSync('hubspot.config.yml', 'utf8'))
const portalConfig = yaml.load(readFileSync(CONFIG_FILE_PATH, 'utf8'))
.portals[0];
expect(portalConfig.name).toEqual('Oauth2');
});

it('should populate the config file with the correct authType', async () => {
const portalConfig = yaml.load(readFileSync('hubspot.config.yml', 'utf8'))
const portalConfig = yaml.load(readFileSync(CONFIG_FILE_PATH, 'utf8'))
.portals[0];
expect(portalConfig.authType).toEqual('oauth2');
});

it('should populate the config file with the correct clientId', async () => {
const portalConfig = yaml.load(readFileSync('hubspot.config.yml', 'utf8'))
const portalConfig = yaml.load(readFileSync(CONFIG_FILE_PATH, 'utf8'))
.portals[0];
expect(portalConfig.auth.clientId).toEqual(config.clientId);
});

it('should populate the config file with the correct clientSecret', async () => {
const portalConfig = yaml.load(readFileSync('hubspot.config.yml', 'utf8'))
const portalConfig = yaml.load(readFileSync(CONFIG_FILE_PATH, 'utf8'))
.portals[0];
expect(portalConfig.auth.clientSecret).toEqual(config.clientSecret);
});

it('should populate the config file with the correct refreshToken', async () => {
const portalConfig = yaml.load(readFileSync('hubspot.config.yml', 'utf8'))
const portalConfig = yaml.load(readFileSync(CONFIG_FILE_PATH, 'utf8'))
.portals[0];
expect(portalConfig.auth.tokenInfo.refreshToken).toEqual(
config.refreshToken
);
});

it('should populate the config file with the correct defaultPortal', async () => {
const config = yaml.load(readFileSync('hubspot.config.yml', 'utf8'));
const config = yaml.load(readFileSync(CONFIG_FILE_PATH, 'utf8'));
expect(config.defaultPortal).toEqual('Oauth2');
});
});
13 changes: 7 additions & 6 deletions acceptance-tests/spec/tests/init-personal-access-key.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const cmd = require('../helpers/cmd');
const { CONFIG_FILE_PATH } = require('../../constants');
const rimraf = require('rimraf');
const { existsSync, readFileSync } = require('fs');
const yaml = require('js-yaml');
Expand All @@ -7,31 +8,31 @@ describe('hs init using personalAccessKey', () => {
const { cli, config } = global;

beforeAll(() => {
rimraf.sync('hubspot.config.yml');
rimraf.sync(CONFIG_FILE_PATH);
});

it('should begin with no config file present', async () => {
expect(existsSync('hubspot.config.yml')).toBe(false);
expect(existsSync(CONFIG_FILE_PATH)).toBe(false);
});

it('should create a new config file', async () => {
await cli.execute(
['init', '--force'],
['init', `--c="${CONFIG_FILE_PATH}"`],
[cmd.ENTER, config.personalAccessKey, cmd.ENTER, 'QA', cmd.ENTER]
);

expect(existsSync('hubspot.config.yml')).toBe(true);
expect(existsSync(CONFIG_FILE_PATH)).toBe(true);
});

it('should create the correct content within the config file', async () => {
expect(
yaml.load(readFileSync('hubspot.config.yml', 'utf8')).portals[0]
yaml.load(readFileSync(CONFIG_FILE_PATH, 'utf8')).portals[0]
.personalAccessKey
).toEqual(config.personalAccessKey);
});

it('should populate the config file with the correct defaultPortal', async () => {
const config = yaml.load(readFileSync('hubspot.config.yml', 'utf8'));
const config = yaml.load(readFileSync(CONFIG_FILE_PATH, 'utf8'));
expect(config.defaultPortal).toEqual('QA');
});
});
4 changes: 3 additions & 1 deletion acceptance-tests/spec/tests/list.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { CONFIG_FILE_PATH } = require('../../constants');

describe('hs list', () => {
const { cli } = global;

it('should print the correct output', async () => {
let val = await cli.execute(['list']);
let val = await cli.execute(['list', `--c="${CONFIG_FILE_PATH}"`]);
expect(val).toContain('@marketplace');
}, 20000);
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"publish-release": "yarn lerna publish --conventional-graduate",
"publish-prerelease": "yarn lerna publish prerelease --preid beta --dist-tag next",
"test": "jest",
"test-cli": "yarn --cwd 'acceptance-tests' && ./acceptance-tests/run-tests",
"jest": "jest --watch",
"circular-deps": "yarn madge --circular packages"
},
Expand Down
22 changes: 6 additions & 16 deletions packages/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
updateAccountConfig,
setConfigPath,
} = require('@hubspot/cli-lib/lib/config');
const { addConfigOptions } = require('../lib/commonOpts');
const { handleExit } = require('@hubspot/cli-lib/lib/process');
const { logErrorInstance } = require('@hubspot/cli-lib/errorHandlers');
const {
Expand Down Expand Up @@ -84,10 +85,7 @@ exports.command = 'init';
exports.describe = `initialize ${DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME} for a HubSpot account`;

exports.handler = async options => {
const {
auth: authType = PERSONAL_ACCESS_KEY_AUTH_METHOD.value,
force,
} = options;
const { auth: authType = PERSONAL_ACCESS_KEY_AUTH_METHOD.value, c } = options;
const configPath = getConfigPath();
setLogLevel(options);
logDebugInfo(options);
Expand All @@ -96,7 +94,7 @@ exports.handler = async options => {
});
const env = options.qa ? ENVIRONMENTS.QA : ENVIRONMENTS.PROD;

if (!force && configPath) {
if ((!c && configPath) || (c && configPath === c)) {
logger.error(`The config file '${configPath}' already exists.`);
logger.info(
'To update an existing config file, use the "hs auth" command.'
Expand All @@ -106,10 +104,8 @@ exports.handler = async options => {

trackAuthAction('init', authType, TRACKING_STATUS.STARTED);

if (force) {
setConfigPath(
path.join(process.cwd(), DEFAULT_HUBSPOT_CONFIG_YAML_FILE_NAME)
);
if (c) {
setConfigPath(path.join(process.cwd(), c));
}
createEmptyConfigFile();
handleExit(deleteEmptyConfigFile);
Expand Down Expand Up @@ -145,13 +141,7 @@ exports.builder = yargs => {
defaultDescription: `"${PERSONAL_ACCESS_KEY_AUTH_METHOD.value}": \nAn access token tied to a specific user account. This is the recommended way of authenticating with local development tools.`,
});

yargs.option('force', {
describe:
'force config creation without looking for config in parent folders',
type: 'boolean',
default: false,
});

addConfigOptions(yargs, true);
addTestingOptions(yargs, true);

return yargs;
Expand Down

0 comments on commit 348fdfe

Please sign in to comment.