Skip to content

Commit

Permalink
test: use json stream parser in existing test
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSchafer committed Mar 21, 2024
1 parent 2310994 commit 39189d4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@
"depcheck": "^1.4.3",
"eslint": "6.8.0",
"eslint-config-prettier": "^6.1.0",
"jest-junit": "^16.0.0",
"eslint-plugin-anti-trojan-source": "^1.1.1",
"eslint-plugin-jest": "^24.4.0",
"express": "^4.17.1",
"fs-extra": "^9.1.0",
"jest": "^28.1.3",
"jest-junit": "^16.0.0",
"jsonparse": "^1.3.1",
"lodash": "^4.17.20",
"mock-fs": "^4.13.0",
"node-loader": "^2.0.0",
Expand Down
34 changes: 12 additions & 22 deletions test/jest/acceptance/cli-json-output.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,23 @@ describe('test --json', () => {
},
);

// check for these properties in the buffer chunks
const pathString = `"path": "${imageName}"`;
if (stderrBuffer && stderrBuffer.length > 0)
console.log(stderrBuffer?.toString('utf8'));

let hasExpectedPathString = false;
const vulnerabilitiesString = `"vulnerabilities": [`;
let hasExpectedVulnerabilitiesString = false;

const chunkSize = 1024 * 1024 * 450; // 450 MB
if (stdoutBuffer) {
for (let i = 0; i < stdoutBuffer.length; i += chunkSize) {
const chunk = stdoutBuffer.slice(
i,
Math.min(i + chunkSize, stdoutBuffer.length),
);

if (!hasExpectedVulnerabilitiesString) {
hasExpectedVulnerabilitiesString = chunk
.toString('utf8')
.includes(vulnerabilitiesString);
}

if (!hasExpectedPathString) {
hasExpectedPathString = chunk.toString('utf8').includes(pathString);
}
const Parser = require('jsonparse');
const p = new Parser();
p.onValue = function(value) {
if (this.key === 'path' && value === imageName) {
hasExpectedPathString = true;
} else if (this.key === 'vulnerabilities') {
hasExpectedVulnerabilitiesString = true;
}
}
};

if (stderrBuffer) console.log(stderrBuffer.toString('utf8'));
p.write(stdoutBuffer);

expect(code).toEqual(1);
expect(hasExpectedVulnerabilitiesString).toBeTruthy();
Expand Down

0 comments on commit 39189d4

Please sign in to comment.