|
| 1 | +/*! |
| 2 | +This file is part of CycloneDX generator for NPM projects. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +
|
| 16 | +SPDX-License-Identifier: Apache-2.0 |
| 17 | +Copyright (c) OWASP Foundation. All Rights Reserved. |
| 18 | +*/ |
| 19 | + |
| 20 | +const { join } = require('path') |
| 21 | +const { mkdirSync, writeFileSync, readFileSync } = require('fs') |
| 22 | + |
| 23 | +const { describe, expect, test } = require('@jest/globals') |
| 24 | + |
| 25 | +const { makeReproducible } = require('../_helper') |
| 26 | +const { UPDATE_SNAPSHOTS, mkTemp, runCLI, latestCdxSpecVersion, dummyProjectsRoot, npmLsReplacement, demoResultsRoot } = require('./') |
| 27 | + |
| 28 | +describe('integration.cli.edge-cases', () => { |
| 29 | + const cliRunTestTimeout = 15000 |
| 30 | + |
| 31 | + const tmpRoot = mkTemp('cli.edge_cases') |
| 32 | + |
| 33 | + describe('broken project', () => { |
| 34 | + const tmpRootRun = join(tmpRoot, 'broken-project') |
| 35 | + mkdirSync(tmpRootRun) |
| 36 | + |
| 37 | + test.each([ |
| 38 | + ['no-lockfile', /missing evidence/i], |
| 39 | + ['no-manifest', /missing .*manifest file/i] |
| 40 | + ])('%s', async (folderName, expectedError) => { |
| 41 | + const logFileBase = join(tmpRootRun, folderName) |
| 42 | + const cwd = join(dummyProjectsRoot, folderName) |
| 43 | + |
| 44 | + const { res, errFile } = runCLI([], logFileBase, cwd, { npm_execpath: undefined }) |
| 45 | + |
| 46 | + try { |
| 47 | + await expect(res).rejects.toThrow(expectedError) |
| 48 | + } catch (err) { |
| 49 | + process.stderr.write(readFileSync(errFile)) |
| 50 | + throw err |
| 51 | + } |
| 52 | + }, cliRunTestTimeout) |
| 53 | + }) |
| 54 | + |
| 55 | + describe('with broken npm-ls', () => { |
| 56 | + const tmpRootRun = join(tmpRoot, 'with-broken') |
| 57 | + mkdirSync(tmpRootRun) |
| 58 | + |
| 59 | + test('error on non-existing binary', async () => { |
| 60 | + const logFileBase = join(tmpRootRun, 'non-existing') |
| 61 | + const cwd = join(dummyProjectsRoot, 'with-lockfile') |
| 62 | + |
| 63 | + const { res, errFile } = runCLI([], logFileBase, cwd, { |
| 64 | + npm_execpath: npmLsReplacement.nonExistingBinary |
| 65 | + }) |
| 66 | + |
| 67 | + try { |
| 68 | + await expect(res).rejects.toThrow(/^unexpected npm execpath/i) |
| 69 | + } catch (err) { |
| 70 | + process.stderr.write(readFileSync(errFile)) |
| 71 | + throw err |
| 72 | + } |
| 73 | + }, cliRunTestTimeout) |
| 74 | + |
| 75 | + test('error on non-zero exit', async () => { |
| 76 | + const logFileBase = join(tmpRootRun, 'error-exit-nonzero') |
| 77 | + const cwd = join(dummyProjectsRoot, 'with-lockfile') |
| 78 | + |
| 79 | + const expectedExitCode = 1 + Math.floor(254 * Math.random()) |
| 80 | + |
| 81 | + const { res, errFile } = runCLI([], logFileBase, cwd, { |
| 82 | + CT_VERSION: '8.99.0', |
| 83 | + // non-zero exit code |
| 84 | + CT_EXIT_CODE: `${expectedExitCode}`, |
| 85 | + npm_execpath: npmLsReplacement.justExit |
| 86 | + }) |
| 87 | + |
| 88 | + try { |
| 89 | + await expect(res).rejects.toThrow(`npm-ls exited with errors: ${expectedExitCode} noSignal`) |
| 90 | + } catch (err) { |
| 91 | + process.stderr.write(readFileSync(errFile)) |
| 92 | + throw err |
| 93 | + } |
| 94 | + }, cliRunTestTimeout) |
| 95 | + |
| 96 | + test('error on broken json response', async () => { |
| 97 | + const logFileBase = join(tmpRootRun, 'error-json-broken') |
| 98 | + const cwd = join(dummyProjectsRoot, 'with-lockfile') |
| 99 | + |
| 100 | + const { res, errFile } = runCLI([], logFileBase, cwd, { |
| 101 | + CT_VERSION: '8.99.0', |
| 102 | + // abuse the npm-ls replacement, as it can be caused to crash under control. |
| 103 | + npm_execpath: npmLsReplacement.brokenJson |
| 104 | + }) |
| 105 | + |
| 106 | + try { |
| 107 | + await expect(res).rejects.toThrow(/failed to parse npm-ls response/i) |
| 108 | + } catch (err) { |
| 109 | + process.stderr.write(readFileSync(errFile)) |
| 110 | + throw err |
| 111 | + } |
| 112 | + }, cliRunTestTimeout) |
| 113 | + }) |
| 114 | + |
| 115 | + test('suppressed error on non-zero exit', async () => { |
| 116 | + const dd = { subject: 'dev-dependencies', npm: '8', node: '14', os: 'ubuntu-latest' } |
| 117 | + |
| 118 | + mkdirSync(join(tmpRoot, 'suppressed-error-on-non-zero-exit')) |
| 119 | + const expectedOutSnap = join(demoResultsRoot, 'suppressed-error-on-non-zero-exit', `${dd.subject}_npm${dd.npm}_node${dd.node}_${dd.os}.snap.json`) |
| 120 | + const logFileBase = join(tmpRoot, 'suppressed-error-on-non-zero-exit', `${dd.subject}_npm${dd.npm}_node${dd.node}_${dd.os}`) |
| 121 | + const cwd = dummyProjectsRoot |
| 122 | + |
| 123 | + const expectedExitCode = 1 + Math.floor(254 * Math.random()) |
| 124 | + |
| 125 | + const { res, outFile, errFile } = runCLI([ |
| 126 | + '-vvv', |
| 127 | + '--ignore-npm-errors', |
| 128 | + '--output-reproducible', |
| 129 | + // no intention to test all the spec-versions nor all the output-formats - this would be not our scope. |
| 130 | + '--spec-version', `${latestCdxSpecVersion}`, |
| 131 | + '--output-format', 'JSON', |
| 132 | + // prevent file interaction in this synthetic scenario - they would not exist anyway |
| 133 | + '--package-lock-only', |
| 134 | + '--', |
| 135 | + join('with-lockfile', 'package.json') |
| 136 | + ], logFileBase, cwd, { |
| 137 | + CT_VERSION: `${dd.npm}.99.0`, |
| 138 | + // non-zero exit code |
| 139 | + CT_EXIT_CODE: expectedExitCode, |
| 140 | + CT_SUBJECT: dd.subject, |
| 141 | + CT_NPM: dd.npm, |
| 142 | + CT_NODE: dd.node, |
| 143 | + CT_OS: dd.os, |
| 144 | + npm_execpath: npmLsReplacement.demoResults |
| 145 | + }) |
| 146 | + |
| 147 | + try { |
| 148 | + await expect(res).resolves.toBe(0) |
| 149 | + } catch (err) { |
| 150 | + process.stderr.write(readFileSync(errFile)) |
| 151 | + throw err |
| 152 | + } |
| 153 | + |
| 154 | + const actualOutput = makeReproducible('json', readFileSync(outFile, 'utf8')) |
| 155 | + |
| 156 | + if (UPDATE_SNAPSHOTS) { |
| 157 | + writeFileSync(expectedOutSnap, actualOutput, 'utf8') |
| 158 | + } |
| 159 | + |
| 160 | + expect(actualOutput).toEqual( |
| 161 | + readFileSync(expectedOutSnap, 'utf8'), |
| 162 | + `${outFile} should equal ${expectedOutSnap}` |
| 163 | + ) |
| 164 | + }, cliRunTestTimeout) |
| 165 | +}) |
0 commit comments