Skip to content

Commit 10bb9c4

Browse files
committed
Fix missing Array.isArray guard on targets field in get_file_coverage
1 parent 208c45a commit 10bb9c4

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/mcp/tools/coverage/__tests__/get_file_coverage.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,22 @@ describe('get_file_coverage', () => {
362362
expect(text).toContain('No coverage data found for "Missing.swift"');
363363
});
364364

365+
it('should return no data when targets field is not an array', async () => {
366+
const mockExecutor = createMockExecutor({
367+
success: true,
368+
output: JSON.stringify({ targets: 'not-an-array' }),
369+
});
370+
371+
const result = await get_file_coverageLogic(
372+
{ xcresultPath: '/tmp/test.xcresult', file: 'Foo.swift', showLines: false },
373+
mockExecutor,
374+
);
375+
376+
expect(result.isError).toBe(true);
377+
const text = result.content[0].type === 'text' ? result.content[0].text : '';
378+
expect(text).toContain('No coverage data found for "Foo.swift"');
379+
});
380+
365381
it('should handle file entry with no functions gracefully', async () => {
366382
const noFunctions = [{ file: '/src/Empty.swift', functions: [] }];
367383
const mockExecutor = createMockExecutor({

src/mcp/tools/coverage/get_file_coverage.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ export async function get_file_coverageLogic(
113113

114114
if (Array.isArray(data)) {
115115
fileEntries = (data as RawFileEntry[]).map(normalizeFileEntry);
116-
} else if (typeof data === 'object' && data !== null && 'targets' in data) {
116+
} else if (
117+
typeof data === 'object' &&
118+
data !== null &&
119+
'targets' in data &&
120+
Array.isArray((data as { targets: unknown }).targets)
121+
) {
117122
const targets = (data as { targets: { files?: RawFileEntry[] }[] }).targets;
118123
for (const t of targets) {
119124
if (t.files) {

0 commit comments

Comments
 (0)