|
| 1 | +import { PassThrough } from 'stream'; |
1 | 2 | import { CLI, ProblemError } from '@snyk/error-catalog-nodejs-public'; |
2 | 3 | import { CustomError } from '../../../../../src/lib/errors'; |
3 | 4 | import { FailedProjectScanError } from '../../../../../src/lib/plugins/get-multi-plugin-result'; |
4 | 5 | import { |
5 | 6 | getOrCreateErrorCatalogError, |
6 | 7 | getRequestConcurrency, |
| 8 | + printDepGraphError, |
7 | 9 | } from '../../../../../src/lib/snyk-test/common'; |
8 | 10 |
|
9 | 11 | describe('getOrCreateErrorCatalogError', () => { |
@@ -120,6 +122,66 @@ describe('getOrCreateErrorCatalogError', () => { |
120 | 122 | }); |
121 | 123 | }); |
122 | 124 |
|
| 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 | + |
123 | 185 | describe('getRequestConcurrency', () => { |
124 | 186 | const originalValue = process.env.SNYK_INTERNAL_REQUEST_CONCURRENCY; |
125 | 187 |
|
|
0 commit comments