Skip to content

Commit

Permalink
add command: runSyncTest and viewCodeCoverage to run test class and v…
Browse files Browse the repository at this point in the history
…iew coverage
  • Loading branch information
xjsender committed Oct 19, 2019
1 parent 4f23ec1 commit 16d27cd
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 51 deletions.
3 changes: 2 additions & 1 deletion i18n/zho/out/commands/main.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"fileDeployed.text": "代码已经部署成功",
"fileDestructed.text": "代码已经删除成功",
"noSubscribedMetadata.text": "没有订阅的MetadataObjects",
"deleteConfirmation.text": "请再次确认是否从服务器中删除该文件?"
"deleteConfirmation.text": "请再次确认是否从服务器中删除该文件?",
"coverageTip.text": "打开你的功能类,点击状态栏上的'查看测试覆盖率'即可查看测试覆盖情况"
}
3 changes: 3 additions & 0 deletions i18n/zho/out/utils/coverage.i18n.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"viewTestCoverage.text": "查看测试覆盖率"
}
4 changes: 2 additions & 2 deletions i18n/zho/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"extension.haoide.generateWorkbooks.title": "生成对象的Workbook",
"extension.haoide.reloadSobjectCache.title": "重新加载对象的缓存",
"extension.haoide.reloadSymbolTable.title": "重新加载SymbolTable",
"extension.haoide.runSyncTest.title": "Run Sync Test",
"extension.haoide.runSyncTest.title": "执行同步单元测试",
"extension.haoide.convertArray2Table.title": "将JSON数组转换成CSV",
"extension.haoide.convertJson2Apex.title": "将JSON转换为Apex代码",
"extension.haoide.convertJson2Typescript.title": "将JSON转换为Typescript",
Expand All @@ -52,5 +52,5 @@
"extension.haoide.setSyntaxToApex.title": "Set Syntax: Apex",
"extension.haoide.setSyntaxToJS.title": "Set Syntax: JS",
"extension.haoide.setSyntaxToHtml.title": "Set Syntax: HTML",
"extension.haoide.createManifestFile.title": "New Manifest File"
"extension.haoide.createManifestFile.title": "创建Package.xml文件"
}
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@
"title": "%extension.haoide.createManifestFile.title%",
"category": "Haoide"
},
{
"command": "extension.haoide.runSyncTest",
"title": "%extension.haoide.runSyncTest.title%",
"category": "Haoide"
},
{
"command": "extension.haoide.setSyntaxToApex",
"title": "%extension.haoide.setSyntaxToApex.title%",
Expand Down Expand Up @@ -310,6 +315,10 @@
"command": "extension.haoide.addDefaultProjectToWorkspace",
"when": "haoide.hasDefaultProject"
},
{
"command": "extension.haoide.runSyncTest",
"when": "haoide.hasDefaultProject && haoide.isTestClass"
},
{
"command": "extension.haoide.loginToSFDC",
"when": "haoide.hasOpenProject"
Expand Down Expand Up @@ -549,11 +558,16 @@
},
{
"group": "haoide@8",
"command": "extension.haoide.runSyncTest",
"when": "haoide.hasOpenProject && haoide.isTestClass"
},
{
"group": "haoide@9",
"command": "extension.haoide.viewFileProperty",
"when": "haoide.hasOpenProject && resourceFilename != package.xml"
},
{
"group": "haoide@9",
"group": "haoide@10",
"command": "extension.haoide.viewIdInBrowser",
"when": "haoide.hasOpenProject && haoide.hasIdSelected"
}
Expand Down
91 changes: 70 additions & 21 deletions src/commands/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ import { _session, settings, metadata, extensionSettings } from '../settings';
import {
SObjectDesc,
MetadataModel,
TestSuite, TestObject,
DeployResult,
TestSuite,
Template,
ConfirmAction,
QueryResult,
SObjectReloadScope,
GlobalDescribe,
SObjectSOQL
SObjectSOQL,
TestResponse
} from '../typings';
import { CheckRetrieveResult, CheckDeployResult } from '../typings/meta';
import { convertArrayToTable } from '../utils/json';
import CodeCoverage from '../utils/coverage';

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -301,37 +302,85 @@ export function executeRestTest(options: any) {
*/
export function runSyncTest() {
let editor = vscode.window.activeTextEditor;
if (editor) {
let property = util.getFilePropertyByFileName(
editor.document.fileName
if (!editor) {
return;
}

// Get file property cache
let property = util.getFilePropertyByFileName(
editor.document.fileName
);

let toolingApi = new ToolingApi();
ProgressNotification.showProgress(
toolingApi, "runSyncTest", {
data: {
"tests": [{
classId: property.id
}] as TestSuite
},
progressMessage: "Running test class, please wait"
}
)
.then( (result: TestResponse) => {
let codeCoverage = CodeCoverage.getInstance();
vscode.window.showInformationMessage(
localize('coverageTip.text',
'Open the function class and click the view code coverage item in the status bar'
)
);

runSyncTests([{
classId: property.id,
testMethods: [
"testCommunitiesLoginController"
]
}, {
maxFailedTests: 2
}]);
})
.catch (err => {
vscode.window.showErrorMessage(err.message);
});
}

export function viewCodeCoverage(classId?: string) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
return;
}
else {
util.showCommandWarning();

// Get file property cache
let fileName = editor.document.fileName;
let property = util.getFilePropertyByFileName(fileName);

if (!classId) {
classId = property.id;
}

ProgressNotification.showProgress(
new ToolingApi(), 'query', {
soql: "SELECT Coverage FROM ApexCodeCoverageAggregate " +
`WHERE ApexClassOrTriggerId = '${classId}'`,
progressMessage: "Fetching code coverage"
}
)
.then( (result: QueryResult) => {
if (result.records && result.records.length > 0) {
let coverage = result.records[0].Coverage;
let coveredPercent = coverage.coveredLines.length /
( coverage.uncoveredLines.length + coverage.coveredLines.length);
CodeCoverage.getInstance().report(
fileName, coveredPercent, coverage.uncoveredLines
);
}
})
.catch( err => {
vscode.window.showErrorMessage(err.message);
});
}

/**
* Running sync test class
*
* @param classIds ids of test class to be ran
*/
export function runSyncTests(testSuites: TestSuite[]) {
export function runSyncTests(data: any) {
let toolingApi = new ToolingApi();
ProgressNotification.showProgress(
toolingApi, "runSyncTest", {
data: {
"tests": testSuites
},
data: data,
progressMessage: "Running test class, please wait"
}
)
Expand Down
11 changes: 10 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const localize = nls.config({ messageFormat: nls.MessageFormat.file })();

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { commands, languages, ExtensionContext, Uri } from 'vscode';
import { commands, languages, ExtensionContext, Uri, window } from 'vscode';
import * as provider from "./salesforce/completions/provider";
import * as contextUtil from "./utils/context";
import { auth, utility, main, packages } from "./commands";
Expand Down Expand Up @@ -337,6 +337,14 @@ export function activate(context: ExtensionContext) {
}
));

// Register fetchCodeCoverage command
context.subscriptions.push(commands.registerCommand(
"extension.haoide.fetchCodeCoverage", () => {
main.viewCodeCoverage();
}
));


/**
* Webview part
*/
Expand Down Expand Up @@ -395,6 +403,7 @@ export function activate(context: ExtensionContext) {
contextUtil.setHasIdSelected();
contextUtil.watchWorkspaceChange();
contextUtil.watchActiveEditorChange();
contextUtil.setIsTestClass(window.activeTextEditor);

/**
* Status bar item
Expand Down
47 changes: 38 additions & 9 deletions src/typings/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,43 @@ export interface TestSuite {
* runTestsSynchronous response
*/
export interface TestResponse {
numTestsRun: number;
numFailures: number;
totalTime: number;
codeCoverage: number;
codeCoverageWarnings: number;
apexLogId: string;
successes: Success[];
failures: Failure[];
apexLogId: Object | undefined;
codeCoverage: CodeCoverage[];
codeCoverageWarnings: CodeCoverageWarnings[];
failures: Failure[];
flowCoverage: FlowCoverage[];
flowCoverageWarnings: string[];
numFailures: number;
numTestsRun: number;
successes: Success[];
totalTime: number;
}

export interface CodeCoverage {
id: string;
locationsNotCovered: any[];
name: string;
namespace: Object | undefined;
numLocations: number;
numLocationsNotCovered: number;
type: string;
}

export interface CodeCoverageWarnings {
id: string;
message: string;
name: string;
namespace: Object | undefined;
}

export interface FlowCoverage {
elementsNotCovered: string[];
flowId: string;
flowName: string;
flowNamespace: Object | undefined;
numElements: number;
numElementsNotCovered: number;
processType: string;
}

/**
Expand Down Expand Up @@ -61,4 +90,4 @@ export interface Failure {
seeAllData: boolean;
time: number;
packageName: string;
}
}
30 changes: 16 additions & 14 deletions src/utils/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,24 @@ export function setHasIdSelected() {
export function watchActiveEditorChange() {
vscode.window.onDidChangeActiveTextEditor(
(editor: vscode.TextEditor | undefined) => {
let isTestClass = false;
setIsTestClass(editor);
}
);
}

if (editor) {
let data = fs.readFileSync(
editor.document.fileName, "utf-8"
).toString();
if (data.indexOf(" testMethod ") !== -1
|| data.indexOf("@isTest") !== -1) {
isTestClass = true;
}
}
export function setIsTestClass(editor: vscode.TextEditor | undefined) {
let isTestClass = false;

// Register context key: isTestClass
vscode.commands.executeCommand(
'setContext', 'haoide.isTestClass', isTestClass
);
if (editor && fs.existsSync(editor.document.fileName)) {
let data = fs.readFileSync(
editor.document.fileName, "utf-8"
).toString();
if (/\stestMethod\s/gi.test(data) || /@isTest/gi.test(data)) {
isTestClass = true;
}
}

vscode.commands.executeCommand(
'setContext', 'haoide.isTestClass', isTestClass
);
}
Loading

0 comments on commit 16d27cd

Please sign in to comment.