Skip to content

Commit

Permalink
Merge pull request #13 from aGallea/cobertura-file-source
Browse files Browse the repository at this point in the history
  • Loading branch information
aGallea committed Apr 18, 2023
2 parents aff92c9 + ddc9978 commit 2f87255
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
19 changes: 11 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ const getEventInfo = () => {
commitSha: '',
headRef: '',
baseRef: '',
pwd: process.env.GITHUB_WORKSPACE || '',
};
if (github_1.context.eventName === 'pull_request' && github_1.context.payload) {
eventInfo.commitSha = github_1.context.payload.pull_request?.head.sha;
Expand Down Expand Up @@ -489,7 +490,7 @@ const main = async () => {
const eventInfo = (0, eventInfo_1.getEventInfo)();
const coverageInfo = {
cobertura: eventInfo.diffcoverRef === 'cobertura'
? await (0, cobertura_1.parseFile)(eventInfo.coberturaPath)
? await (0, cobertura_1.parseFile)(eventInfo.coberturaPath, `${eventInfo.pwd}/`)
: [],
clover: eventInfo.diffcoverRef === 'clover'
? await (0, clover_1.parseFile)(eventInfo.cloverPath)
Expand Down Expand Up @@ -728,6 +729,7 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.parseFile = void 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
const fs_1 = __importDefault(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const xml2js_1 = __importDefault(__nccwpck_require__(6189));
const core = __importStar(__nccwpck_require__(2186));
const classesFromPackages = (packages) => {
Expand Down Expand Up @@ -773,15 +775,16 @@ const extractLcovStyleBranches = (c) => {
}
return branches;
};
const unpackage = (coverage) => {
const unpackage = (coverage, pwd) => {
const packages = coverage.packages;
// const source = coverage.sources[0].source[0];
const source = coverage.sources[0].source[0];
const classes = classesFromPackages(packages);
return classes.map((c) => {
const branches = extractLcovStyleBranches(c);
const classCov = {
title: c.$.name,
file: c.$.filename,
// file: c.$.filename,
file: path_1.default.join(source, c.$.filename).replace(pwd, ''),
functions: {
found: c.methods && c.methods[0].method ? c.methods[0].method.length : 0,
hit: 0,
Expand Down Expand Up @@ -824,7 +827,7 @@ const unpackage = (coverage) => {
return classCov;
});
};
const parseContent = (xml) => {
const parseContent = (xml, pwd) => {
return new Promise((resolve, reject) => {
xml2js_1.default.parseString(xml, (err, parseResult) => {
if (err) {
Expand All @@ -833,12 +836,12 @@ const parseContent = (xml) => {
if (!parseResult?.coverage) {
return reject(new Error('invalid or missing xml content'));
}
const result = unpackage(parseResult.coverage);
const result = unpackage(parseResult.coverage, pwd);
resolve(result);
});
});
};
const parseFile = async (file) => {
const parseFile = async (file, pwd) => {
return new Promise((resolve, reject) => {
if (!file || file === '') {
core.info('no file specified');
Expand All @@ -852,7 +855,7 @@ const parseFile = async (file) => {
}
else {
try {
const info = await parseContent(data);
const info = await parseContent(data, pwd);
// console.log('====== cobertura ======');
// console.log(JSON.stringify(info, null, 2));
resolve(info);
Expand Down
1 change: 1 addition & 0 deletions src/eventInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const getEventInfo = (): EventInfo => {
commitSha: '',
headRef: '',
baseRef: '',
pwd: process.env.GITHUB_WORKSPACE || '',
};
if (context.eventName === 'pull_request' && context.payload) {
eventInfo.commitSha = context.payload.pull_request?.head.sha;
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const main = async (): Promise<void> => {
const coverageInfo: CoverageTypeInfo = {
cobertura:
eventInfo.diffcoverRef === 'cobertura'
? await parseCoberturaFile(eventInfo.coberturaPath)
? await parseCoberturaFile(eventInfo.coberturaPath, `${eventInfo.pwd}/`)
: [],
clover:
eventInfo.diffcoverRef === 'clover'
Expand Down
16 changes: 9 additions & 7 deletions src/parsers/cobertura.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import fs from 'fs';
import path from 'path';
import { CoverInfo, CoverInfoBranchesDetails } from '../types';
import parseString from 'xml2js';
import * as core from '@actions/core';
Expand Down Expand Up @@ -55,16 +56,17 @@ const extractLcovStyleBranches = (c: any) => {
return branches;
};

const unpackage = (coverage: any): CoverInfo[] => {
const unpackage = (coverage: any, pwd: string): CoverInfo[] => {
const packages = coverage.packages;
// const source = coverage.sources[0].source[0];
const source = coverage.sources[0].source[0];

const classes = classesFromPackages(packages);
return classes.map((c) => {
const branches = extractLcovStyleBranches(c);
const classCov: CoverInfo = {
title: c.$.name,
file: c.$.filename,
// file: c.$.filename,
file: path.join(source, c.$.filename).replace(pwd, ''),
functions: {
found: c.methods && c.methods[0].method ? c.methods[0].method.length : 0,
hit: 0,
Expand Down Expand Up @@ -113,7 +115,7 @@ const unpackage = (coverage: any): CoverInfo[] => {
});
};

const parseContent = (xml: string): Promise<CoverInfo[]> => {
const parseContent = (xml: string, pwd: string): Promise<CoverInfo[]> => {
return new Promise((resolve, reject) => {
parseString.parseString(xml, (err, parseResult) => {
if (err) {
Expand All @@ -122,13 +124,13 @@ const parseContent = (xml: string): Promise<CoverInfo[]> => {
if (!parseResult?.coverage) {
return reject(new Error('invalid or missing xml content'));
}
const result = unpackage(parseResult.coverage);
const result = unpackage(parseResult.coverage, pwd);
resolve(result);
});
});
};

export const parseFile = async (file: string): Promise<CoverInfo[]> => {
export const parseFile = async (file: string, pwd: string): Promise<CoverInfo[]> => {
return new Promise((resolve, reject) => {
if (!file || file === '') {
core.info('no file specified');
Expand All @@ -143,7 +145,7 @@ export const parseFile = async (file: string): Promise<CoverInfo[]> => {
reject(err);
} else {
try {
const info = await parseContent(data);
const info = await parseContent(data, pwd);
// console.log('====== cobertura ======');
// console.log(JSON.stringify(info, null, 2));
resolve(info);
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface EventInfo {
commitSha: string;
headRef: string;
baseRef: string;
pwd: string;
filesStatus?: FilesStatus;
}

Expand Down
25 changes: 20 additions & 5 deletions test/diffCover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ describe('diffCover tests', () => {
describe('Empty responses', () => {
test('showDiffcover false', async () => {
const eventInfo: EventInfo = getEventInfo();
const cobertura = await parseFile('./test/assets/cobertura-coverage.xml');
const cobertura = await parseFile(
'./test/assets/cobertura-coverage.xml',
'/Users/user/workspace/private/tests-coverage-report/',
);
const coverageInfo: CoverageTypeInfo = {
cobertura,
clover: [],
Expand All @@ -55,7 +58,10 @@ describe('diffCover tests', () => {
const eventInfo: EventInfo = getEventInfo();
eventInfo.showDiffcover = true;
eventInfo.diffcoverRef = 'cobertura';
const cobertura = await parseFile('./test/assets/cobertura-coverage.xml');
const cobertura = await parseFile(
'./test/assets/cobertura-coverage.xml',
'/Users/user/workspace/private/tests-coverage-report/',
);
const coverageInfo: CoverageTypeInfo = {
cobertura,
clover: [],
Expand All @@ -77,7 +83,10 @@ describe('diffCover tests', () => {
const eventInfo: EventInfo = getEventInfo();
eventInfo.showDiffcover = true;
eventInfo.diffcoverRef = 'cobertura';
const cobertura = await parseFile('./test/assets/cobertura-coverage.xml');
const cobertura = await parseFile(
'./test/assets/cobertura-coverage.xml',
'/Users/user/workspace/private/tests-coverage-report/',
);
const coverageInfo: CoverageTypeInfo = {
cobertura,
clover: [],
Expand Down Expand Up @@ -117,7 +126,10 @@ describe('diffCover tests', () => {
const eventInfo: EventInfo = getEventInfo();
eventInfo.showDiffcover = true;
eventInfo.diffcoverRef = 'cobertura';
const cobertura = await parseFile('./test/assets/cobertura-coverage.xml');
const cobertura = await parseFile(
'./test/assets/cobertura-coverage.xml',
'/Users/user/workspace/private/tests-coverage-report/',
);
const coverageInfo: CoverageTypeInfo = {
cobertura,
clover: [],
Expand Down Expand Up @@ -238,7 +250,10 @@ describe('diffCover tests', () => {
});
eventInfo.showDiffcover = true;
eventInfo.diffcoverRef = 'cobertura';
const cobertura = await parseFile('./test/assets/cobertura-coverage.xml');
const cobertura = await parseFile(
'./test/assets/cobertura-coverage.xml',
'/Users/user/workspace/private/tests-coverage-report/',
);
const coverageInfo: CoverageTypeInfo = {
cobertura,
clover: [],
Expand Down
13 changes: 8 additions & 5 deletions test/parsers/cobertura.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@ import { parseFile } from '../../src/parsers/cobertura';

describe('cobertura parser tests', () => {
test('No such file', async () => {
await expect(parseFile('invalid.file')).rejects.toThrow(
await expect(parseFile('invalid.file', '')).rejects.toThrow(
`ENOENT: no such file or directory, open 'invalid.file'`,
);
});
test('Filename empty string', async () => {
await expect(parseFile('')).resolves.toEqual([]);
await expect(parseFile('', '')).resolves.toEqual([]);
});
test('Invalid xml content', async () => {
await expect(parseFile('./test/assets/invalid.xml')).rejects.toThrow(
await expect(parseFile('./test/assets/invalid.xml', '')).rejects.toThrow(
'invalid or missing xml content',
);
});
test('error xml content', async () => {
await expect(parseFile('./test/assets/invalidXmlContent.xml')).rejects.toThrow(
await expect(parseFile('./test/assets/invalidXmlContent.xml', '')).rejects.toThrow(
'Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar: s',
);
});

test('Parse', async () => {
const parsed = await parseFile('./test/assets/cobertura-coverage.xml');
const parsed = await parseFile(
'./test/assets/cobertura-coverage.xml',
'/Users/user/workspace/private/tests-coverage-report/',
);
expect(parsed).toHaveLength(12);
expect(parsed[0].file).toEqual('src/changedFiles.ts');
expect(parsed[0].functions.found).toEqual(1);
Expand Down

0 comments on commit 2f87255

Please sign in to comment.