Skip to content

Commit 4b51126

Browse files
committed
chore: wip
1 parent 5d366fb commit 4b51126

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

test/composer-parser.test.ts

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, it } from 'bun:test'
1+
import { describe, expect, it, beforeEach, afterEach, spyOn } from 'bun:test'
22
import {
33
generateComposerUpdates,
44
isComposerFile,
@@ -227,39 +227,36 @@ describe('Composer Parser', () => {
227227
},
228228
}, null, 2)
229229

230-
// We'll mock fs.readFileSync inline in each test for better control
231-
232230
it('should generate updates for composer.json files', async () => {
233-
// Create a mock composer.json file for this test
231+
// Mock fs readFileSync using spyOn
234232
const fs = await import('node:fs')
235-
const path = await import('node:path')
236-
const testComposerPath = path.join(process.cwd(), 'test-updates-composer.json')
233+
const readFileSpy = spyOn(fs, 'readFileSync')
237234

238-
// Write test file
239-
fs.writeFileSync(testComposerPath, mockComposerContent)
235+
readFileSpy.mockImplementation(((filePath: any) => {
236+
if (String(filePath).endsWith('composer.json')) {
237+
return mockComposerContent
238+
}
239+
throw new Error(`File not found: ${filePath}`)
240+
}) as any)
240241

241242
try {
242243
const updates = [
243-
{ name: 'laravel/framework', newVersion: '10.16.0', file: 'test-updates-composer.json' },
244-
{ name: 'phpunit/phpunit', newVersion: '10.3.0', file: 'test-updates-composer.json' },
244+
{ name: 'laravel/framework', newVersion: '10.16.0', file: 'composer.json' },
245+
{ name: 'phpunit/phpunit', newVersion: '10.3.0', file: 'composer.json' },
245246
]
246247

247248
const result = await generateComposerUpdates(updates)
248249

249250
expect(result).toHaveLength(1)
250-
expect(result[0].path).toBe('test-updates-composer.json')
251+
expect(result[0].path).toBe('composer.json')
251252
expect(result[0].type).toBe('update')
252253

253254
const updatedContent = result[0].content
254255
expect(updatedContent).toContain('^10.16.0') // laravel/framework updated
255256
expect(updatedContent).toContain('^10.3.0') // phpunit/phpunit updated
256257
expect(updatedContent).toContain('^6.0.0') // symfony/console unchanged
257-
}
258-
finally {
259-
// Clean up test file
260-
if (fs.existsSync(testComposerPath)) {
261-
fs.unlinkSync(testComposerPath)
262-
}
258+
} finally {
259+
readFileSpy.mockRestore()
263260
}
264261
})
265262

@@ -271,18 +268,21 @@ describe('Composer Parser', () => {
271268
},
272269
}, null, 2)
273270

274-
// Create a mock composer.json file for this test
271+
// Mock fs readFileSync using spyOn
275272
const fs = await import('node:fs')
276-
const path = await import('node:path')
277-
const testComposerPath = path.join(process.cwd(), 'test-constraints-composer.json')
273+
const readFileSpy = spyOn(fs, 'readFileSync')
278274

279-
// Write test file
280-
fs.writeFileSync(testComposerPath, constraintComposerContent)
275+
readFileSpy.mockImplementation(((filePath: any) => {
276+
if (String(filePath).endsWith('composer.json')) {
277+
return constraintComposerContent
278+
}
279+
throw new Error(`File not found: ${filePath}`)
280+
}) as any)
281281

282282
try {
283283
const updates = [
284-
{ name: 'laravel/framework', newVersion: '10.16.0', file: 'test-constraints-composer.json' },
285-
{ name: 'symfony/console', newVersion: '6.3.0', file: 'test-constraints-composer.json' },
284+
{ name: 'laravel/framework', newVersion: '10.16.0', file: 'composer.json' },
285+
{ name: 'symfony/console', newVersion: '6.3.0', file: 'composer.json' },
286286
]
287287

288288
const result = await generateComposerUpdates(updates)
@@ -292,12 +292,8 @@ describe('Composer Parser', () => {
292292

293293
expect(updatedContent).toContain('~10.16.0') // Preserves ~ constraint
294294
expect(updatedContent).toContain('>=6.3.0,<7.0') // Preserves complex constraint
295-
}
296-
finally {
297-
// Clean up test file
298-
if (fs.existsSync(testComposerPath)) {
299-
fs.unlinkSync(testComposerPath)
300-
}
295+
} finally {
296+
readFileSpy.mockRestore()
301297
}
302298
})
303299

@@ -316,17 +312,20 @@ describe('Composer Parser', () => {
316312
})
317313

318314
it('should handle missing packages gracefully', async () => {
319-
// Create a mock composer.json file for this test
315+
// Mock fs readFileSync using spyOn
320316
const fs = await import('node:fs')
321-
const path = await import('node:path')
322-
const testComposerPath = path.join(process.cwd(), 'test-missing-composer.json')
317+
const readFileSpy = spyOn(fs, 'readFileSync')
323318

324-
// Write test file
325-
fs.writeFileSync(testComposerPath, mockComposerContent)
319+
readFileSpy.mockImplementation(((filePath: any) => {
320+
if (String(filePath).endsWith('composer.json')) {
321+
return mockComposerContent
322+
}
323+
throw new Error(`File not found: ${filePath}`)
324+
}) as any)
326325

327326
try {
328327
const updates = [
329-
{ name: 'non-existent/package', newVersion: '1.0.0', file: 'test-missing-composer.json' },
328+
{ name: 'non-existent/package', newVersion: '1.0.0', file: 'composer.json' },
330329
]
331330

332331
const result = await generateComposerUpdates(updates)
@@ -336,12 +335,8 @@ describe('Composer Parser', () => {
336335
const updatedContent = result[0].content
337336
expect(updatedContent).toContain('"laravel/framework": "^10.0.0"') // Original content preserved
338337
expect(updatedContent).not.toContain('non-existent/package')
339-
}
340-
finally {
341-
// Clean up test file
342-
if (fs.existsSync(testComposerPath)) {
343-
fs.unlinkSync(testComposerPath)
344-
}
338+
} finally {
339+
readFileSpy.mockRestore()
345340
}
346341
})
347342
})

0 commit comments

Comments
 (0)