Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib/snyk-test/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,10 @@ export async function printDepGraphError(
return new Promise((res, rej) => {
// Normalize the target file path to be relative to root, consistent with printDepGraphJsonl
const normalisedTargetFile = failedProjectScanError.targetFile
? path.relative(root, failedProjectScanError.targetFile)
? path.relative(
root,
path.resolve(root, failedProjectScanError.targetFile),
Comment thread
danskmt marked this conversation as resolved.
)
: failedProjectScanError.targetFile;

const problemError = getOrCreateErrorCatalogError(failedProjectScanError);
Expand Down
62 changes: 62 additions & 0 deletions test/jest/unit/lib/snyk-test/common.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { PassThrough } from 'stream';
import { CLI, ProblemError } from '@snyk/error-catalog-nodejs-public';
import { CustomError } from '../../../../../src/lib/errors';
import { FailedProjectScanError } from '../../../../../src/lib/plugins/get-multi-plugin-result';
import {
getOrCreateErrorCatalogError,
getRequestConcurrency,
printDepGraphError,
} from '../../../../../src/lib/snyk-test/common';

describe('getOrCreateErrorCatalogError', () => {
Expand Down Expand Up @@ -120,6 +122,66 @@ describe('getOrCreateErrorCatalogError', () => {
});
});

describe('printDepGraphError', () => {
function collectStream(stream: PassThrough): Promise<string> {
return new Promise((resolve) => {
const chunks: Buffer[] = [];
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
});
}

it('normalises a relative targetFile to stay relative to root', async () => {
const root = '/project';
const failedProjectScanError: FailedProjectScanError = {
errMessage: 'scan failed',
error: undefined,
targetFile: 'subdir/package.json',
};
const output = new PassThrough();
const collected = collectStream(output);

await printDepGraphError(root, failedProjectScanError, output);
output.end();

const result = JSON.parse(await collected);
expect(result.normalisedTargetFile).toBe('subdir/package.json');
});

it('normalises an absolute targetFile to be relative to root', async () => {
const root = '/project';
const failedProjectScanError: FailedProjectScanError = {
errMessage: 'scan failed',
error: undefined,
targetFile: '/project/subdir/package.json',
};
const output = new PassThrough();
const collected = collectStream(output);

await printDepGraphError(root, failedProjectScanError, output);
output.end();

const result = JSON.parse(await collected);
expect(result.normalisedTargetFile).toBe('subdir/package.json');
});

it('keeps targetFile undefined when it is not provided', async () => {
const root = '/project';
const failedProjectScanError: FailedProjectScanError = {
errMessage: 'scan failed',
error: undefined,
};
const output = new PassThrough();
const collected = collectStream(output);

await printDepGraphError(root, failedProjectScanError, output);
output.end();

const result = JSON.parse(await collected);
expect(result.normalisedTargetFile).toBeUndefined();
});
});

describe('getRequestConcurrency', () => {
const originalValue = process.env.SNYK_INTERNAL_REQUEST_CONCURRENCY;

Expand Down
Loading