Skip to content

Commit

Permalink
fix: add a --project- prefix to all flags
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysh committed Oct 11, 2021
1 parent 779cb14 commit 90a6266
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 34 deletions.
22 changes: 13 additions & 9 deletions src/cli/commands/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,16 +360,20 @@ function getProjectAttribute<T>(
export function generateProjectAttributes(options): ProjectAttributes {
return {
criticality: getProjectAttribute(
'business-criticality',
'project-business-criticality',
PROJECT_CRITICALITY,
options,
),
environment: getProjectAttribute(
'environment',
'project-environment',
PROJECT_ENVIRONMENT,
options,
),
lifecycle: getProjectAttribute('lifecycle', PROJECT_LIFECYCLE, options),
lifecycle: getProjectAttribute(
'project-lifecycle',
PROJECT_LIFECYCLE,
options,
),
};
}

Expand All @@ -384,25 +388,25 @@ export function generateProjectAttributes(options): ProjectAttributes {
* @returns List of parsed tags or undefined if they are to be left untouched.
*/
export function generateTags(options): Tag[] | undefined {
if (options.tags === undefined) {
if (options['project-tags'] === undefined) {
return undefined;
}

if (options.tags === '') {
if (options['project-tags'] === '') {
return [];
}

// When it's specified without the =, we raise an explicit error to avoid
// accidentally clearing the existing tags;
if (options.tags === true) {
if (options['project-tags'] === true) {
throw new ValidationError(
`--tags must contain an '=' with a comma-separated list of pairs (also separated with an '='). To clear all existing values, pass no values i.e. --tags=`,
`--project-tags must contain an '=' with a comma-separated list of pairs (also separated with an '='). To clear all existing values, pass no values i.e. --project-tags=`,
);
}

const tags: Tag[] = [];
const keyEqualsValuePairs = options.tags.split(',');
const keyEqualsValuePairs = options['project-tags'].split(',');

const tags: Tag[] = [];
for (const keyEqualsValue of keyEqualsValuePairs) {
const parts = keyEqualsValue.split('=');
if (parts.length !== 2) {
Expand Down
12 changes: 6 additions & 6 deletions test/jest/acceptance/cli-args.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,25 +278,25 @@ describe('cli args', () => {
});
});

test('project attributes are implemented (--business-criticality, --lifecycle, --environment)', async () => {
test('project attributes are implemented --project{-business-criticality, -lifecycle, -environment}', async () => {
const { code, stdout } = await runSnykCLI(
`monitor --business-criticality`,
`monitor --project-business-criticality`,
{
env,
},
);
expect(stdout).toMatch(
"--business-criticality must contain an '=' with a comma-separated list of values. To clear all existing values, pass no values i.e. --business-criticality=",
"--project-business-criticality must contain an '=' with a comma-separated list of values. To clear all existing values, pass no values i.e. --project-business-criticality=",
);
expect(code).toEqual(2);
});

test('snyk monitor --tags is implemented', async () => {
const { code, stdout } = await runSnykCLI(`monitor --tags`, {
test('snyk monitor --project-tags is implemented', async () => {
const { code, stdout } = await runSnykCLI(`monitor --project-tags`, {
env,
});
expect(stdout).toMatch(
"--tags must contain an '=' with a comma-separated list of pairs (also separated with an '='). To clear all existing values, pass no values i.e. --tags=",
"--project-tags must contain an '=' with a comma-separated list of pairs (also separated with an '='). To clear all existing values, pass no values i.e. --project-tags=",
);
expect(code).toEqual(2);
});
Expand Down
26 changes: 15 additions & 11 deletions test/jest/acceptance/snyk-monitor/project-attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe('project attributes (--lifecycle, --environment, --business-criticality
it('parses the options correctly when they are valid', () => {
expect(
generateProjectAttributes({
'business-criticality': 'critical,high',
lifecycle: 'development,sandbox',
environment: 'backend,frontend',
'project-business-criticality': 'critical,high',
'project-lifecycle': 'development,sandbox',
'project-environment': 'backend,frontend',
}),
).toStrictEqual({
criticality: [PROJECT_CRITICALITY.CRITICAL, PROJECT_CRITICALITY.HIGH],
Expand All @@ -30,27 +30,31 @@ describe('project attributes (--lifecycle, --environment, --business-criticality

it('raises the correct error with an invalid business criticality', () => {
expect(() =>
generateProjectAttributes({ 'business-criticality': 'invalid' }),
generateProjectAttributes({ 'project-business-criticality': 'invalid' }),
).toThrow(
'1 invalid business-criticality: invalid. Possible values are: critical, high, medium, low',
'1 invalid project-business-criticality: invalid. Possible values are: critical, high, medium, low',
);
});

it('raises the correct error with an invalid lifecycle', () => {
expect(() => generateProjectAttributes({ lifecycle: 'invalid' })).toThrow(
'1 invalid lifecycle: invalid. Possible values are: production, development, sandbox',
expect(() =>
generateProjectAttributes({ 'project-lifecycle': 'invalid' }),
).toThrow(
'1 invalid project-lifecycle: invalid. Possible values are: production, development, sandbox',
);
});

it('raises the correct error with an invalid environment', () => {
expect(() => generateProjectAttributes({ environment: 'invalid' })).toThrow(
'1 invalid environment: invalid. Possible values are: frontend, backend, internal, external, mobile, saas, onprem, hosted, distributed',
expect(() =>
generateProjectAttributes({ 'project-environment': 'invalid' }),
).toThrow(
'1 invalid project-environment: invalid. Possible values are: frontend, backend, internal, external, mobile, saas, onprem, hosted, distributed',
);
});

it('raises the correct error with multiple invalid attributes', () => {
expect(() =>
generateProjectAttributes({ lifecycle: 'invalid1,invalid2' }),
).toThrow(/2 invalid lifecycle: invalid1, invalid2/);
generateProjectAttributes({ 'project-lifecycle': 'invalid1,invalid2' }),
).toThrow(/2 invalid project-lifecycle: invalid1, invalid2/);
});
});
16 changes: 8 additions & 8 deletions test/jest/acceptance/snyk-monitor/tags.spec.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { generateTags } from '../../../../src/cli/commands/monitor/index';

describe('--tags', () => {
it('raises the correct error when passed as --tags (i.e. missing the =)', () => {
expect(() => generateTags({ tags: true })).toThrow(
it('raises the correct error when passed as --project-tags (i.e. missing the =)', () => {
expect(() => generateTags({ 'project-tags': true })).toThrow(
/must contain.*comma-separated/,
);
});

it('returns an empty set of tags when passed as --tags=', () => {
expect(generateTags({ tags: '' })).toStrictEqual([]);
it('returns an empty set of tags when passed as --project-tags=', () => {
expect(generateTags({ 'project-tags': '' })).toStrictEqual([]);
});

it('returns undefined when --tags is not passed', () => {
it('returns undefined when --project-tags is not passed', () => {
expect(generateTags({ someOtherArg: true })).toBeUndefined();
});

it('raises the correct error when a key is supplied with no value', () => {
expect(() =>
generateTags({ tags: 'invalidAsOnlyAKeyWasSpecified' }),
generateTags({ 'project-tags': 'invalidAsOnlyAKeyWasSpecified' }),
).toThrow(/does not have an "="/);
});

it('parses a single key/value pair into the correct data structure', () => {
expect(generateTags({ tags: 'team=rhino' })).toStrictEqual([
expect(generateTags({ 'project-tags': 'team=rhino' })).toStrictEqual([
{
key: 'team',
value: 'rhino',
Expand All @@ -32,7 +32,7 @@ describe('--tags', () => {

it('parses multiple key/value pairs into the correct data structure', () => {
expect(
generateTags({ tags: 'team=rhino,department=finance' }),
generateTags({ 'project-tags': 'team=rhino,department=finance' }),
).toStrictEqual([
{
key: 'team',
Expand Down

0 comments on commit 90a6266

Please sign in to comment.