Skip to content

Commit a7b3216

Browse files
authored
Handle failTaskOnFailedTests option - Azure Test Plan (#20712)
* Gradle - do not fail here if test fails, we fail from publish test results * Maven - do not fail due to test fail here, we do that from publish test results * Test task should fail on test failures by default. * Gen * default value to be true. * added comment * documentation link * send back error through call back function.
1 parent 1444497 commit a7b3216

File tree

13 files changed

+125
-40
lines changed

13 files changed

+125
-40
lines changed

Tasks/AzureTestPlanV0/Invokers/maveninvoker.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ export async function executeMavenTests(testsToBeExecuted: string[], pomFilePath
3333
args.push(pomFilePath);
3434
}
3535

36+
//for returning success exit code incase of test failure and later we detect test failure from PTR command, documentation: https://maven.apache.org/surefire/maven-failsafe-plugin/verify-mojo.html, https://maven.apache.org/archives/maven-1.x/plugins/test/announcements/announcement-1.8.txt
37+
args.push('-Dmaven.test.failure.ignore=true');
38+
3639
tl.debug("Executing java maven tests with executable : " + executable);
3740
tl.debug("Executing java maven tests with args :" + args);
3841

3942
let status = await execMavenBuild(args);
4043

4144
return status ?? 1;
42-
}
45+
}

Tasks/AzureTestPlanV0/task.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 0,
16-
"Minor": 248,
16+
"Minor": 250,
1717
"Patch": 0
1818
},
1919
"preview": true,
@@ -123,7 +123,7 @@
123123
"name": "failTaskOnFailedTests",
124124
"type": "boolean",
125125
"label": "Fail if there are test failures",
126-
"defaultValue": "false",
126+
"defaultValue": "true",
127127
"required": false,
128128
"helpMarkDown": "Fail the task if there are any test failures. Check this option to fail the task if test failures are detected in the result files."
129129
},

Tasks/AzureTestPlanV0/task.loc.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 0,
16-
"Minor": 248,
16+
"Minor": 250,
1717
"Patch": 0
1818
},
1919
"preview": true,
@@ -123,7 +123,7 @@
123123
"name": "failTaskOnFailedTests",
124124
"type": "boolean",
125125
"label": "ms-resource:loc.input.label.failTaskOnFailedTests",
126-
"defaultValue": "false",
126+
"defaultValue": "true",
127127
"required": false,
128128
"helpMarkDown": "ms-resource:loc.input.help.failTaskOnFailedTests"
129129
},

Tasks/AzureTestPlanV0/testLibExecutor.ts

+29-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs = require('fs');
44
import * as path from 'path';
55
import * as tl from 'azure-pipelines-task-lib/task';
66
import * as tr from 'azure-pipelines-task-lib/toolrunner';
7+
import { Writable } from 'stream';
78

89
var isWindows = os.type().match(/^Win/);
910

@@ -40,11 +41,29 @@ function getMavenExec() {
4041
return mvnExec;
4142
}
4243

43-
function getExecOptions(): tr.IExecOptions {
44+
function getExecOptions(output?: { stdout: string}): tr.IExecOptions {
4445
var env = process.env;
45-
return <tr.IExecOptions>{
46+
47+
var execOptions: tr.IExecOptions = output
48+
? {
49+
env: env,
50+
outStream: new Writable({
51+
write(chunk, encoding, callback) {
52+
try {
53+
output.stdout += chunk.toString();
54+
process.stdout.write(chunk);
55+
callback();
56+
} catch (error) {
57+
callback(error);
58+
}
59+
},
60+
}),
61+
}
62+
: {
4663
env: env,
4764
};
65+
66+
return execOptions;
4867
}
4968

5069
/**Maven orchestration occurs as follows:
@@ -149,12 +168,17 @@ export async function execGradleBuild(args: string[]): Promise<number> {
149168
gradleRunner.arg('clean');
150169
gradleRunner.arg(args);
151170

171+
let runnerOutput = { stdout: ''};
172+
152173
try {
153-
await gradleRunner.exec(getExecOptions());
174+
await gradleRunner.exec(getExecOptions(runnerOutput));
154175
// Gradle build succeeded
155176
return 0; // Return 0 indicating success
156177
} catch (err) {
157-
console.error(err.message);
178+
// we read stdout and return success incase of error due to test failure as later we detect test failure from PTR command
179+
if (runnerOutput.stdout.includes('There were failing tests')) {
180+
return 0;
181+
}
158182
tl.setResult(tl.TaskResult.Failed, "Build failed."); // Set the step result to Failed
159183
return 1; // Return 1 indicating failure
160184
}
@@ -170,4 +194,4 @@ export async function executeJestCommand(jestPath: string, argument: string): Pr
170194
let jest: tr.ToolRunner = tl.tool(jestPath);
171195
jest.line(argument);
172196
return await jest.exec(<tr.IExecOptions>{ cwd: "" });
173-
}
197+
}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Default|0.248.0
2-
Node20-225|0.248.1
1+
Default|0.250.0
2+
Node20-225|0.250.1

_generated/AzureTestPlanV0/Invokers/maveninvoker.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ export async function executeMavenTests(testsToBeExecuted: string[], pomFilePath
3333
args.push(pomFilePath);
3434
}
3535

36+
//for returning success exit code incase of test failure and later we detect test failure from PTR command, documentation: https://maven.apache.org/surefire/maven-failsafe-plugin/verify-mojo.html, https://maven.apache.org/archives/maven-1.x/plugins/test/announcements/announcement-1.8.txt
37+
args.push('-Dmaven.test.failure.ignore=true');
38+
3639
tl.debug("Executing java maven tests with executable : " + executable);
3740
tl.debug("Executing java maven tests with args :" + args);
3841

3942
let status = await execMavenBuild(args);
4043

4144
return status ?? 1;
42-
}
45+
}

_generated/AzureTestPlanV0/task.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 0,
16-
"Minor": 248,
16+
"Minor": 250,
1717
"Patch": 0
1818
},
1919
"preview": true,
@@ -123,7 +123,7 @@
123123
"name": "failTaskOnFailedTests",
124124
"type": "boolean",
125125
"label": "Fail if there are test failures",
126-
"defaultValue": "false",
126+
"defaultValue": "true",
127127
"required": false,
128128
"helpMarkDown": "Fail the task if there are any test failures. Check this option to fail the task if test failures are detected in the result files."
129129
},
@@ -196,7 +196,8 @@
196196
"MultipleMatchingGradlewFound": "Multiple gradlew files found. Selecting the first matched instance"
197197
},
198198
"_buildConfigMapping": {
199-
"Default": "0.248.0",
200-
"Node20-225": "0.248.1"
199+
"Default": "0.250.0",
200+
"LocalPackages": "0.249.4",
201+
"Node20-225": "0.250.1"
201202
}
202203
}

_generated/AzureTestPlanV0/task.loc.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 0,
16-
"Minor": 248,
16+
"Minor": 250,
1717
"Patch": 0
1818
},
1919
"preview": true,
@@ -123,7 +123,7 @@
123123
"name": "failTaskOnFailedTests",
124124
"type": "boolean",
125125
"label": "ms-resource:loc.input.label.failTaskOnFailedTests",
126-
"defaultValue": "false",
126+
"defaultValue": "true",
127127
"required": false,
128128
"helpMarkDown": "ms-resource:loc.input.help.failTaskOnFailedTests"
129129
},
@@ -196,7 +196,8 @@
196196
"MultipleMatchingGradlewFound": "ms-resource:loc.messages.MultipleMatchingGradlewFound"
197197
},
198198
"_buildConfigMapping": {
199-
"Default": "0.248.0",
200-
"Node20-225": "0.248.1"
199+
"Default": "0.250.0",
200+
"LocalPackages": "0.249.4",
201+
"Node20-225": "0.250.1"
201202
}
202203
}

_generated/AzureTestPlanV0/testLibExecutor.ts

+29-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs = require('fs');
44
import * as path from 'path';
55
import * as tl from 'azure-pipelines-task-lib/task';
66
import * as tr from 'azure-pipelines-task-lib/toolrunner';
7+
import { Writable } from 'stream';
78

89
var isWindows = os.type().match(/^Win/);
910

@@ -40,11 +41,29 @@ function getMavenExec() {
4041
return mvnExec;
4142
}
4243

43-
function getExecOptions(): tr.IExecOptions {
44+
function getExecOptions(output?: { stdout: string}): tr.IExecOptions {
4445
var env = process.env;
45-
return <tr.IExecOptions>{
46+
47+
var execOptions: tr.IExecOptions = output
48+
? {
49+
env: env,
50+
outStream: new Writable({
51+
write(chunk, encoding, callback) {
52+
try {
53+
output.stdout += chunk.toString();
54+
process.stdout.write(chunk);
55+
callback();
56+
} catch (error) {
57+
callback(error);
58+
}
59+
},
60+
}),
61+
}
62+
: {
4663
env: env,
4764
};
65+
66+
return execOptions;
4867
}
4968

5069
/**Maven orchestration occurs as follows:
@@ -149,12 +168,17 @@ export async function execGradleBuild(args: string[]): Promise<number> {
149168
gradleRunner.arg('clean');
150169
gradleRunner.arg(args);
151170

171+
let runnerOutput = { stdout: ''};
172+
152173
try {
153-
await gradleRunner.exec(getExecOptions());
174+
await gradleRunner.exec(getExecOptions(runnerOutput));
154175
// Gradle build succeeded
155176
return 0; // Return 0 indicating success
156177
} catch (err) {
157-
console.error(err.message);
178+
// we read stdout and return success incase of error due to test failure as later we detect test failure from PTR command
179+
if (runnerOutput.stdout.includes('There were failing tests')) {
180+
return 0;
181+
}
158182
tl.setResult(tl.TaskResult.Failed, "Build failed."); // Set the step result to Failed
159183
return 1; // Return 1 indicating failure
160184
}
@@ -170,4 +194,4 @@ export async function executeJestCommand(jestPath: string, argument: string): Pr
170194
let jest: tr.ToolRunner = tl.tool(jestPath);
171195
jest.line(argument);
172196
return await jest.exec(<tr.IExecOptions>{ cwd: "" });
173-
}
197+
}

_generated/AzureTestPlanV0_Node20/Invokers/maveninvoker.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ export async function executeMavenTests(testsToBeExecuted: string[], pomFilePath
3333
args.push(pomFilePath);
3434
}
3535

36+
//for returning success exit code incase of test failure and later we detect test failure from PTR command, documentation: https://maven.apache.org/surefire/maven-failsafe-plugin/verify-mojo.html, https://maven.apache.org/archives/maven-1.x/plugins/test/announcements/announcement-1.8.txt
37+
args.push('-Dmaven.test.failure.ignore=true');
38+
3639
tl.debug("Executing java maven tests with executable : " + executable);
3740
tl.debug("Executing java maven tests with args :" + args);
3841

3942
let status = await execMavenBuild(args);
4043

4144
return status ?? 1;
42-
}
45+
}

_generated/AzureTestPlanV0_Node20/task.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 0,
16-
"Minor": 248,
16+
"Minor": 250,
1717
"Patch": 1
1818
},
1919
"preview": true,
@@ -123,7 +123,7 @@
123123
"name": "failTaskOnFailedTests",
124124
"type": "boolean",
125125
"label": "Fail if there are test failures",
126-
"defaultValue": "false",
126+
"defaultValue": "true",
127127
"required": false,
128128
"helpMarkDown": "Fail the task if there are any test failures. Check this option to fail the task if test failures are detected in the result files."
129129
},
@@ -200,7 +200,8 @@
200200
"MultipleMatchingGradlewFound": "Multiple gradlew files found. Selecting the first matched instance"
201201
},
202202
"_buildConfigMapping": {
203-
"Default": "0.248.0",
204-
"Node20-225": "0.248.1"
203+
"Default": "0.250.0",
204+
"LocalPackages": "0.249.4",
205+
"Node20-225": "0.250.1"
205206
}
206207
}

_generated/AzureTestPlanV0_Node20/task.loc.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "Microsoft Corporation",
1414
"version": {
1515
"Major": 0,
16-
"Minor": 248,
16+
"Minor": 250,
1717
"Patch": 1
1818
},
1919
"preview": true,
@@ -123,7 +123,7 @@
123123
"name": "failTaskOnFailedTests",
124124
"type": "boolean",
125125
"label": "ms-resource:loc.input.label.failTaskOnFailedTests",
126-
"defaultValue": "false",
126+
"defaultValue": "true",
127127
"required": false,
128128
"helpMarkDown": "ms-resource:loc.input.help.failTaskOnFailedTests"
129129
},
@@ -200,7 +200,8 @@
200200
"MultipleMatchingGradlewFound": "ms-resource:loc.messages.MultipleMatchingGradlewFound"
201201
},
202202
"_buildConfigMapping": {
203-
"Default": "0.248.0",
204-
"Node20-225": "0.248.1"
203+
"Default": "0.250.0",
204+
"LocalPackages": "0.249.4",
205+
"Node20-225": "0.250.1"
205206
}
206207
}

0 commit comments

Comments
 (0)