Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
Validate code coverage for individual test classes in TriggerApexTest…
Browse files Browse the repository at this point in the history
… task
  • Loading branch information
aly76 committed Jul 29, 2020
1 parent 965354d commit ababa35
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ async function run() {

try {
const target_org: string = tl.getInput("target_org", true);

test_options["wait_time"] = tl.getInput("wait_time", true);


test_options["testlevel"] = tl.getInput("testlevel", true);
test_options["synchronous"] = tl.getBoolInput("synchronous", false);
test_options["isValidateCoverage"] = tl.getBoolInput("isValidateCoverage", false);
test_options["coverageThreshold"] = tl.getInput("coverageThreshold", false);

if (test_options["testlevel"] == "RunSpecifiedTests")
test_options["specified_tests"] = tl.getInput("specified_tests", true);
Expand Down
19 changes: 18 additions & 1 deletion packages/azpipelines/BuildTasks/TriggerApexTestTask/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@
"helpMarkDown": "Select an option if the tests are to be run synchronously",
"required": false
},
{
"name": "isValidateCoverage",
"type": "boolean",
"label": "Validate code coverage of individual test classes",
"defaultValue": false,
"helpMarkDown": "When enabled, verifies whether indvidual test classes meet minimum code coverage requirement",
"required": false
},
{
"name": "coverageThreshold",
"type": "string",
"label": "Minimum percentage coverage required per test class",
"defaultValue": "75",
"helpMarkDown": "Minimum coverage required per test class, in order for the task to succeed",
"required":true,
"visibleRule": "isValidateCoverage = true"
},
{
"name": "wait_time",
"type": "string",
Expand All @@ -84,4 +101,4 @@
"argumentFormat": ""
}
}
}
}
33 changes: 30 additions & 3 deletions packages/core/src/sfdxwrappers/TriggerApexTestImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ let fs = require("fs-extra");
let path = require("path");

export default class TriggerApexTestImpl {
public constructor(private target_org: string, private test_options: any) {}
public constructor(
private target_org: string,
private test_options: any,
) {}

public async exec(): Promise<{
id: string;
Expand Down Expand Up @@ -84,8 +87,32 @@ export default class TriggerApexTestImpl {
test_result.result = false;
console.error(output);
} else {
test_result.message = `${test_report_json.summary.passing} Tests passed with overall Test Run Coverage of ${test_report_json.summary.testRunCoverage}`;
test_result.result = true;
const classesWithInvalidCoverage: string[] = [];

if (this.test_options["isValidateCoverage"]) {
let code_coverage = fs.readFileSync(
path.join(
this.test_options["outputdir"],
`test-result-codecoverage.json`
),
"utf8"
);
let code_coverage_json = JSON.parse(code_coverage);

for (let testClass of code_coverage_json) {
if (testClass["coveredPercent"] < this.test_options["coverageThreshold"]) {
classesWithInvalidCoverage.push(testClass["name"]);
}
}
}

if (classesWithInvalidCoverage.length == 0) {
test_result.message = `${test_report_json.summary.passing} Tests passed with overall Test Run Coverage of ${test_report_json.summary.testRunCoverage}`;
test_result.result = true;
} else {
test_result.message=`The test classes ${classesWithInvalidCoverage.toString()} do not meet the required code coverage of ${this.test_options["coverageThreshold"]}`;
test_result.result = false;
}
console.log(output);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/sfpowerscripts-cli/messages/trigger_apex_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"synchronousFlagDescription": "Select an option if the tests are to be run synchronously",
"specifiedTestsFlagDescription": "comma-separated list of Apex test class names or IDs and, if applicable, test methods to run",
"apexTestSuiteFlagDescription": "comma-separated list of Apex test suite names to run",
"validateCoverageFlagDescription": "Enable code coverage validation for individual test classes",
"coveragePercentFlagDescription": "Minimum coverage percentage required for each test class",
"waitTimeFlagDescription": "wait time for command to finish in minutes"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default class TriggerApexTest extends SfdxCommand {
synchronous: flags.boolean({char: 's', description: messages.getMessage('synchronousFlagDescription')}),
specifiedtests: flags.string({description: messages.getMessage('specifiedTestsFlagDescription')}),
apextestsuite: flags.string({description: messages.getMessage('apexTestSuiteFlagDescription')}),
validatecoverage: flags.boolean({char: 'c', description: messages.getMessage('validateCoverageFlagDescription')}),
coveragepercent: flags.integer({char: 'p', description: messages.getMessage('coveragePercentFlagDescription'), dependsOn: ['validatecoverage'], default: 75}),
waittime: flags.string({description: messages.getMessage('waitTimeFlagDescription'), default: '60'})
};

Expand All @@ -38,6 +40,8 @@ export default class TriggerApexTest extends SfdxCommand {
test_options["wait_time"] = this.flags.waittime;
test_options["testlevel"] = this.flags.testlevel;
test_options["synchronous"] = this.flags.synchronous;
test_options["isValidateCoverage"] = this.flags.validatecoverage;
test_options["coverageThreshold"] = this.flags.coveragepercent;

if (test_options["testlevel"] == "RunSpecifiedTests")
test_options["specified_tests"] = this.flags.specifiedtests;
Expand All @@ -63,7 +67,7 @@ export default class TriggerApexTest extends SfdxCommand {
}
} catch(err) {
// AppInsights.trackExcepiton("sfpwowerscript-triggerapextest-task",err);
console.log(err);
console.error(err);
// Fail the task when an error occurs
process.exit(1);
}
Expand Down

0 comments on commit ababa35

Please sign in to comment.