Skip to content

Commit 5f3ef59

Browse files
committed
fix Cpptools.test.ts
1 parent f9ae70a commit 5f3ef59

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/cpptools.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,19 @@ export class CppConfigurationProvider implements cpptools.CustomConfigurationPro
485485
}
486486

487487
if (sysroot) {
488-
// Send sysroot with quoting for CppTools API V5 and below.
489-
flags.push('--sysroot=' + shlex.quote(sysroot));
488+
if (!useFragments) {
489+
// Send sysroot with quoting for CppTools API V5 and below.
490+
flags.push('--sysroot=' + shlex.quote(sysroot));
491+
} else {
492+
// Pass sysroot (without quote added) as the only compilerArgs for CppTools API V6 and above.
493+
flags.push(('--sysroot=' + sysroot));
494+
}
490495
}
491496

492497
this.workspaceBrowseConfiguration = {
493498
browsePath: newBrowsePath,
494499
compilerPath: normalizedCompilerPath || undefined,
495-
// Pass sysroot (without quote added) as the only compilerArgs for CppTools API V6 and above.
496-
compilerArgs: !useFragments ? flags : sysroot ? [('--sysroot=' + sysroot)] : undefined,
500+
compilerArgs: flags,
497501
compilerFragments: useFragments ? compileCommandFragments : undefined,
498502
standard
499503
// windowsSdkVersion
@@ -521,7 +525,7 @@ export class CppConfigurationProvider implements cpptools.CustomConfigurationPro
521525
* @param fileGroup The file group
522526
* @param options Index update options
523527
*/
524-
private updateFileGroup(sourceDir: string, fileGroup: codeModel.CodeModelFileGroup, options: codeModel.CodeModelParams, target: TargetDefaults, sysroot: string | undefined) {
528+
private updateFileGroup(sourceDir: string, fileGroup: codeModel.CodeModelFileGroup, options: codeModel.CodeModelParams, target: TargetDefaults, sysroot?: string) {
525529
const configuration = this.buildConfigurationData(fileGroup, options, target, sysroot);
526530
for (const src of fileGroup.sources) {
527531
const absolutePath = path.isAbsolute(src) ? src : path.join(sourceDir, src);

test/unit-tests/cpptools.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ suite('CppTools tests', () => {
163163
expect(mode).to.eql('clang-x86');
164164
});
165165

166+
// Validate codeModel using cppTools API V6
166167
test('Validate code model', async () => {
167168
const provider = new CppConfigurationProvider();
168169
const cache = await CMakeCache.fromPath(getTestResourceFilePath('TestCMakeCache.txt'));
@@ -181,7 +182,8 @@ suite('CppTools tests', () => {
181182
fileGroups: [{
182183
sources: [sourceFile],
183184
isGenerated: false,
184-
compileCommandFragments: ['-DFLAG1'],
185+
defines: ['DEFINE1'],
186+
compileCommandFragments: ['-DFRAGMENT1'],
185187
language: 'CXX'
186188
}]
187189
},
@@ -191,7 +193,8 @@ suite('CppTools tests', () => {
191193
fileGroups: [{
192194
sources: [sourceFile],
193195
isGenerated: false,
194-
compileCommandFragments: ['-DFLAG2'],
196+
defines: ['DEFINE2'],
197+
compileCommandFragments: ['-DFRAGMENT2'],
195198
language: 'CXX'
196199
}]
197200
}
@@ -220,7 +223,8 @@ suite('CppTools tests', () => {
220223
fileGroups: [{
221224
sources: [sourceFile2],
222225
isGenerated: false,
223-
compileCommandFragments: ['-DFLAG3'],
226+
defines: ['DEFINE3'],
227+
compileCommandFragments: ['-DFRAGMENT3'],
224228
language: 'CXX'
225229
}]
226230
}]
@@ -236,19 +240,22 @@ suite('CppTools tests', () => {
236240

237241
configurations = await provider.provideConfigurations([uri]);
238242
expect(configurations.length).to.eq(1);
239-
expect(configurations[0].configuration.defines).to.contain('FLAG1');
243+
expect(configurations[0].configuration.defines).to.eq('DEFINE3');
244+
expect(configurations[0].configuration.compilerFragments).to.contain('-DFRAGMENT3');
245+
expect(configurations[0].configuration.compilerArgs).to.eq(undefined);
240246

241247
provider.updateConfigurationData({ cache, codeModelContent, activeTarget: 'target2', activeBuildTypeVariant: 'Release', folder: here });
242248
configurations = await provider.provideConfigurations([uri]);
243249
expect(configurations.length).to.eq(1);
244-
expect(configurations[0].configuration.defines).to.contain('FLAG2');
250+
expect(configurations[0].configuration.defines).to.eq('DEFINE2');
251+
expect(configurations[0].configuration.compilerFragments).to.contain('-DFRAGMENT2');
245252
expect(configurations[0].configuration.compilerPath).to.eq('clang++');
246253

247254
provider.updateConfigurationData({ cache, codeModelContent, activeTarget: 'all', activeBuildTypeVariant: 'Release', folder: here });
248255
configurations = await provider.provideConfigurations([uri]);
249256
expect(configurations.length).to.eq(1);
250-
expect(configurations[0].configuration.defines.some(def => def === 'FLAG1' || def === 'FLAG2')).to.be.true;
251-
expect(configurations[0].configuration.defines).to.not.have.all.members(['FLAG1', 'FLAG2']);
257+
expect(configurations[0].configuration.defines.some(def => def === 'DEFINE1' || def === 'DEFINE2')).to.be.true;
258+
expect(configurations[0].configuration.defines).to.not.have.all.members(['DEFINE1', 'DEFINE2']);
252259

253260
// Verify the per-folder browsePath.
254261
const canProvideBrowseConfigPerFolder: boolean = await provider.canProvideBrowseConfigurationsPerFolder();
@@ -260,7 +267,7 @@ suite('CppTools tests', () => {
260267
// Verify the browsePath with a different folder.
261268
const configurations2 = await provider.provideConfigurations([uri2]);
262269
expect(configurations2.length).to.eq(1);
263-
expect(configurations2[0].configuration.defines).to.contain('FLAG3');
270+
expect(configurations2[0].configuration.defines).to.contain('DEFINE3');
264271
const browseConfig2 = await provider.provideFolderBrowseConfiguration(vscode.Uri.file(smokeFolder));
265272
expect(browseConfig2.browsePath.length).to.eq(1);
266273
expect(browseConfig2.browsePath[0]).to.eq(util.platformNormalizePath(smokeFolder));

0 commit comments

Comments
 (0)