Skip to content

Commit 0bbaf3f

Browse files
committed
fix: improve e2e test diagnostics for reachability errors
1 parent b61aa9a commit 0bbaf3f

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

src/commands/scan/cmd-scan-reach.e2e.test.mts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,33 @@ function findReachabilityForGhsa(
226226
* Logs stdout and stderr to help diagnose test failures.
227227
*/
228228
function logCommandOutput(code: number, stdout: string, stderr: string): void {
229-
logger.error(`Command failed with code ${code}`)
229+
logger.error(`Command exited with code ${code}`)
230230
logger.error('stdout:', stdout)
231231
logger.error('stderr:', stderr)
232232
}
233233

234+
/**
235+
* Log reachability entries that have type "error" for debugging.
236+
* Helps diagnose Coana analysis failures in CI.
237+
*/
238+
function logReachabilityErrors(facts: SocketFactsJson): void {
239+
for (const component of facts.components) {
240+
if (!component.reachability) {
241+
continue
242+
}
243+
for (const ghsaEntry of component.reachability) {
244+
for (const entry of ghsaEntry.reachability) {
245+
if (entry.type === 'error') {
246+
logger.error(
247+
`Reachability error for ${ghsaEntry.ghsa_id} in ${component.name}@${component.version} ` +
248+
`(subproject: ${entry.subprojectPath}): ${JSON.stringify(entry)}`,
249+
)
250+
}
251+
}
252+
}
253+
}
254+
}
255+
234256
describe('socket scan reach (E2E tests)', async () => {
235257
const { binCliPath } = constants
236258
// Standard timeout for most tests.
@@ -386,8 +408,13 @@ describe('socket scan reach (E2E tests)', async () => {
386408

387409
logger.info('\nReachability analysis completed successfully')
388410
} catch (e) {
389-
if (code !== 0) {
390-
logCommandOutput(code, stdout, stderr)
411+
logCommandOutput(code, stdout, stderr)
412+
// Log reachability errors from the facts file if it was parsed.
413+
try {
414+
const errorFacts = await readSocketFactsJson(tempFixture.path)
415+
logReachabilityErrors(errorFacts)
416+
} catch {
417+
// Facts file may not exist if the failure was earlier.
391418
}
392419
throw e
393420
} finally {
@@ -483,9 +510,7 @@ describe('socket scan reach (E2E tests)', async () => {
483510
'\nReachability analysis with excluded paths completed successfully',
484511
)
485512
} catch (e) {
486-
if (code !== 0) {
487-
logCommandOutput(code, stdout, stderr)
488-
}
513+
logCommandOutput(code, stdout, stderr)
489514
throw e
490515
} finally {
491516
await tempFixture.cleanup()
@@ -595,9 +620,7 @@ describe('socket scan reach (E2E tests)', async () => {
595620
'\nReachability analysis with target restriction completed successfully',
596621
)
597622
} catch (e) {
598-
if (code !== 0) {
599-
logCommandOutput(code, stdout, stderr)
600-
}
623+
logCommandOutput(code, stdout, stderr)
601624
throw e
602625
} finally {
603626
await tempFixture.cleanup()
@@ -673,9 +696,7 @@ describe('socket scan reach (E2E tests)', async () => {
673696
'\nReachability analysis with --cwd flag completed successfully',
674697
)
675698
} catch (e) {
676-
if (code !== 0) {
677-
logCommandOutput(code, stdout, stderr)
678-
}
699+
logCommandOutput(code, stdout, stderr)
679700
throw e
680701
} finally {
681702
await tempFixture.cleanup()
@@ -771,9 +792,7 @@ describe('socket scan reach (E2E tests)', async () => {
771792
'\nReachability analysis with --cwd and target completed successfully',
772793
)
773794
} catch (e) {
774-
if (code !== 0) {
775-
logCommandOutput(code, stdout, stderr)
776-
}
795+
logCommandOutput(code, stdout, stderr)
777796
throw e
778797
} finally {
779798
await tempFixture.cleanup()
@@ -915,9 +934,7 @@ describe('socket scan reach (E2E tests)', async () => {
915934
'\nReachability analysis output location verified successfully',
916935
)
917936
} catch (e) {
918-
if (code !== 0) {
919-
logCommandOutput(code, stdout, stderr)
920-
}
937+
logCommandOutput(code, stdout, stderr)
921938
throw e
922939
} finally {
923940
await tempFixture.cleanup()
@@ -1019,9 +1036,7 @@ describe('socket scan reach (E2E tests)', async () => {
10191036
'\nReachability analysis with pypi ecosystem filter completed successfully',
10201037
)
10211038
} catch (e) {
1022-
if (code !== 0) {
1023-
logCommandOutput(code, stdout, stderr)
1024-
}
1039+
logCommandOutput(code, stdout, stderr)
10251040
throw e
10261041
} finally {
10271042
await tempFixture.cleanup()
@@ -1119,9 +1134,7 @@ describe('socket scan reach (E2E tests)', async () => {
11191134
'\nReachability analysis with npm ecosystem filter completed successfully',
11201135
)
11211136
} catch (e) {
1122-
if (code !== 0) {
1123-
logCommandOutput(code, stdout, stderr)
1124-
}
1137+
logCommandOutput(code, stdout, stderr)
11251138
throw e
11261139
} finally {
11271140
await tempFixture.cleanup()

0 commit comments

Comments
 (0)