forked from getsentry/XcodeBuildMCP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ios_simulator.ts
More file actions
186 lines (172 loc) · 5.56 KB
/
test_ios_simulator.ts
File metadata and controls
186 lines (172 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* iOS Simulator Test Tools - Tools for running tests on iOS applications in simulators
*
* This module provides specialized tools for running tests on iOS applications in simulators
* using xcodebuild test. It supports both workspace and project-based testing with simulator targeting
* by name or UUID, and includes test failure parsing.
*
* Responsibilities:
* - Running tests on iOS applications in simulators from project files and workspaces
* - Supporting simulator targeting by name or UUID
* - Parsing and summarizing test failure results
* - Handling test configuration and derived data paths
*/
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { XcodePlatform, executeXcodeCommand } from '../utils/xcode.js';
import { executeXcodeBuild } from '../utils/build-utils.js';
import { log } from '../utils/logger.js';
import { createTextResponse } from '../utils/validation.js';
import { ToolResponse, ToolResponseContent } from '../types/common.js';
import {
registerTool,
workspacePathSchema,
projectPathSchema,
schemeSchema,
configurationSchema,
derivedDataPathSchema,
extraArgsSchema,
simulatorNameSchema,
simulatorIdSchema,
useLatestOSSchema
} from './common.js';
// --- internal logic ---
async function _handleIOSSimulatorTestLogic(params: {
workspacePath?: string;
projectPath?: string;
scheme: string;
configuration: string;
simulatorName?: string;
simulatorId?: string;
useLatestOS: boolean;
derivedDataPath?: string;
extraArgs?: string[];
}): Promise<ToolResponse> {
log('info', `Starting iOS Simulator tests for scheme ${params.scheme} (internal)`);
const buildResult = await executeXcodeBuild(
{
...params,
},
{
platform: XcodePlatform.iOSSimulator,
simulatorName: params.simulatorName,
simulatorId: params.simulatorId,
useLatestOS: params.useLatestOS,
logPrefix: 'iOS Simulator Test',
},
'test',
);
if (buildResult.isError) return buildResult;
// --- Parse failures ---
const raw = buildResult.rawOutput ?? '';
const failures = raw
.split('\n')
.filter(l => /Test Case .* failed/.test(l))
.map(l => {
const m = l.match(/Test Case '(.*)' failed \((.*)\)/)!;
return { testCase: m[1], reason: m[2] };
});
const summary = failures.length
? `❌ ${failures.length} test(s) failed`
: '✅ All tests passed';
const content: ToolResponseContent[] = [
{ type: 'text', text: summary }
];
// Add failures as formatted text if any exist
if (failures.length > 0) {
content.push({
type: 'text',
text: `Test failures:\n${failures.map(f => `- ${f.testCase}: ${f.reason}`).join('\n')}`
});
}
return { content };
}
/**
* Register all iOS Simulator test tools with the MCP server
*/
export function registerIOSSimulatorTestTools(server: McpServer): void {
// Common default values
const defaults = {
configuration: 'Debug',
useLatestOS: true,
};
// 1) workspace + name
registerTool(
server,
'ios_simulator_test_by_name_workspace',
'Run tests for an iOS app on a simulator specified by name using a workspace',
{
workspacePath: workspacePathSchema,
scheme: schemeSchema,
simulatorName: simulatorNameSchema,
configuration: configurationSchema,
derivedDataPath: derivedDataPathSchema,
extraArgs: extraArgsSchema,
useLatestOS: useLatestOSSchema,
},
(params: any) => _handleIOSSimulatorTestLogic({
...params,
configuration: params.configuration || defaults.configuration,
useLatestOS: params.useLatestOS ?? defaults.useLatestOS
})
);
// 2) project + name
registerTool(
server,
'ios_simulator_test_by_name_project',
'Run tests for an iOS app on a simulator specified by name using a project file',
{
projectPath: projectPathSchema,
scheme: schemeSchema,
simulatorName: simulatorNameSchema,
configuration: configurationSchema,
derivedDataPath: derivedDataPathSchema,
extraArgs: extraArgsSchema,
useLatestOS: useLatestOSSchema,
},
(params: any) => _handleIOSSimulatorTestLogic({
...params,
configuration: params.configuration || defaults.configuration,
useLatestOS: params.useLatestOS ?? defaults.useLatestOS
})
);
// 3) workspace + id
registerTool(
server,
'ios_simulator_test_by_id_workspace',
'Run tests for an iOS app on a simulator specified by ID using a workspace',
{
workspacePath: workspacePathSchema,
scheme: schemeSchema,
simulatorId: simulatorIdSchema,
configuration: configurationSchema,
derivedDataPath: derivedDataPathSchema,
extraArgs: extraArgsSchema,
useLatestOS: useLatestOSSchema,
},
(params: any) => _handleIOSSimulatorTestLogic({
...params,
configuration: params.configuration || defaults.configuration,
useLatestOS: params.useLatestOS ?? defaults.useLatestOS
})
);
// 4) project + id
registerTool(
server,
'ios_simulator_test_by_id_project',
'Run tests for an iOS app on a simulator specified by ID using a project file',
{
projectPath: projectPathSchema,
scheme: schemeSchema,
simulatorId: simulatorIdSchema,
configuration: configurationSchema,
derivedDataPath: derivedDataPathSchema,
extraArgs: extraArgsSchema,
useLatestOS: useLatestOSSchema,
},
(params: any) => _handleIOSSimulatorTestLogic({
...params,
configuration: params.configuration || defaults.configuration,
useLatestOS: params.useLatestOS ?? defaults.useLatestOS
})
);
}