Skip to content

Commit aa2ffa7

Browse files
committed
better logging when assertions fail
1 parent 6bca8ed commit aa2ffa7

File tree

1 file changed

+82
-27
lines changed

1 file changed

+82
-27
lines changed

src/commands/fix/cmd-fix-e2e.test.mts

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ function compareVersions(v1: string, v2: string): number {
113113
return 0
114114
}
115115

116+
/**
117+
* Helper to log command output for debugging.
118+
* Logs stdout and stderr to help diagnose test failures.
119+
*/
120+
function logCommandOutput(
121+
code: number,
122+
stdout: string,
123+
stderr: string,
124+
): void {
125+
logger.error(`Command failed with code ${code}`)
126+
logger.error('stdout:', stdout)
127+
logger.error('stderr:', stderr)
128+
}
129+
116130
describe('socket fix (E2E tests)', async () => {
117131
const { binCliPath } = constants
118132
const testTimeout = 120_000
@@ -131,26 +145,26 @@ describe('socket fix (E2E tests)', async () => {
131145
'should fix all vulnerabilities in JavaScript project',
132146
async cmd => {
133147
const tempFixture = await createTempFixtureCopy('e2e-test-js')
148+
let stdout = ''
149+
let stderr = ''
150+
let code = -1
134151

135152
try {
136153
const beforePkg = await readPackageJson(tempFixture.path)
137154
const beforeLodashVersion = beforePkg.dependencies?.['lodash']
138155

139156
expect(beforeLodashVersion).toBe('4.17.20')
140157

141-
const { code, stderr, stdout } = await spawnSocketCli(
142-
binCliPath,
143-
cmd,
144-
{
145-
cwd: tempFixture.path,
146-
env: getTestEnv(apiToken),
147-
},
148-
)
158+
const result = await spawnSocketCli(binCliPath, cmd, {
159+
cwd: tempFixture.path,
160+
env: getTestEnv(apiToken),
161+
})
162+
stdout = result.stdout
163+
stderr = result.stderr
164+
code = result.code
149165

150166
if (code !== 0) {
151-
logger.error(`Command failed with code ${code}`)
152-
logger.error('stdout:', stdout)
153-
logger.error('stderr:', stderr)
167+
logCommandOutput(code, stdout, stderr)
154168
}
155169

156170
expect(code, 'should exit with code 0').toBe(0)
@@ -177,6 +191,11 @@ describe('socket fix (E2E tests)', async () => {
177191
logger.info(
178192
`\nSuccessfully upgraded lodash from ${beforeVersion} to ${afterVersion}`,
179193
)
194+
} catch (e) {
195+
if (code !== 0) {
196+
logCommandOutput(code, stdout, stderr)
197+
}
198+
throw e
180199
} finally {
181200
await tempFixture.cleanup()
182201
}
@@ -189,6 +208,9 @@ describe('socket fix (E2E tests)', async () => {
189208
'should fix vulnerabilities and write output file with fixes result',
190209
async cmd => {
191210
const tempFixture = await createTempFixtureCopy('e2e-test-js')
211+
let stdout = ''
212+
let stderr = ''
213+
let code = -1
192214

193215
try {
194216
const beforePkg = await readPackageJson(tempFixture.path)
@@ -201,15 +223,16 @@ describe('socket fix (E2E tests)', async () => {
201223
'socket-fix-output.json',
202224
)
203225

204-
const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd, {
226+
const result = await spawnSocketCli(binCliPath, cmd, {
205227
cwd: tempFixture.path,
206228
env: getTestEnv(apiToken),
207229
})
230+
stdout = result.stdout
231+
stderr = result.stderr
232+
code = result.code
208233

209234
if (code !== 0) {
210-
logger.error(`Command failed with code ${code}`)
211-
logger.error('stdout:', stdout)
212-
logger.error('stderr:', stderr)
235+
logCommandOutput(code, stdout, stderr)
213236
}
214237

215238
expect(code, 'should exit with code 0').toBe(0)
@@ -248,6 +271,11 @@ describe('socket fix (E2E tests)', async () => {
248271
logger.info(
249272
`\nSuccessfully upgraded lodash from ${beforeVersion} to ${afterVersion} and wrote output file`,
250273
)
274+
} catch (e) {
275+
if (code !== 0) {
276+
logCommandOutput(code, stdout, stderr)
277+
}
278+
throw e
251279
} finally {
252280
await tempFixture.cleanup()
253281
}
@@ -260,22 +288,26 @@ describe('socket fix (E2E tests)', async () => {
260288
'should fix specific GHSA vulnerability in JavaScript project',
261289
async cmd => {
262290
const tempFixture = await createTempFixtureCopy('e2e-test-js')
291+
let stdout = ''
292+
let stderr = ''
293+
let code = -1
263294

264295
try {
265296
const beforePkg = await readPackageJson(tempFixture.path)
266297
const beforeLodashVersion = beforePkg.dependencies?.['lodash']
267298

268299
expect(beforeLodashVersion).toBe('4.17.20')
269300

270-
const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd, {
301+
const result = await spawnSocketCli(binCliPath, cmd, {
271302
cwd: tempFixture.path,
272303
env: getTestEnv(apiToken),
273304
})
305+
stdout = result.stdout
306+
stderr = result.stderr
307+
code = result.code
274308

275309
if (code !== 0) {
276-
logger.error(`Command failed with code ${code}`)
277-
logger.error('stdout:', stdout)
278-
logger.error('stderr:', stderr)
310+
logCommandOutput(code, stdout, stderr)
279311
}
280312

281313
expect(code, 'should exit with code 0').toBe(0)
@@ -297,6 +329,11 @@ describe('socket fix (E2E tests)', async () => {
297329
logger.info(
298330
`\nSuccessfully fixed GHSA-35jh-r3h4-6jhm by upgrading lodash from ${beforeVersion} to ${afterVersion}`,
299331
)
332+
} catch (e) {
333+
if (code !== 0) {
334+
logCommandOutput(code, stdout, stderr)
335+
}
336+
throw e
300337
} finally {
301338
await tempFixture.cleanup()
302339
}
@@ -309,22 +346,26 @@ describe('socket fix (E2E tests)', async () => {
309346
'should convert CVE to GHSA and fix JavaScript project',
310347
async cmd => {
311348
const tempFixture = await createTempFixtureCopy('e2e-test-js')
349+
let stdout = ''
350+
let stderr = ''
351+
let code = -1
312352

313353
try {
314354
const beforePkg = await readPackageJson(tempFixture.path)
315355
const beforeLodashVersion = beforePkg.dependencies?.['lodash']
316356

317357
expect(beforeLodashVersion).toBe('4.17.20')
318358

319-
const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd, {
359+
const result = await spawnSocketCli(binCliPath, cmd, {
320360
cwd: tempFixture.path,
321361
env: getTestEnv(apiToken),
322362
})
363+
stdout = result.stdout
364+
stderr = result.stderr
365+
code = result.code
323366

324367
if (code !== 0) {
325-
logger.error(`Command failed with code ${code}`)
326-
logger.error('stdout:', stdout)
327-
logger.error('stderr:', stderr)
368+
logCommandOutput(code, stdout, stderr)
328369
}
329370

330371
expect(code, 'should exit with code 0').toBe(0)
@@ -346,6 +387,11 @@ describe('socket fix (E2E tests)', async () => {
346387
logger.info(
347388
`\nSuccessfully converted CVE-2021-23337 to GHSA and fixed by upgrading lodash from ${beforeVersion} to ${afterVersion}`,
348389
)
390+
} catch (e) {
391+
if (code !== 0) {
392+
logCommandOutput(code, stdout, stderr)
393+
}
394+
throw e
349395
} finally {
350396
await tempFixture.cleanup()
351397
}
@@ -360,6 +406,9 @@ describe('socket fix (E2E tests)', async () => {
360406
'should fix all vulnerabilities in Python project',
361407
async cmd => {
362408
const tempFixture = await createTempFixtureCopy('e2e-test-py')
409+
let stdout = ''
410+
let stderr = ''
411+
let code = -1
363412

364413
try {
365414
const beforeReqs = await readRequirementsTxt(tempFixture.path)
@@ -370,15 +419,16 @@ describe('socket fix (E2E tests)', async () => {
370419
expect(beforeDjango).toBeDefined()
371420
expect(beforeDjango).toContain('3.0.0')
372421

373-
const { code, stderr, stdout } = await spawnSocketCli(binCliPath, cmd, {
422+
const result = await spawnSocketCli(binCliPath, cmd, {
374423
cwd: tempFixture.path,
375424
env: getTestEnv(apiToken),
376425
})
426+
stdout = result.stdout
427+
stderr = result.stderr
428+
code = result.code
377429

378430
if (code !== 0) {
379-
logger.error(`Command failed with code ${code}`)
380-
logger.error('stdout:', stdout)
381-
logger.error('stderr:', stderr)
431+
logCommandOutput(code, stdout, stderr)
382432
}
383433

384434
expect(code, 'should exit with code 0').toBe(0)
@@ -406,6 +456,11 @@ describe('socket fix (E2E tests)', async () => {
406456
logger.info(
407457
`\nSuccessfully upgraded django from ${beforeVersion} to ${afterVersion}`,
408458
)
459+
} catch (e) {
460+
if (code !== 0) {
461+
logCommandOutput(code, stdout, stderr)
462+
}
463+
throw e
409464
} finally {
410465
await tempFixture.cleanup()
411466
}

0 commit comments

Comments
 (0)