Skip to content

Commit 7ef01ed

Browse files
jfblaaclaude
andauthored
test(manifest): simplify the .NET facts tests
Table-driven coverage and wizard cases, file-level temp-dir and mock hooks, and small helpers for the repeated isRootCovered, sidecar and auto-manifest calls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3268fdf commit 7ef01ed

8 files changed

Lines changed: 220 additions & 394 deletions

‎src/commands/manifest/detect-manifest-actions.test.mts‎

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,21 @@ import {
1111

1212
import type { SocketJson } from '../../utils/socket-json.mts'
1313

14-
function mkTmp(): string {
15-
return mkdtempSync(path.join(os.tmpdir(), 'detect-manifest-bazel-'))
16-
}
14+
let cwd: string
15+
16+
beforeEach(() => {
17+
cwd = mkdtempSync(path.join(os.tmpdir(), 'detect-manifest-'))
18+
})
19+
20+
afterEach(() => {
21+
rmSync(cwd, { recursive: true, force: true })
22+
})
1723

1824
function touch(dir: string, name: string): void {
1925
writeFileSync(path.join(dir, name), '')
2026
}
2127

2228
describe('detectManifestActions — bazel detector', () => {
23-
let cwd: string
24-
25-
beforeEach(() => {
26-
cwd = mkTmp()
27-
})
28-
29-
afterEach(() => {
30-
rmSync(cwd, { recursive: true, force: true })
31-
})
32-
3329
it('skips bazel when defaults.manifest.bazel.disabled is true', async () => {
3430
touch(cwd, 'MODULE.bazel')
3531
const result = await detectManifestActions(
@@ -78,16 +74,6 @@ describe('detectManifestActions — bazel detector', () => {
7874
})
7975

8076
describe('detectManifestActions — gradle detector', () => {
81-
let cwd: string
82-
83-
beforeEach(() => {
84-
cwd = mkTmp()
85-
})
86-
87-
afterEach(() => {
88-
rmSync(cwd, { recursive: true, force: true })
89-
})
90-
9177
it('detects build.gradle', async () => {
9278
touch(cwd, 'build.gradle')
9379
const result = await detectManifestActions(null, cwd)
@@ -143,16 +129,6 @@ describe('detectManifestActions — gradle detector', () => {
143129
})
144130

145131
describe('detectManifestActions — conda detector', () => {
146-
let cwd: string
147-
148-
beforeEach(() => {
149-
cwd = mkTmp()
150-
})
151-
152-
afterEach(() => {
153-
rmSync(cwd, { recursive: true, force: true })
154-
})
155-
156132
it('detects environment.yml and reports it as the conda file', async () => {
157133
touch(cwd, 'environment.yml')
158134
const result = await detectManifestActions(null, cwd)
@@ -198,16 +174,6 @@ describe('detectManifestActions — conda detector', () => {
198174
})
199175

200176
describe('detectManifestActions — dotnet detector', () => {
201-
let cwd: string
202-
203-
beforeEach(() => {
204-
cwd = mkTmp()
205-
})
206-
207-
afterEach(() => {
208-
rmSync(cwd, { recursive: true, force: true })
209-
})
210-
211177
it.each([
212178
'App.sln',
213179
'App.slnx',
@@ -231,7 +197,7 @@ describe('detectManifestActions — dotnet detector', () => {
231197
touch(cwd, 'App.sln')
232198
const sockJson = {
233199
defaults: { manifest: { dotnet: { disabled: true } } },
234-
} as unknown as SocketJson
200+
} as SocketJson
235201
expect((await detectManifestActions(sockJson, cwd)).dotnet).toBe(false)
236202
})
237203

‎src/commands/manifest/discover-manifest-roots.test.mts‎

Lines changed: 87 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from 'node:fs'
22
import { tmpdir } from 'node:os'
33
import path from 'node:path'
44

5-
import { describe, expect, it } from 'vitest'
5+
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
66

77
import {
88
buildRootPath,
@@ -224,50 +224,39 @@ describe('findBuildToolCandidates', () => {
224224
})
225225

226226
describe('isBuildRootCovered', () => {
227-
it('covers a POM only by its own file', () => {
228-
const covered = new Set(['/repo/service/pom.xml'])
229-
expect(
230-
isBuildRootCovered(covered, 'maven', {
231-
dir: '/repo/service',
232-
entryFile: 'pom.xml',
233-
}),
234-
).toBe(true)
235-
expect(
236-
isBuildRootCovered(covered, 'maven', {
237-
dir: '/repo/service',
238-
entryFile: 'app-dev.pom.xml',
239-
}),
240-
).toBe(false)
241-
})
242-
243-
it('covers a .NET project by its project file, never a solution', () => {
244-
const covered = new Set(['/repo/src/App/App.csproj'])
245-
expect(
246-
isBuildRootCovered(covered, 'dotnet', {
247-
dir: '/repo/src/App',
248-
entryFile: 'App.csproj',
249-
}),
250-
).toBe(true)
251-
expect(
252-
isBuildRootCovered(covered, 'dotnet', {
253-
dir: '/repo/src/App',
254-
entryFile: 'App.sln',
255-
}),
256-
).toBe(false)
257-
})
258-
259-
it('covers a root without an entry file through a marker file', () => {
260-
expect(
261-
isBuildRootCovered(new Set(['/repo/lib/settings.gradle.kts']), 'gradle', {
262-
dir: '/repo/lib',
263-
}),
264-
).toBe(true)
265-
expect(
266-
isBuildRootCovered(new Set(['/repo/lib/sub/build.gradle']), 'gradle', {
267-
dir: '/repo/lib',
268-
}),
269-
).toBe(false)
270-
})
227+
it.for([
228+
[
229+
'maven',
230+
'/repo/service/pom.xml',
231+
{ dir: '/repo/service', entryFile: 'pom.xml' },
232+
true,
233+
],
234+
[
235+
'maven',
236+
'/repo/service/pom.xml',
237+
{ dir: '/repo/service', entryFile: 'app-dev.pom.xml' },
238+
false,
239+
],
240+
[
241+
'dotnet',
242+
'/repo/src/App/App.csproj',
243+
{ dir: '/repo/src/App', entryFile: 'App.csproj' },
244+
true,
245+
],
246+
[
247+
'dotnet',
248+
'/repo/src/App/App.csproj',
249+
{ dir: '/repo/src/App', entryFile: 'App.sln' },
250+
false,
251+
],
252+
['gradle', '/repo/lib/settings.gradle.kts', { dir: '/repo/lib' }, true],
253+
['gradle', '/repo/lib/sub/build.gradle', { dir: '/repo/lib' }, false],
254+
] as const)(
255+
'%s: %s covers %o: %s',
256+
({ 0: tool, 1: file, 2: root, 3: expected }) => {
257+
expect(isBuildRootCovered(new Set([file]), tool, root)).toBe(expected)
258+
},
259+
)
271260
})
272261

273262
describe('sortByDepthThenPath', () => {
@@ -315,122 +304,84 @@ function listing(members: string[]) {
315304
return async () => members
316305
}
317306

318-
async function unreadable(): Promise<undefined> {
319-
return undefined
320-
}
321-
322307
async function failToList(): Promise<string[]> {
323308
throw new Error('listed')
324309
}
325310

311+
async function slnxUnreadable({
312+
entryFile,
313+
}: {
314+
entryFile?: string | undefined
315+
}): Promise<string[] | undefined> {
316+
return entryFile?.endsWith('.slnx') ? undefined : []
317+
}
318+
319+
async function unreadable(): Promise<undefined> {
320+
return undefined
321+
}
322+
326323
describe('isRootCovered', () => {
327324
const root = { dir: '/repo', entryFile: 'App.slnx' }
325+
const covers = (
326+
covered: string[],
327+
list: Parameters<typeof isRootCovered>[4],
328+
r: BuildRoot = root,
329+
) => isRootCovered(new Set(covered), 'dotnet', r, {}, list)
328330

329331
it('covers a solution once every project it builds is covered', async () => {
330-
const covered = new Set(['/repo/src/A/A.csproj', '/repo/src/B/B.csproj'])
331-
expect(
332-
await isRootCovered(
333-
covered,
334-
'dotnet',
335-
root,
336-
{},
337-
listing(['/repo/src/A/A.csproj', '/repo/src/B/B.csproj']),
338-
),
339-
).toBe(true)
332+
const covered = ['/repo/src/A/A.csproj', '/repo/src/B/B.csproj']
333+
expect(await covers(covered, listing(covered))).toBe(true)
340334
expect(
341-
await isRootCovered(
335+
await covers(
342336
covered,
343-
'dotnet',
344-
root,
345-
{},
346337
listing(['/repo/src/A/A.csproj', '/repo/src/C/C.csproj']),
347338
),
348339
).toBe(false)
349340
})
350341

351-
it('takes a solution the SDK cannot read as covered by its twin in the other format', async () => {
352-
const dir = await fs.mkdtemp(path.join(tmpdir(), 'entry-covered-'))
353-
try {
354-
await fs.writeFile(path.join(dir, 'App.sln'), '')
355-
await fs.writeFile(path.join(dir, 'App.slnx'), '')
356-
const covered = new Set(['/repo/src/A/A.csproj'])
357-
const slnxUnreadable = async ({ entryFile }: { entryFile?: string }) =>
358-
entryFile?.endsWith('.slnx') ? undefined : []
359-
expect(
360-
await isRootCovered(
361-
covered,
362-
'dotnet',
363-
{ dir, entryFile: 'App.slnx' },
364-
{},
365-
slnxUnreadable,
366-
),
367-
).toBe(true)
368-
// Also before anything is covered, e.g. when the twin built nothing.
369-
expect(
370-
await isRootCovered(
371-
new Set(),
372-
'dotnet',
373-
{ dir, entryFile: 'App.slnx' },
374-
{},
375-
slnxUnreadable,
376-
),
377-
).toBe(true)
378-
expect(
379-
await isRootCovered(
380-
covered,
381-
'dotnet',
382-
{ dir, entryFile: 'Other.slnx' },
383-
{},
384-
slnxUnreadable,
385-
),
386-
).toBe(false)
387-
} finally {
388-
await fs.rm(dir, { recursive: true, force: true })
389-
}
390-
})
391-
392-
it('builds an unreadable solution whose twin is unreadable too, so its failure shows', async () => {
393-
const dir = await fs.mkdtemp(path.join(tmpdir(), 'entry-covered-'))
394-
try {
395-
await fs.writeFile(path.join(dir, 'App.sln'), '')
396-
await fs.writeFile(path.join(dir, 'App.slnx'), '')
397-
const covered = await Promise.all(
398-
['App.sln', 'App.slnx'].map(entryFile =>
399-
isRootCovered(
400-
new Set(),
401-
'dotnet',
402-
{ dir, entryFile },
403-
{},
404-
unreadable,
405-
),
406-
),
407-
)
408-
expect(covered).toEqual([false, false])
409-
} finally {
410-
await fs.rm(dir, { recursive: true, force: true })
411-
}
412-
})
413-
414342
it('lists no members while nothing is covered and no twin exists, or for a project or a POM', async () => {
415-
const list = failToList
416-
expect(await isRootCovered(new Set(), 'dotnet', root, {}, list)).toBe(false)
343+
expect(await covers([], failToList)).toBe(false)
417344
expect(
418-
await isRootCovered(
419-
new Set(['/x']),
420-
'dotnet',
421-
{ dir: '/repo', entryFile: 'A.csproj' },
422-
{},
423-
list,
424-
),
345+
await covers(['/x'], failToList, { dir: '/repo', entryFile: 'A.csproj' }),
425346
).toBe(false)
426347
expect(
427348
await isRootCovered(
428349
new Set(['/x']),
429350
'maven',
430351
{ dir: '/repo', entryFile: 'pom.xml' },
431352
{},
432-
list,
353+
failToList,
433354
),
434355
).toBe(false)
435356
})
357+
358+
describe('with an App.sln and App.slnx pair', () => {
359+
let dir = ''
360+
const at = (entryFile: string) => ({ dir, entryFile })
361+
362+
beforeAll(async () => {
363+
dir = await fs.mkdtemp(path.join(tmpdir(), 'entry-covered-'))
364+
await fs.writeFile(path.join(dir, 'App.sln'), '')
365+
await fs.writeFile(path.join(dir, 'App.slnx'), '')
366+
})
367+
368+
afterAll(async () => {
369+
await fs.rm(dir, { recursive: true, force: true })
370+
})
371+
372+
it('takes a solution the SDK cannot read as covered by its twin in the other format', async () => {
373+
const covered = ['/repo/src/A/A.csproj']
374+
expect(await covers(covered, slnxUnreadable, at('App.slnx'))).toBe(true)
375+
// Also before anything is covered, e.g. when the twin built nothing.
376+
expect(await covers([], slnxUnreadable, at('App.slnx'))).toBe(true)
377+
expect(await covers(covered, slnxUnreadable, at('Other.slnx'))).toBe(
378+
false,
379+
)
380+
})
381+
382+
it('builds an unreadable solution whose twin is unreadable too, so its failure shows', async () => {
383+
expect(await covers([], unreadable, at('App.sln'))).toBe(false)
384+
expect(await covers([], unreadable, at('App.slnx'))).toBe(false)
385+
})
386+
})
436387
})

0 commit comments

Comments
 (0)