Skip to content

Commit

Permalink
Make sure error exit codes are set properly
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Oct 22, 2019
1 parent ace9d6b commit ab5ae11
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
13 changes: 11 additions & 2 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const deploy = require('@statickit/deploy');
const ora = require('ora');
const version = require('../../package.json').version;

const setErrorExit = () => {
process.exitCode = 1;
};

exports.command = ['deploy', '$0'];

exports.describe = 'Performs a deployment';
Expand Down Expand Up @@ -37,7 +41,7 @@ exports.handler = async args => {

if (!rawConfig) {
console.error(chalk.bold.red('Configuration not provided'));
process.exitCode = 1;
setErrorExit();
return;
}

Expand All @@ -47,14 +51,15 @@ exports.handler = async args => {
config = JSON.parse(rawConfig);
} catch (err) {
console.error(chalk.bold.red('Configuration could not be parsed'));
process.exitCode = 1;
setErrorExit();
return;
}

const key = deploy.getDeployKey(args);

if (!key) {
console.error(chalk.bold.red('Deploy key not found'));
setErrorExit();
return;
}

Expand All @@ -78,6 +83,7 @@ exports.handler = async args => {

case 401:
console.error(`--> ${chalk.red('Deploy key is not valid')}`);
setErrorExit();
return;

case 422:
Expand All @@ -88,15 +94,18 @@ exports.handler = async args => {
console.table(response.data.errors);
console.log('');
console.log(`${chalk.gray('id: ' + response.data.id)}`);
setErrorExit();
return;

default:
console.error(`--> ${chalk.red('Deployment failed')}`);
setErrorExit();
return;
}
} catch (error) {
spinner.stop();
console.error(`--> ${chalk.red('Deployment failed unexpectedly')}`);
setErrorExit();
throw error;
}
};
36 changes: 28 additions & 8 deletions test/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,24 @@ it('returns an error if deploy key is invalid', async () => {
try {
await command(`deploy -c '{}' -k invalidkey`);
} catch (result) {
console.log(result);
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(/Deploy key is not valid/);
}
});

it('succeeds given valid params', async () => {
const { stdout } = await command(
const { stdout, exitCode } = await command(
`deploy -c '{}' -k ${process.env.STATICKIT_TEST_DEPLOY_KEY}`
);
expect(exitCode).toBe(0);
expect(stdout).toMatch(/Deployment succeeded/);
});

it('accepts a deploy key from env', async () => {
const { stdout } = await command(`deploy -c '{}'`, {
const { stdout, exitCode } = await command(`deploy -c '{}'`, {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
expect(exitCode).toBe(0);
expect(stdout).toMatch(/Deployment succeeded/);
});

Expand All @@ -97,24 +98,43 @@ it('accepts a deploy key from .env file', async () => {
'utf8'
);

const { stdout } = await command(`deploy -c '{}'`);
const { stdout, exitCode } = await command(`deploy -c '{}'`);
expect(exitCode).toBe(0);
expect(stdout).toMatch(/Deployment succeeded/);
});

it('accepts a config from the statickit.json file', async () => {
await fs.writeFile(resolvePath('statickit.json'), '{}', 'utf8');

const { stdout } = await command('deploy', {
const { stdout, exitCode } = await command('deploy', {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
expect(exitCode).toBe(0);
expect(stdout).toMatch(/Deployment succeeded/);
});

it('accepts a config from a custom file', async () => {
await fs.writeFile(resolvePath('statickit-custom.json'), '{}', 'utf8');

const { stdout } = await command('deploy --file statickit-custom.json', {
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
});
const { stdout, exitCode } = await command(
'deploy --file statickit-custom.json',
{
env: { STATICKIT_DEPLOY_KEY: process.env.STATICKIT_TEST_DEPLOY_KEY }
}
);
expect(exitCode).toBe(0);
expect(stdout).toMatch(/Deployment succeeded/);
});

it('fails given invalid params', async () => {
try {
await command(
`deploy -c '{"forms":{"a":{"name":""}}}' -k ${process.env.STATICKIT_TEST_DEPLOY_KEY}`
);
} catch (result) {
expect(result.exitCode).toBe(1);
expect(result.stderr).toMatch(
/Deployment failed due to configuration errors/
);
}
});

0 comments on commit ab5ae11

Please sign in to comment.