Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dangling commas in json results #361

Merged
merged 3 commits into from
Apr 12, 2024
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
6 changes: 3 additions & 3 deletions src/streaming/codeCoverageStringifyStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ export class CodeCoverageStringifyStream extends Transform {
const { coverage, ...theRest } = perClassCoverage;
// stringify all properties except coverage and strip off the closing '}'
this.push(JSON.stringify(theRest).slice(0, -1));
this.push(',"coverage": {');
this.push('"coveredLines": [');
this.push(',"coverage":{');
this.push('"coveredLines":[');
pushArrayToStream(coverage.coveredLines ?? [], this);
this.push('],"uncoveredLines": [');
this.push('],"uncoveredLines":[');
pushArrayToStream(coverage.uncoveredLines ?? [], this);
this.push(']}}');

Expand Down
16 changes: 8 additions & 8 deletions src/streaming/testResultStringifyStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class TestResultStringifyStream extends Readable {
// outer curly
this.push('{');
// summary
this.push(`"summary": ${JSON.stringify(summary)},`);
this.push(`"summary":${JSON.stringify(summary)},`);

this.buildTests();
this.buildCodeCoverage();
Expand All @@ -47,19 +47,19 @@ export class TestResultStringifyStream extends Readable {
const numberOfTests = this.testResult.tests.length - 1;
this.testResult.tests.forEach((test, index) => {
const { perClassCoverage, ...testRest } = test;
this.push(`${JSON.stringify(testRest).slice(0, -1)},`);
this.push(`${JSON.stringify(testRest).slice(0, -1)}`);
if (perClassCoverage) {
const numberOfPerClassCoverage = perClassCoverage.length - 1;
this.push('"perClassCoverage": [');
this.push(',"perClassCoverage":[');
perClassCoverage.forEach((pcc, index) => {
const { coverage, ...coverageRest } = pcc;
this.push(`${JSON.stringify(coverageRest).slice(0, -1)},`);
this.push(`"coverage": ${JSON.stringify(coverage)}}`);
this.push(`${JSON.stringify(coverageRest).slice(0, -1)}`);
this.push(`,"coverage":${JSON.stringify(coverage)}}`);
if (numberOfPerClassCoverage !== index) {
this.push(',');
}
});
this.push('],');
this.push(']');
}
// close the tests
this.push('}');
Expand All @@ -78,8 +78,8 @@ export class TestResultStringifyStream extends Readable {
const numberOfCodeCoverage = this.testResult.codecoverage.length - 1;
this.testResult.codecoverage.forEach((coverage, index) => {
const { coveredLines, uncoveredLines, ...theRest } = coverage;
this.push(`${JSON.stringify(theRest).slice(0, -1)},`);
this.push('"coveredLines":[');
this.push(`${JSON.stringify(theRest).slice(0, -1)}`);
this.push(',"coveredLines":[');
pushArrayToStream(coveredLines, this);
this.push('],"uncoveredLines":[');
pushArrayToStream(uncoveredLines, this);
Expand Down
82 changes: 82 additions & 0 deletions test/streaming/codeCoverageStringifyStream.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { expect } from 'chai';
import { CodeCoverageStringifyStream } from '../../src/streaming/codeCoverageStringifyStream';
import { PerClassCoverage } from '../../src/tests';

describe('CodeCoverageStringifyStream', () => {
let stream: CodeCoverageStringifyStream;
let data: PerClassCoverage[];

beforeEach(() => {
data = [
{
apexClassOrTriggerName: 'TestClass3',
apexClassOrTriggerId: '01p3h00000KoP4UAAV',
apexTestClassId: '01p3h00000KoP4VAAV',
apexTestMethodName: 'testMethod3',
numLinesCovered: 12,
numLinesUncovered: 3,
percentage: '80.00',
coverage: {
coveredLines: [26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37],
uncoveredLines: [38, 39, 40]
}
},
{
apexClassOrTriggerName: 'TestClass4',
apexClassOrTriggerId: '01p3h00000KoP4WAAV',
apexTestClassId: '01p3h00000KoP4XAAV',
apexTestMethodName: 'testMethod4',
numLinesCovered: 15,
numLinesUncovered: 0,
percentage: '100.00',
coverage: {
coveredLines: [
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55
],
uncoveredLines: []
}
}
];
stream = new CodeCoverageStringifyStream();
});

it('should transform data correctly', (done) => {
const expectedOutput = JSON.stringify([data]);

let output = '';
stream.on('data', (chunk: string) => {
output += chunk;
});

stream.on('end', () => {
expect(output).to.equal(expectedOutput);
done();
});

stream.write(data);
stream.end();
});

it('should handle empty data', (done) => {
const expectedOutput = '[[]]';

let output = '';
stream.on('data', (chunk: string) => {
output += chunk;
});

stream.on('end', () => {
expect(output).to.equal(expectedOutput);
done();
});

stream.write([]);
stream.end();
});
});
16 changes: 13 additions & 3 deletions test/streaming/jsonStringifyStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(json));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -43,15 +44,18 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(json));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});

it('should handle complex objects ending with a key/array', (done) => {
it('should handle complex objects and arrays without dangling commas', (done) => {
const json = {
key1: 'value1',
key2: { key3: 'value3' },
key4: ['value4', 'value5']
key2: { key3: 'value3', key5: ['value6', ['value7', 'value8', null]] },
key4: ['value4', 'value5', null],
// @ts-ignore
key6: { key7: 'value7', key8: null }
};
const stream = JSONStringifyStream.from(json);

Expand All @@ -62,6 +66,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(json));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -81,6 +86,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(json));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -96,6 +102,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(value));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -111,6 +118,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(value));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -126,6 +134,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(value));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand All @@ -141,6 +150,7 @@ describe('JSONStringifyStream', () => {

stream.on('end', () => {
expect(result).to.equal(JSON.stringify(value));
expect(() => JSON.parse(result)).to.not.throw();
done();
});
});
Expand Down
Loading
Loading