Skip to content

Commit

Permalink
test: support running accpetance tests against the published version …
Browse files Browse the repository at this point in the history
…of the CLI
  • Loading branch information
brandenrodgers committed Aug 22, 2024
1 parent e741bd9 commit e757a3f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/testLatestPublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ jobs:
uses: actions/setup-node@v2
with:
node-version: 16.x
- name: Install Deps
run: npm install
- name: Test Latest CLI Release
run: |
npx --yes --package=@hubspot/cli@latest --call='hs --help --use-env'
shell: bash
npm run test-cli --cliNPMVersion="latest"
env:
PORTAL_ID: ${{ secrets.ACCEPTANCE_TEST_PORTAL_ID }}
PERSONAL_ACCESS_KEY: ${{ secrets.ACCEPTANCE_TEST_PERSONAL_ACCESS_KEY }}
10 changes: 9 additions & 1 deletion acceptance-tests/lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const getEnvValue = envVariable => {
const setArgsOverrides = args => {
args.portalId && (argsOverrides.portalId = args.portalId);
args.cliPath && (argsOverrides.cliPath = args.cliPath);
args.cliNPMVersion && (argsOverrides.cliNPMVersion = args.cliNPMVersion);
args.personalAccessKey &&
(argsOverrides.personalAccessKey = args.personalAccessKey);
argsOverrides.qa = args.qa;
Expand All @@ -45,6 +46,7 @@ const envOverrides = getTruthyValuesOnly({
portalId: getEnvValue('PORTAL_ID') || getEnvValue('ACCOUNT_ID'),
cliPath: getEnvValue('CLI_PATH'),
personalAccessKey: getEnvValue('PERSONAL_ACCESS_KEY'),
cliNPMVersion: getEnvValue('CLI_NPM_VERSION'),
});

const getTestConfig = () => {
Expand All @@ -57,7 +59,13 @@ const getTestConfig = () => {
);
}

if (!config.cliPath) {
if (config.cliPath && config.cliNPMVersion) {
throw new Error(
'You cannot specify both a cliPath and a cliNPMVersion. Remove one and try again.'
);
}

if (!config.cliPath && !config.cliNPMVersion) {
const defaultPath = path.join(process.cwd(), DEFAULT_CLI_PATH);

if (existsSync(defaultPath)) {
Expand Down
13 changes: 9 additions & 4 deletions acceptance-tests/run-tests
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ setArgsOverrides(
.option('accountId', {
alias: ['p', 'portalId', 'a'],
type: 'string',
description: 'Account ID',
description: 'The account ID to use',
})
.option('cliPath', {
alias: 'c',
type: 'string',
description: 'CLI path',
description: 'The path to the CLI',
})
.option('cliNPMVersion', {
alias: 'npm',
type: "string",
description: "The npm version of the CLI to download and run"
})
.option('personalAccessKey', {
alias: 'pak',
Expand All @@ -33,7 +38,7 @@ setArgsOverrides(
.option('qa', {
type: 'boolean',
default: false,
description: 'Set this if you are using a app.hubspotqa.com site',
description: 'Set this if you are using an app.hubspotqa.com account',
})
.option('headless', {
type: 'boolean',
Expand All @@ -55,7 +60,7 @@ setArgsOverrides(
testRunner.addReporter(reporter);

global.config = getTestConfig();
global.cli = cmd.createCli(global.config.cliPath);
global.cli = cmd.createCli(global.config.cliPath, global.config.cliNPMVersion);

await global.cli.execute(
['init', `--c="${CONFIG_FILE_NAME}"`],
Expand Down
40 changes: 28 additions & 12 deletions acceptance-tests/tests/helpers/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,31 @@ const PATH = process.env.PATH;

/**
* Creates a child process with script path
* @param {string} processPath Path of the process to execute
* @param {string} cliPath Path of the CLI process to execute
* @param {string} cliNPMVersion NPM Version number
* @param {Array} args Arguments to the command
* @param {Object} env (optional) Environment variables
*/
function createProcess(processPath, args = [], env = null) {
// Ensure that path exists
if (!processPath || !existsSync(processPath)) {
throw new Error('Invalid process path');
}
function createProcess(cliPath, cliNPMVersion, args = [], env = null) {
let processCommand;

args = [processPath].concat(args);
if (cliNPMVersion) {
processCommand = 'npx';
args = ['--yes', '--package', `@hubspot/cli@${cliNPMVersion}`, 'hs'].concat(
args
);
} else {
// Ensure that path exists
if (!cliPath || !existsSync(cliPath)) {
throw new Error(`Invalid process path ${cliPath}`);
}
processCommand = 'node';
args = [cliPath].concat(args);
}

// This works for node based CLIs, but can easily be adjusted to
// any other process installed in the system
return spawn('node', args, {
return spawn(processCommand, args, {
env: Object.assign(
{
NODE_ENV: 'test',
Expand All @@ -48,7 +58,13 @@ function createProcess(processPath, args = [], env = null) {
* @param {Array} inputs (Optional) Array of inputs (user responses)
* @param {Object} opts (optional) Environment variables
*/
function executeWithInput(processPath, args = [], inputs = [], opts = {}) {
function executeWithInput(
cliPath,
cliNPMVersion,
args = [],
inputs = [],
opts = {}
) {
if (!Array.isArray(inputs)) {
opts = inputs;
inputs = [];
Expand All @@ -59,7 +75,7 @@ function executeWithInput(processPath, args = [], inputs = [], opts = {}) {
}

const { env = opts.env, timeout = 500, maxTimeout = 10000 } = opts;
const childProcess = createProcess(processPath, args, env);
const childProcess = createProcess(cliPath, cliNPMVersion, args, env);
childProcess.stdin.setEncoding('utf-8');

let currentInputTimeout, killIOTimeout;
Expand Down Expand Up @@ -171,8 +187,8 @@ function executeWithInput(processPath, args = [], inputs = [], opts = {}) {

module.exports = {
createProcess,
createCli: processPath => ({
execute: (...args) => executeWithInput(processPath, ...args),
createCli: (cliPath, cliNPMVersion) => ({
execute: (...args) => executeWithInput(cliPath, cliNPMVersion, ...args),
}),
DOWN: '\x1B\x5B\x42',
UP: '\x1B\x5B\x41',
Expand Down
1 change: 0 additions & 1 deletion acceptance-tests/tests/list.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe('hs list', () => {

it('should print the correct output', async () => {
let val = await cli.execute(['list', `--c="${CONFIG_FILE_NAME}"`]);
console.log(val);
expect(val).toContain('@marketplace');
}, 20000);
});

0 comments on commit e757a3f

Please sign in to comment.