Skip to content

Commit 8c8c0ae

Browse files
Improve and clarify error for missing scheme file (#2599)
* Add test for getBuildConfigurationFromXcScheme * Remove extra apostrophe from schemes error message * Improve and clarify error for missing scheme file
1 parent 3ab727c commit 8c8c0ae

File tree

2 files changed

+105
-7
lines changed

2 files changed

+105
-7
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import {getBuildConfigurationFromXcScheme} from '../getBuildConfigurationFromXcScheme';
2+
import fs from 'fs';
3+
import path from 'path';
4+
import {CLIError} from '@react-native-community/cli-tools';
5+
6+
jest.mock('fs', () => ({
7+
readFileSync: jest.fn(),
8+
readdirSync: jest.fn(),
9+
}));
10+
11+
describe('getBuildConfigurationFromXcScheme', () => {
12+
afterEach(() => {
13+
jest.resetAllMocks();
14+
});
15+
16+
it('returns build configuration when the shared scheme file exists', () => {
17+
(fs.readdirSync as jest.Mock).mockReturnValue(['Test.xcodeproj']);
18+
19+
const xmlContent = `<Scheme>
20+
<LaunchAction buildConfiguration="Debug"/>
21+
</Scheme>`;
22+
(fs.readFileSync as jest.Mock).mockReturnValue(xmlContent);
23+
24+
const sourceDir = '/some/dir';
25+
const scheme = 'Test';
26+
const defaultConfig = 'Release';
27+
const projectInfo = {name: 'Test', schemes: [scheme]};
28+
29+
const result = getBuildConfigurationFromXcScheme(
30+
scheme,
31+
defaultConfig,
32+
sourceDir,
33+
projectInfo,
34+
);
35+
36+
expect(result).toBe('Debug');
37+
38+
const expectedPath = path.join(
39+
sourceDir,
40+
'Test.xcodeproj',
41+
'xcshareddata',
42+
'xcschemes',
43+
`${scheme}.xcscheme`,
44+
);
45+
expect(fs.readFileSync).toHaveBeenCalledWith(expectedPath, {
46+
encoding: 'utf-8',
47+
});
48+
});
49+
50+
it('throws CLIError when reading the shared scheme file fails', () => {
51+
process.env.FORCE_COLOR = '0'; // To disable chalk
52+
(fs.readdirSync as jest.Mock).mockReturnValue(['Test.xcodeproj']);
53+
(fs.readFileSync as jest.Mock).mockImplementation(() => {
54+
throw new Error('File not found');
55+
});
56+
57+
const sourceDir = '/some/dir';
58+
const scheme = 'Test';
59+
const defaultConfig = 'Release';
60+
const projectInfo = {name: 'Test', schemes: [scheme]};
61+
62+
expect(() => {
63+
getBuildConfigurationFromXcScheme(
64+
scheme,
65+
defaultConfig,
66+
sourceDir,
67+
projectInfo,
68+
);
69+
}).toThrow(CLIError);
70+
71+
try {
72+
getBuildConfigurationFromXcScheme(
73+
scheme,
74+
defaultConfig,
75+
sourceDir,
76+
projectInfo,
77+
);
78+
} catch (err) {
79+
const msg = (err as CLIError).message;
80+
expect(msg).toContain(`Could not load the shared scheme for ${scheme}`);
81+
expect(msg).toContain(`includes: ${projectInfo.schemes[0]}`);
82+
}
83+
});
84+
85+
it('returns the default configuration when no .xcodeproj folder is found', () => {
86+
(fs.readdirSync as jest.Mock).mockReturnValue([]);
87+
88+
const sourceDir = '/some/dir';
89+
const scheme = 'Test';
90+
const defaultConfig = 'Release';
91+
const result = getBuildConfigurationFromXcScheme(
92+
scheme,
93+
defaultConfig,
94+
sourceDir,
95+
undefined,
96+
);
97+
98+
expect(result).toBe(defaultConfig);
99+
});
100+
});

packages/cli-platform-apple/src/tools/getBuildConfigurationFromXcScheme.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ export function getBuildConfigurationFromXcScheme(
3939
return Scheme.LaunchAction['@_buildConfiguration'];
4040
}
4141
} catch {
42-
const availableSchemas =
43-
projectInfo && projectInfo.schemes && projectInfo.schemes.length > 0
44-
? `Available schemas are: ${projectInfo.schemes
45-
.map((name) => chalk.bold(name))
46-
.join(', ')}'`
47-
: '';
42+
const projectSchemes =
43+
projectInfo?.schemes && projectInfo.schemes.length > 0
44+
? `${projectInfo.schemes.map((name) => chalk.bold(name)).join(', ')}`
45+
: 'No schemes';
4846

4947
throw new CLIError(
50-
`Could not find scheme ${scheme}. Please make sure the schema you want to run exists. ${availableSchemas}`,
48+
`Could not load the shared scheme for ${scheme}. Your project configuration includes: ${projectSchemes}. Please ensure a valid .xcscheme file exists in xcshareddata/xcschemes.`,
5149
);
5250
}
5351

0 commit comments

Comments
 (0)