Skip to content

Commit

Permalink
Add support for test file/line number
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbonnet committed Nov 6, 2020
1 parent 22f3ac9 commit a91af89
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@
"default": "",
"scope": "resource"
},
"cmakeExplorer.testFileVar": {
"description": "CTest environment variable defined for a test, giving the path of the source file containing the test",
"type": "string",
"default": "",
"scope": "resource"
},
"cmakeExplorer.testLineVar": {
"description": "CTest environment variable defined for a test, giving the line number within the file where the test definition starts (if known)",
"type": "string",
"default": "",
"scope": "resource"
},
"cmakeExplorer.logpanel": {
"description": "Write diagnotic logs to an output panel",
"type": "boolean",
Expand Down
78 changes: 77 additions & 1 deletion src/cmake-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,15 @@ export class CmakeAdapter implements TestAdapter {
buildConfig,
extraCtestLoadArgs,
suiteDelimiter,
testFileVar,
testLineVar,
] = await this.getConfigStrings([
'buildDir',
'buildConfig',
'extraCtestLoadArgs',
'suiteDelimiter',
'testFileVar',
'testLineVar',
]);

// Load CTest test list
Expand All @@ -225,7 +229,7 @@ export class CmakeAdapter implements TestAdapter {
extraCtestLoadArgs
);

// Convert to Text Explorer format
// Convert to Test Explorer format
const rootSuite: TestSuiteInfo = {
type: 'suite',
id: ROOT_SUITE_ID,
Expand All @@ -239,6 +243,7 @@ export class CmakeAdapter implements TestAdapter {
type: 'test',
id: test.name,
label: test.name,
...getTestFileInfo(test, testFileVar, testLineVar),
}));
return rootSuite;
} else {
Expand Down Expand Up @@ -275,6 +280,7 @@ export class CmakeAdapter implements TestAdapter {
label: testName,
description: test.name,
tooltip: test.name,
...getTestFileInfo(test, testFileVar, testLineVar),
};
suite.children.push(testInfo);
}
Expand Down Expand Up @@ -569,3 +575,73 @@ export class CmakeAdapter implements TestAdapter {
return parallelJobs;
}
}

/**
* Get test file/line number info from CMake test info
*
* @param test CMake test info
* @param testFileVar CTest environment variable for file path
* @param testLineVar CTest environment variable for line number
*/
const getTestFileInfo = (
test: CmakeTestInfo,
testFileVar: string,
testLineVar: string
) => {
const variables = getTestEnvironmentVariables(test);
if (!variables) return {};

return {
file: getFileFromEnvironment(variables, testFileVar),
line: getLineFromEnvironment(variables, testLineVar),
};
};

/**
* Get environment variables defined for a CMake test
*
* @param test CMake test info
*/
const getTestEnvironmentVariables = (test: CmakeTestInfo) => {
const ENVIRONMENT = test.properties.find((p) => p.name === 'ENVIRONMENT');
if (ENVIRONMENT) return ENVIRONMENT.value as string[];
return;
};

/**
* Get file path from environment variables
*
* @param variables Array of environment variables in the form `NAME=VALUE`
* @param varname Variable name to get value for
*/
const getFileFromEnvironment = (variables: string[], fileVar: string) => {
return getVariableValue(variables, fileVar);
};

/**
* Get line number from environment variables
*
* @param variables Array of environment variables in the form `NAME=VALUE`
* @param varname Variable name to get value for
*/
const getLineFromEnvironment = (variables: string[], varname: string) => {
const value = getVariableValue(variables, varname);
if (value) return Number.parseInt(value) - 1;
return;
};

/**
* Extract value from environment variables
*
* @param variables Array of environment variables in the form `NAME=VALUE`
* @param varname Variable name to get value for
*/
const getVariableValue = (variables: string[], varname: string) => {
if (!varname) return;
const varRe = new RegExp(`^${varname}=(.*)$`);
for (let variable of variables) {
const match = variable.match(varRe);
if (match) return match[1];
}
return;
};
2 changes: 1 addition & 1 deletion src/interfaces/cmake-test-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export interface CmakeTestInfo {
command: string[];
properties: {
name: string;
value: string;
value: string | string[];
}[];
}

0 comments on commit a91af89

Please sign in to comment.