Skip to content

Commit

Permalink
fix: Fixes broken file-coverage links (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
davelosert committed Jun 13, 2023
1 parent 148463f commit 25ca77f
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 38 deletions.
51 changes: 26 additions & 25 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
name: 'Build and Test'
on:
name: "Build and Test"
on:
pull_request:


jobs:
build-and-test:
permissions:
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: 'Install Node'
uses: actions/setup-node@v2
with:
node-version: '16.x'
- name: 'Install Deps'
run: npm install
- name: 'Build'
run: npm run build
- name: 'Test'
run: npm run test:coverage
# Remove node_modules to see if this action runs entirely compiled
- name: 'Remove Node Modules'
run: rm -rf node_modules
- name: 'Test Working Directory Option'
uses: ./
with:
working-directory: './test/mockReports'
- name: 'Test Default Action'
# run step also on failure of the previous step
if: always()
uses: ./
- uses: actions/checkout@v3
- name: "Install Node"
uses: actions/setup-node@v2
with:
node-version: "16.x"
- name: "Install Deps"
run: npm install
- name: "Build"
run: npm run build
- name: "Test"
run: npm run test:coverage
# Remove node_modules to see if this action runs entirely compiled
- name: "Remove Node Modules"
run: rm -rf node_modules
- name: "Test Working Directory Option"
uses: ./
with:
working-directory: "./test/mockReports"
- name: "Test Default Action"
# run step also on failure of the previous step
if: always()
uses: ./
with:
file-coverage-mode: "all"
71 changes: 63 additions & 8 deletions src/generateFileCoverageHtml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ describe('generateFileCoverageHtml()', () => {
pullChanges: []
});

const firstTableLine = getTableLine(2, html);
const tableLine = getTableLine(2, html);

expect(firstTableLine).toContain('70%');
expect(firstTableLine).toContain('80%');
expect(firstTableLine).toContain('90%');
expect(firstTableLine).toContain('100%');
expect(tableLine).toContain('70%');
expect(tableLine).toContain('80%');
expect(tableLine).toContain('90%');
expect(tableLine).toContain('100%');
});

it('renders the line-coverage in the same row as the coverage.', async (): Promise<void> => {
Expand All @@ -123,9 +123,64 @@ describe('generateFileCoverageHtml()', () => {
pullChanges: []
});

const firstTableLine = getTableLine(2, html);
const tableLine = getTableLine(2, html);

expect(firstTableLine).toContain('70%');
expect(firstTableLine).toContain('1-2');
expect(tableLine).toContain('70%');
expect(tableLine).toContain('1-2');
expect(tableLine).toContain('#L1-L2');
});

it('renders single line coverage without range.', async (): Promise<void> => {
const jsonSummary: JsonSummary = createMockJsonSummary({
'src/exampleFile.ts': createMockCoverageReport({
statements: createMockReportNumbers({ pct: 70, }),
}),
});
const jsonFinal: JsonFinal = {
...createJsonFinalEntry('src/exampleFile.ts', [
{ line: 2, covered: false }
]),
};

const html = generateFileCoverageHtml({
jsonSummary,
jsonFinal,
fileCoverageMode: FileCoverageMode.All,
pullChanges: []
});

const tableLine = getTableLine(2, html);

expect(tableLine).toContain('2');
expect(tableLine).toContain('#L2');
});

it('renders non adjacent line coverage with multiple links.', async (): Promise<void> => {
const jsonSummary: JsonSummary = createMockJsonSummary({
'src/exampleFile.ts': createMockCoverageReport({
statements: createMockReportNumbers({ pct: 70, }),
}),
});
const jsonFinal: JsonFinal = {
...createJsonFinalEntry('src/exampleFile.ts', [
{ line: 2, covered: false },
{ line: 3, covered: true },
{ line: 4, covered: true },
{ line: 5, covered: false },
{ line: 6, covered: false }
]),
};

const html = generateFileCoverageHtml({
jsonSummary,
jsonFinal,
fileCoverageMode: FileCoverageMode.All,
pullChanges: []
});

const tableLine = getTableLine(2, html);

expect(tableLine).toContain('#L2');
expect(tableLine).toContain('#L5-L6');
});
});
10 changes: 5 additions & 5 deletions src/generateFileCoverageHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ function formatGroupLine (caption: string): string {

function createRangeURLs(uncoveredLines: LineRange[], url: string): string {
return uncoveredLines.map((range) => {
let end = '';
let endUrl = '';
let linkText = `${range.start}`;
let urlHash = `#L${range.start}`;

if (range.start !== range.end) {
end = `-${range.end}`;
endUrl = `-L${range.end}`;
linkText += `-${range.end}`;
urlHash += `-L${range.end}`;
}

return `<a href="${url}${endUrl}" class="text-red">${range.start}${end}</a>`;
return `<a href="${url}${urlHash}" class="text-red">${linkText}</a>`;
})
.join(', ');
}
Expand Down

0 comments on commit 25ca77f

Please sign in to comment.