Repository navigation
test(coverage): improve coverage and generate badge #1799
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
Draft
John-David Dalton (jdalton)
wants to merge
1
commit into
v1.x
Choose a base branch
from
test/coverage-99
base: v1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,535
−10
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { readFile, writeFile } from 'node:fs/promises' | ||
| import path from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
|
|
||
| import { isMainModule } from './lib/is-main-module.mts' | ||
|
|
||
| type CoverageSummary = { | ||
| total?: { | ||
| lines?: { | ||
| pct?: number | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const BADGE_MARKER = '<!-- coverage-badge:start -->' | ||
| const BADGE_END_MARKER = '<!-- coverage-badge:end -->' | ||
|
|
||
| export function formatCoverageBadge(percentage: number): string { | ||
| if (!Number.isFinite(percentage) || percentage < 0 || percentage > 100) { | ||
| throw new RangeError(`Coverage percentage must be between 0 and 100`) | ||
| } | ||
|
|
||
| const rounded = Math.round(percentage) | ||
| const color = rounded >= 90 ? 'brightgreen' : rounded >= 80 ? 'yellow' : 'red' | ||
| return `[](https://github.com/SocketDev/socket-cli/actions)` | ||
| } | ||
|
|
||
| export async function updateCoverageBadge({ | ||
| readmePath, | ||
| summaryPath, | ||
| }: { | ||
| readmePath: string | ||
| summaryPath: string | ||
| }): Promise<void> { | ||
| const summary = JSON.parse( | ||
| await readFile(summaryPath, 'utf8'), | ||
| ) as CoverageSummary | ||
| const percentage = summary.total?.lines?.pct | ||
| if (typeof percentage !== 'number' || !Number.isFinite(percentage)) { | ||
| throw new Error(`Coverage summary is missing total line coverage`) | ||
| } | ||
|
|
||
| const readme = await readFile(readmePath, 'utf8') | ||
| const start = readme.indexOf(BADGE_MARKER) | ||
| const end = readme.indexOf(BADGE_END_MARKER) | ||
| if (start === -1 || end < start) { | ||
| throw new Error(`README is missing the coverage badge markers`) | ||
| } | ||
|
|
||
| const badgeStart = start + BADGE_MARKER.length | ||
| const updated = `${readme.slice(0, badgeStart)}\n${formatCoverageBadge(percentage)}\n${readme.slice(end)}` | ||
| await writeFile(readmePath, updated) | ||
| } | ||
|
|
||
| const repositoryRoot = path.dirname( | ||
| path.dirname(fileURLToPath(import.meta.url)), | ||
| ) | ||
|
|
||
| export function isCoverageBadgeEntrypoint(entryPath?: string): boolean { | ||
| return isMainModule(import.meta.url, entryPath) | ||
| } | ||
|
|
||
| if (isCoverageBadgeEntrypoint()) { | ||
| await updateCoverageBadge({ | ||
| readmePath: path.join(repositoryRoot, 'README.md'), | ||
| summaryPath: path.join(repositoryRoot, 'coverage', 'coverage-summary.json'), | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { promises as fs } from 'node:fs' | ||
| import os from 'node:os' | ||
| import path from 'node:path' | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { loggerFail, loggerLog, loggerWarn, resolveBuildToolBin, spawn } = | ||
| vi.hoisted(() => ({ | ||
| loggerFail: vi.fn(), | ||
| loggerLog: vi.fn(), | ||
| loggerWarn: vi.fn(), | ||
| resolveBuildToolBin: vi.fn( | ||
| (_tool: string, _cwd: string, bin: string) => bin, | ||
| ), | ||
| spawn: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('./scripts/build-tool.mts', () => ({ resolveBuildToolBin })) | ||
| vi.mock('../../sea/runtime.mts', () => ({ isSea: () => false })) | ||
| vi.mock('../../utils/spawn.mts', () => ({ spawn })) | ||
| vi.mock('../../utils/logger.mts', () => ({ | ||
| logger: { | ||
| error: vi.fn(), | ||
| fail: loggerFail, | ||
| group: vi.fn(), | ||
| groupEnd: vi.fn(), | ||
| info: vi.fn(), | ||
| log: loggerLog, | ||
| success: vi.fn(), | ||
| warn: loggerWarn, | ||
| }, | ||
| })) | ||
|
|
||
| import { convertGradleToMaven } from './convert_gradle_to_maven.mts' | ||
|
|
||
| describe('Gradle POM conversion', () => { | ||
| let cwd: string | ||
|
|
||
| beforeEach(async () => { | ||
| cwd = await fs.mkdtemp(path.join(os.tmpdir(), 'socket-gradle-pom-')) | ||
| await fs.writeFile(path.join(cwd, 'gradlew'), '') | ||
| resolveBuildToolBin.mockClear() | ||
| spawn.mockReset() | ||
| loggerFail.mockClear() | ||
| loggerLog.mockClear() | ||
| loggerWarn.mockClear() | ||
| process.exitCode = 0 | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| process.exitCode = 0 | ||
| await fs.rm(cwd, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| it('passes exclusions to Gradle and reports each generated POM', async () => { | ||
| spawn.mockResolvedValue({ | ||
| code: 0, | ||
| stdout: 'POM file copied to: service/pom.xml\n', | ||
| stderr: '', | ||
| }) | ||
|
|
||
| await convertGradleToMaven({ | ||
| bin: './gradlew', | ||
| cwd, | ||
| excludePaths: [':docs', ':examples'], | ||
| gradleOpts: ['--offline'], | ||
| verbose: false, | ||
| }) | ||
|
|
||
| expect(spawn).toHaveBeenCalledWith( | ||
| path.join(cwd, 'gradlew'), | ||
| expect.arrayContaining([ | ||
| '-Psocket.excludePaths=:docs,:examples', | ||
| '--offline', | ||
| 'pom', | ||
| ]), | ||
| { cwd }, | ||
| ) | ||
| expect(loggerLog).toHaveBeenCalledWith('- ', 'service/pom.xml') | ||
| }) | ||
|
|
||
| it('streams Gradle output directly when verbose mode is enabled', async () => { | ||
| spawn.mockResolvedValue({ code: 0, stdout: '', stderr: '' }) | ||
|
|
||
| await convertGradleToMaven({ | ||
| bin: './gradlew', | ||
| cwd, | ||
| gradleOpts: [], | ||
| verbose: true, | ||
| }) | ||
|
|
||
| expect(spawn).toHaveBeenCalledWith( | ||
| path.join(cwd, 'gradlew'), | ||
| expect.any(Array), | ||
| { cwd, stdio: 'inherit' }, | ||
| ) | ||
| }) | ||
|
|
||
| it('surfaces a failed Gradle exit and sets a nonzero process status', async () => { | ||
| spawn.mockResolvedValue({ | ||
| code: 17, | ||
| stdout: '', | ||
| stderr: 'configuration error', | ||
| }) | ||
|
|
||
| await convertGradleToMaven({ | ||
| bin: './gradlew', | ||
| cwd, | ||
| gradleOpts: [], | ||
| verbose: false, | ||
| }) | ||
|
|
||
| expect(process.exitCode).toBe(1) | ||
| expect(loggerFail).toHaveBeenCalledWith('Gradle exited with exit code 17') | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.