Skip to content

Commit 7d2b57f

Browse files
test: add printDepGraphError unit tests [CSENG-200]
1 parent 899e54a commit 7d2b57f

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

test/jest/unit/lib/snyk-test/common.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { PassThrough } from 'stream';
12
import { CLI, ProblemError } from '@snyk/error-catalog-nodejs-public';
23
import { CustomError } from '../../../../../src/lib/errors';
34
import { FailedProjectScanError } from '../../../../../src/lib/plugins/get-multi-plugin-result';
45
import {
56
getOrCreateErrorCatalogError,
67
getRequestConcurrency,
8+
printDepGraphError,
79
} from '../../../../../src/lib/snyk-test/common';
810

911
describe('getOrCreateErrorCatalogError', () => {
@@ -120,6 +122,66 @@ describe('getOrCreateErrorCatalogError', () => {
120122
});
121123
});
122124

125+
describe('printDepGraphError', () => {
126+
function collectStream(stream: PassThrough): Promise<string> {
127+
return new Promise((resolve) => {
128+
const chunks: Buffer[] = [];
129+
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)));
130+
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf-8')));
131+
});
132+
}
133+
134+
it('normalises a relative targetFile to stay relative to root', async () => {
135+
const root = '/project';
136+
const failedProjectScanError: FailedProjectScanError = {
137+
errMessage: 'scan failed',
138+
error: undefined,
139+
targetFile: 'subdir/package.json',
140+
};
141+
const output = new PassThrough();
142+
const collected = collectStream(output);
143+
144+
await printDepGraphError(root, failedProjectScanError, output);
145+
output.end();
146+
147+
const result = JSON.parse(await collected);
148+
expect(result.normalisedTargetFile).toBe('subdir/package.json');
149+
});
150+
151+
it('normalises an absolute targetFile to be relative to root', async () => {
152+
const root = '/project';
153+
const failedProjectScanError: FailedProjectScanError = {
154+
errMessage: 'scan failed',
155+
error: undefined,
156+
targetFile: '/project/subdir/package.json',
157+
};
158+
const output = new PassThrough();
159+
const collected = collectStream(output);
160+
161+
await printDepGraphError(root, failedProjectScanError, output);
162+
output.end();
163+
164+
const result = JSON.parse(await collected);
165+
expect(result.normalisedTargetFile).toBe('subdir/package.json');
166+
});
167+
168+
it('keeps targetFile undefined when it is not provided', async () => {
169+
const root = '/project';
170+
const failedProjectScanError: FailedProjectScanError = {
171+
errMessage: 'scan failed',
172+
error: undefined,
173+
};
174+
const output = new PassThrough();
175+
const collected = collectStream(output);
176+
177+
await printDepGraphError(root, failedProjectScanError, output);
178+
output.end();
179+
180+
const result = JSON.parse(await collected);
181+
expect(result.normalisedTargetFile).toBeUndefined();
182+
});
183+
});
184+
123185
describe('getRequestConcurrency', () => {
124186
const originalValue = process.env.SNYK_INTERNAL_REQUEST_CONCURRENCY;
125187

0 commit comments

Comments
 (0)