From b19956f92a7b0c5a2ab752acd686415eac8f3955 Mon Sep 17 00:00:00 2001 From: triptijain2112 <92331194+triptijain2112@users.noreply.github.com> Date: Mon, 11 Sep 2023 10:06:03 +0530 Subject: [PATCH] [PublishTestResultsV2] Add option to fail task if no test result files are found (#18931) * added fail condition if no test file found * Update task.json * Update publishtestresults.ts --- .../resources.resjson/en-US/resources.resjson | 2 ++ Tasks/PublishTestResultsV2/publishtestresults.ts | 9 ++++++++- Tasks/PublishTestResultsV2/task.json | 14 +++++++++++--- Tasks/PublishTestResultsV2/task.loc.json | 12 ++++++++++-- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Tasks/PublishTestResultsV2/Strings/resources.resjson/en-US/resources.resjson b/Tasks/PublishTestResultsV2/Strings/resources.resjson/en-US/resources.resjson index 6052d99dd5dc..3dc5ca1c52f7 100644 --- a/Tasks/PublishTestResultsV2/Strings/resources.resjson/en-US/resources.resjson +++ b/Tasks/PublishTestResultsV2/Strings/resources.resjson/en-US/resources.resjson @@ -15,6 +15,8 @@ "loc.input.help.mergeTestResults": "A test run is created for each results file. Check this option to merge results into a single test run. To optimize for better performance, results will be merged into a single run if there are more than 100 result files, irrespective of this option.", "loc.input.label.failTaskOnFailedTests": "Fail if there are test failures", "loc.input.help.failTaskOnFailedTests": "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.", + "loc.input.label.failTaskOnMissingResultsFile": "ms-resource:loc.input.label.failTaskOnMissingResultsFile", + "loc.input.help.failTaskOnMissingResultsFile": "ms-resource:loc.input.help.failTaskOnMissingResultsFile", "loc.input.label.testRunTitle": "Test run title", "loc.input.help.testRunTitle": "Provide a name for the Test Run.", "loc.input.label.platform": "Build Platform", diff --git a/Tasks/PublishTestResultsV2/publishtestresults.ts b/Tasks/PublishTestResultsV2/publishtestresults.ts index 5b2d9209ca58..9fe7cfee3a15 100644 --- a/Tasks/PublishTestResultsV2/publishtestresults.ts +++ b/Tasks/PublishTestResultsV2/publishtestresults.ts @@ -72,6 +72,7 @@ async function run() { const testRunTitle = tl.getInput('testRunTitle'); const publishRunAttachments = tl.getInput('publishRunAttachments'); const failTaskOnFailedTests = tl.getInput('failTaskOnFailedTests'); + const failTaskOnMissingResultsFile: boolean = tl.getBoolInput('failTaskOnMissingResultsFile'); let searchFolder = tl.getInput('searchFolder'); tl.debug('testRunner: ' + testRunner); @@ -82,6 +83,7 @@ async function run() { tl.debug('testRunTitle: ' + testRunTitle); tl.debug('publishRunAttachments: ' + publishRunAttachments); tl.debug('failTaskOnFailedTests: ' + failTaskOnFailedTests); + tl.debug('failTaskOnMissingResultsFile: ' + failTaskOnMissingResultsFile); if (isNullOrWhitespace(searchFolder)) { searchFolder = tl.getVariable('System.DefaultWorkingDirectory'); @@ -111,6 +113,7 @@ async function run() { ci.addToConsolidatedCi('config', config); ci.addToConsolidatedCi('platform', platform); ci.addToConsolidatedCi('testResultsFilesCount', testResultsFilesCount); + ci.addToConsolidatedCi('failTaskOnMissingResultsFile', failTaskOnMissingResultsFile); const dotnetVersion = getDotNetVersion(); ci.addToConsolidatedCi('dotnetVersion', dotnetVersion); @@ -121,7 +124,11 @@ async function run() { } if (testResultsFilesCount === 0) { - tl.warning('No test result files matching ' + testResultsFiles + ' were found.'); + if (failTaskOnMissingResultsFile) { + tl.setResult(tl.TaskResult.Failed, tl.loc('NoMatchingFilesFound', testResultsFiles)); + } else { + tl.warning(tl.loc('NoMatchingFilesFound', testResultsFiles)); + } ci.addToConsolidatedCi('noResultsFileFound', true); } else { const osType = tl.osType(); diff --git a/Tasks/PublishTestResultsV2/task.json b/Tasks/PublishTestResultsV2/task.json index 723d439e1d29..4b4cc44b507e 100644 --- a/Tasks/PublishTestResultsV2/task.json +++ b/Tasks/PublishTestResultsV2/task.json @@ -13,8 +13,8 @@ "author": "Microsoft Corporation", "version": { "Major": 2, - "Minor": 216, - "Patch": 2 + "Minor": 228, + "Patch": 0 }, "demands": [], "releaseNotes": "", @@ -81,6 +81,14 @@ "required": false, "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." }, + { + "name": "failTaskOnMissingResultsFile", + "type": "boolean", + "label": "ms-resource:loc.input.label.failTaskOnMissingResultsFile", + "defaultValue": false, + "required": false, + "helpMarkDown": "ms-resource:loc.input.help.failTaskOnMissingResultsFile" + }, { "name": "testRunTitle", "type": "string", @@ -139,4 +147,4 @@ "ErrorTestResultsPublisher": "Error while executing TestResultsPublisher: %s.", "ErrorFailTaskOnFailedTests": "There are one or more test failures detected in result files. Detailed summary of published test results can be viewed in the Tests tab." } -} \ No newline at end of file +} diff --git a/Tasks/PublishTestResultsV2/task.loc.json b/Tasks/PublishTestResultsV2/task.loc.json index 5428c5b55f7f..8889ddf91fb0 100644 --- a/Tasks/PublishTestResultsV2/task.loc.json +++ b/Tasks/PublishTestResultsV2/task.loc.json @@ -13,8 +13,8 @@ "author": "Microsoft Corporation", "version": { "Major": 2, - "Minor": 216, - "Patch": 2 + "Minor": 228, + "Patch": 0 }, "demands": [], "releaseNotes": "ms-resource:loc.releaseNotes", @@ -81,6 +81,14 @@ "required": false, "helpMarkDown": "ms-resource:loc.input.help.failTaskOnFailedTests" }, + { + "name": "failTaskOnMissingResultsFile", + "type": "boolean", + "label": "ms-resource:loc.input.label.failTaskOnMissingResultsFile", + "defaultValue": false, + "required": false, + "helpMarkDown": "ms-resource:loc.input.help.failTaskOnMissingResultsFile" + }, { "name": "testRunTitle", "type": "string",