Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: snyk/cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.926.0
Choose a base ref
...
head repository: snyk/cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.927.0
Choose a head ref
  • 4 commits
  • 7 files changed
  • 4 contributors

Commits on May 11, 2022

  1. feat: Rename Invalid Files section for IaC

    Ilianna Papastefanou committed May 11, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    francescomari Francesco Mari
    Copy the full SHA
    1d21526 View commit details
  2. feat: Add progress indicator

    ofekatr committed May 11, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    764e0ce View commit details
  3. Merge pull request #3224 from snyk/feat/rename-invalid-files-section-c…

    …fg-1834
    
    feat: Update 'Invalid Files' section for IaC [CFG-1834]
    ipapast authored May 11, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    JackuB Jakub Mikulas
    Copy the full SHA
    2c8f81e View commit details
  4. Merge pull request #3221 from snyk/chore/add-progress-indicator-cfg-1757

    
    
    feat: Add progress indicator
    ofekatr authored May 11, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    18757fd View commit details
18 changes: 17 additions & 1 deletion src/cli/commands/test/iac/index.ts
Original file line number Diff line number Diff line change
@@ -27,8 +27,11 @@ import * as utils from '../utils';
import {
formatIacTestFailures,
getIacDisplayErrorFileOutput,
iacTestTitle,
shouldLogUserMessages,
spinnerFailureMessage,
spinnerMessage,
spinnerSuccessMessage,
} from '../../../../lib/formatters/iac-output';
import { getEcosystemForTest, testEcosystem } from '../../../../lib/ecosystems';
import {
@@ -72,6 +75,7 @@ import {
import config from '../../../../lib/config';
import { UnsupportedEntitlementError } from '../../../../lib/errors/unsupported-entitlement-error';
import { failuresTipOutput } from '../../../../lib/formatters/iac-output';
import * as ora from 'ora';

const debug = Debug('snyk-test');
const SEPARATOR = '\n-------------------------------------------------------\n';
@@ -119,6 +123,8 @@ export default async function(
}
}

let testSpinner: ora.Ora | undefined;

const resultOptions: Array<Options & TestOptions> = [];
const results = [] as any[];

@@ -130,7 +136,9 @@ export default async function(
const isNewIacOutputSupported = await hasFeatureFlag('iacCliOutput', options);

if (shouldLogUserMessages(options, isNewIacOutputSupported)) {
console.log(EOL + spinnerMessage);
console.log(EOL + iacTestTitle + EOL);

testSpinner = ora({ isSilent: options.quiet, stream: process.stdout });
}

const orgPublicId = (options.org as string) ?? config.org;
@@ -141,6 +149,8 @@ export default async function(
}

try {
testSpinner?.start(spinnerMessage);

const rulesOrigin = await initRules(iacOrgSettings, options);

for (const path of paths) {
@@ -218,6 +228,12 @@ export default async function(
const notSuccess = errorResults.length > 0;
const foundVulnerabilities = vulnerableResults.length > 0;

if (notSuccess) {
testSpinner?.fail(spinnerFailureMessage + EOL);
} else {
testSpinner?.succeed(spinnerSuccessMessage);
}

// resultOptions is now an array of 1 or more options used for
// the tests results is now an array of 1 or more test results
// values depend on `options.json` value - string or object
3 changes: 3 additions & 0 deletions src/lib/formatters/iac-output/index.ts
Original file line number Diff line number Diff line change
@@ -10,7 +10,10 @@ export {
formatIacTestSummary,
getIacDisplayedIssues,
formatIacTestFailures,
iacTestTitle,
spinnerMessage,
spinnerSuccessMessage,
spinnerFailureMessage,
shouldLogUserMessages,
formatShareResultsOutput,
failuresTipOutput,
2 changes: 1 addition & 1 deletion src/lib/formatters/iac-output/v2/failures/list.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import { colors } from '../color-utils';
export function formatIacTestFailures(testFailures: IacFileInDirectory[]) {
const sectionComponents: string[] = [];

const titleOutput = colors.info.bold(`Invalid Files: ${testFailures.length}`);
const titleOutput = colors.info.bold(`Test Failures`);
sectionComponents.push(titleOutput);

const testFailuresListOutput = formatFailuresList(testFailures);
8 changes: 7 additions & 1 deletion src/lib/formatters/iac-output/v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export { getIacDisplayedIssues } from './issues-list';
export { formatIacTestSummary } from './test-summary';
export { spinnerMessage, shouldLogUserMessages } from './user-messages';
export {
iacTestTitle,
spinnerMessage,
spinnerSuccessMessage,
spinnerFailureMessage,
shouldLogUserMessages,
} from './user-messages';
export { formatShareResultsOutput } from './share-results';
export { formatIacTestFailures, failuresTipOutput } from './failures';
21 changes: 19 additions & 2 deletions src/lib/formatters/iac-output/v2/user-messages.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import { IaCTestFlags } from '../../../../cli/commands/test/iac/local-execution/types';
import { colors } from './color-utils';

/**
* Displayed as the title of the test output.
*/
export const iacTestTitle = colors.info.bold('Snyk Infrastructure as Code');

/**
* Progress indication message while files are tested.
*/
export const spinnerMessage = colors.info.bold(
'Snyk testing Infrastructure as Code configuration issues...',
export const spinnerMessage = colors.info(
'Snyk testing Infrastructure as Code configuration issues.',
);

/**
* Displayed when a test resolves successfully.
*/
export const spinnerSuccessMessage = colors.info('Test completed.');

/**
* Displayed when a test fails.
*/
export const spinnerFailureMessage = colors.info(
'Unable to complete the test.',
);

/**
70 changes: 57 additions & 13 deletions test/jest/acceptance/iac/iac-output.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import chalk from 'chalk';
import { EOL } from 'os';
import * as pathLib from 'path';
import {
spinnerFailureMessage,
spinnerMessage,
spinnerSuccessMessage,
} from '../../../../src/lib/formatters/iac-output';

import { FakeServer } from '../../../acceptance/fake-server';
import { startMockServer } from './helpers';
@@ -16,9 +22,6 @@ describe('iac test output', () => {
) => Promise<{ stdout: string; stderr: string; exitCode: number }>;
let teardown: () => Promise<unknown>;

const initialMessage =
'Snyk testing Infrastructure as Code configuration issues...';

beforeAll(async () => {
({ server, run, teardown } = await startMockServer());
});
@@ -36,13 +39,41 @@ describe('iac test output', () => {
server.setFeatureFlag(IAC_CLI_OUTPUT_FF, true);
});

it('should show an initial message', async () => {
const { stdout } = await run('snyk iac test ./iac/arm/rule_test.json');
it('should show the IaC test title', async () => {
// Arrange
const dirPath = './iac/terraform';

// Act
const { stdout } = await run(`snyk iac test ${dirPath}`);

// Assert
expect(stdout).toContain('Snyk Infrastructure as Code');
});

it('should show the spinner message', async () => {
// Arrange
const dirPath = './iac/terraform';

// Act
const { stdout } = await run(`snyk iac test ${dirPath}`);

// Arrange
expect(stdout).toContain(
'Snyk testing Infrastructure as Code configuration issues...',
'Snyk testing Infrastructure as Code configuration issues.',
);
});

it('should show the test completion message', async () => {
// Arrange
const dirPath = './iac/terraform';

// Act
const { stdout } = await run(`snyk iac test ${dirPath}`);

// Assert
expect(stdout).toContain('Test completed.');
});

it('should show the issues list section with correct values', async () => {
const { stdout } = await run('snyk iac test ./iac/arm/rule_test.json');

@@ -123,21 +154,23 @@ Target file: ${dirPath}/`);
const { stdout } = await run(`snyk iac test ${dirPath}`);

// Assert
expect(stdout).not.toContain('Invalid Files');
expect(stdout).not.toContain('Test Failures');
});

describe.each`
dataFormat | dataFormatFlag
${'JSON'} | ${'--json'}
${'SARIF'} | ${'--sarif'}
`(
'when providing the $dataFormatFlag flag',
({ dataFormat, dataFormatFlag }) => {
it(`should not show an initial message for ${dataFormat} output`, async () => {
'when providing the $dataFormatFlag flag for the $dataFormat format',
({ dataFormatFlag }) => {
it(`should not show spinner messages`, async () => {
const { stdout } = await run(
`snyk iac test ${dataFormatFlag} ./iac/arm/rule_test.json`,
);
expect(stdout).not.toContain(initialMessage);
expect(stdout).not.toContain(chalk.reset(spinnerMessage));
expect(stdout).not.toContain(chalk.reset(spinnerFailureMessage));
expect(stdout).not.toContain(chalk.reset(spinnerSuccessMessage));
});
},
);
@@ -201,6 +234,17 @@ Target file: ${dirPath}/`);
});

describe('with only test failures', () => {
it('should the test failure message', async () => {
// Arrange
const dirPath = 'iac/only-invalid';

// Act
const { stdout } = await run(`snyk iac test ${dirPath}`);

// Assert
expect(stdout).toContain('Unable to complete the test.');
});

it('should display the failure reason for the first failed test', async () => {
// Arrange
const dirPath = 'iac/only-invalid';
@@ -241,7 +285,7 @@ https://support.snyk.io/hc/en-us/articles/360013723877-Test-your-Terraform-files
});

describe('with no issues', () => {
it('it should display an appropriate message in the issues section', async () => {
it('should display an appropriate message in the issues section', async () => {
// Arrange
const filePath = 'iac/terraform/vars.tf';

@@ -345,7 +389,7 @@ Project path: ${filePath}

it('should not show an initial message', async () => {
const { stdout } = await run('snyk iac test ./iac/arm/rule_test.json');
expect(stdout).not.toContain(initialMessage);
expect(stdout).not.toContain(chalk.reset(spinnerMessage));
});

it('should not show a subtitle for medium severity issues', async () => {
Original file line number Diff line number Diff line change
@@ -13,12 +13,12 @@ const testFailureFixtures: IacFileInDirectory[] = JSON.parse(
);

describe('formatIacTestFailures', () => {
it('should include the "Invalid Files: X" title with the correct value', () => {
it('should include the "Invalid files: X" title with the correct value', () => {
// Act
const result = formatIacTestFailures(testFailureFixtures);

// Assert
expect(result).toContain(colors.info.bold(`Invalid Files: 5`));
expect(result).toContain(colors.info.bold(`Test Failures`));
});

it('should include the failures list with the correct values', () => {