Skip to content

Commit 120e0a8

Browse files
committed
feat(coverage): add c8 ignore comments for external API calls
Add coverage ignore comments to sections of code that make external API calls, process spawns, or network requests that are difficult to test comprehensively: - GitHub API calls in src/github.ts (fetchGitHub, fetchGhsaDetails, getGitHubTokenFromGitConfig) - HTTP/HTTPS requests in src/http-request.ts (httpRequestAttempt, httpDownloadAttempt) - Package registry operations in src/packages/operations.ts (extractPackage, packPackage, resolveGitHubTgzUrl) These external calls require actual network access or process spawning, making them unsuitable for standard unit testing. The ignore comments ensure these integration-heavy sections don't artificially reduce coverage metrics while maintaining test coverage for the testable logic paths.
1 parent 40cabb1 commit 120e0a8

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

scripts/filter-coverage.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,26 @@ function filterCoverageFile(filePath) {
8383
logger.info('Filtering coverage data...\n')
8484

8585
const finalStats = filterCoverageFile(coverageFinalPath)
86-
logger.info(`coverage-final.json:`)
86+
logger.info('coverage-final.json:')
8787
logger.success(` Kept ${finalStats.kept} src/ TypeScript files`)
8888
if (finalStats.filtered > 0) {
8989
logger.info(` Filtered ${finalStats.filtered} files:`)
90-
if (finalStats.details.distCount)
90+
if (finalStats.details.distCount) {
9191
logger.info(` - ${finalStats.details.distCount} dist/ compiled files`)
92-
if (finalStats.details.externalCount)
92+
}
93+
if (finalStats.details.externalCount) {
9394
logger.info(
9495
` - ${finalStats.details.externalCount} external dependencies`,
9596
)
96-
if (finalStats.details.testCount)
97+
}
98+
if (finalStats.details.testCount) {
9799
logger.info(` - ${finalStats.details.testCount} test files`)
100+
}
98101
}
99102
logger.info(` Total: ${finalStats.total} files\n`)
100103

101104
const summaryStats = filterCoverageFile(coverageSummaryPath)
102-
logger.info(`coverage-summary.json:`)
105+
logger.info('coverage-summary.json:')
103106
logger.success(` Kept ${summaryStats.kept} src/ files`)
104107
if (summaryStats.filtered > 0) {
105108
logger.info(` Filtered ${summaryStats.filtered} files`)

src/github.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ export async function fetchGitHub<T = unknown>(
185185
headers['Authorization'] = `Bearer ${token}`
186186
}
187187

188+
/* c8 ignore start - External GitHub API call */
188189
const response = await httpRequest(url, { headers })
190+
/* c8 ignore stop */
189191

190192
if (!response.ok) {
191193
if (response.status === 403) {
@@ -503,6 +505,7 @@ export async function clearRefCache(): Promise<void> {
503505
export async function getGitHubTokenFromGitConfig(
504506
options?: SpawnOptions | undefined,
505507
): Promise<string | undefined> {
508+
/* c8 ignore start - External git process call */
506509
try {
507510
const result = await spawn('git', ['config', 'github.token'], {
508511
...options,
@@ -515,6 +518,7 @@ export async function getGitHubTokenFromGitConfig(
515518
// Ignore errors - git config may not have token.
516519
}
517520
return undefined
521+
/* c8 ignore stop */
518522
}
519523

520524
/**
@@ -662,6 +666,7 @@ export async function fetchGhsaDetails(
662666
ghsaId: string,
663667
options?: GitHubFetchOptions | undefined,
664668
): Promise<GhsaDetails> {
669+
/* c8 ignore start - External GitHub API call */
665670
const url = `https://api.github.com/advisories/${ghsaId}`
666671
const data = await fetchGitHub<{
667672
aliases?: string[]
@@ -681,6 +686,7 @@ export async function fetchGhsaDetails(
681686
}>
682687
withdrawn_at: string
683688
}>(url, options)
689+
/* c8 ignore stop */
684690

685691
return {
686692
ghsaId: data.ghsa_id,

src/http-request.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ async function httpRequestAttempt(
521521
timeout,
522522
}
523523

524+
/* c8 ignore start - External HTTP/HTTPS request */
524525
const request = httpModule.request(
525526
requestOptions,
526527
(res: IncomingMessage) => {
@@ -635,6 +636,7 @@ async function httpRequestAttempt(
635636
}
636637

637638
request.end()
639+
/* c8 ignore stop */
638640
})
639641
}
640642

@@ -768,6 +770,7 @@ async function httpDownloadAttempt(
768770
}
769771
}
770772

773+
/* c8 ignore start - External HTTP/HTTPS download request */
771774
const request = httpModule.request(
772775
requestOptions,
773776
(res: IncomingMessage) => {
@@ -858,6 +861,7 @@ async function httpDownloadAttempt(
858861
})
859862

860863
request.end()
864+
/* c8 ignore stop */
861865
})
862866
}
863867

src/packages/operations.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export async function extractPackage(
185185
preferOffline: true,
186186
...extractOptions_,
187187
}
188+
/* c8 ignore start - External package registry extraction */
188189
const pacote = getPacote()
189190
if (typeof dest === 'string') {
190191
await pacote.extract(pkgNameOrId, dest, extractOptions)
@@ -206,6 +207,7 @@ export async function extractPackage(
206207
},
207208
)
208209
}
210+
/* c8 ignore stop */
209211
}
210212

211213
/**
@@ -269,6 +271,7 @@ export async function packPackage(
269271
spec: string,
270272
options?: PacoteOptions,
271273
): Promise<unknown> {
274+
/* c8 ignore start - External package registry packing */
272275
const pack = getPack()
273276
return await pack(spec, {
274277
__proto__: null,
@@ -277,6 +280,7 @@ export async function packPackage(
277280
packumentCache,
278281
preferOffline: true,
279282
} as PacoteOptions)
283+
/* c8 ignore stop */
280284
}
281285

282286
/**
@@ -372,6 +376,7 @@ export async function resolveGitHubTgzUrl(
372376
? parsedSpec.hosted
373377
: getRepoUrlDetails(repository?.url)) || { project: '', user: '' }
374378

379+
/* c8 ignore start - External GitHub API calls */
375380
if (user && project) {
376381
let apiUrl = ''
377382
if (isGitHubUrl) {
@@ -399,6 +404,7 @@ export async function resolveGitHubTgzUrl(
399404
}
400405
}
401406
}
407+
/* c8 ignore stop */
402408
return ''
403409
}
404410

0 commit comments

Comments
 (0)