|
| 1 | +import tl = require('azure-pipelines-task-lib/task'); |
| 2 | +import * as path from 'path'; |
| 3 | +import constants = require('./constants'); |
| 4 | + |
| 5 | +function publish(testRunner, resultFiles, mergeResults, failTaskOnFailedTests, platform, publishRunAttachments, testRunSystem , failTaskOnFailureToPublishResults, listOfAutomatedTestPoints) { |
| 6 | + var properties = <{ [key: string]: string }>{}; |
| 7 | + properties['type'] = testRunner; |
| 8 | + properties['mergeResults'] = mergeResults; |
| 9 | + properties['platform'] = platform; |
| 10 | + properties['publishRunAttachments'] = publishRunAttachments; |
| 11 | + properties['resultFiles'] = resultFiles; |
| 12 | + properties['failTaskOnFailedTests'] = failTaskOnFailedTests; |
| 13 | + properties['failTaskOnFailureToPublishResults'] = failTaskOnFailureToPublishResults; |
| 14 | + properties['testRunSystem'] = testRunSystem; |
| 15 | + properties['listOfAutomatedTestPoints'] = listOfAutomatedTestPoints; |
| 16 | + properties['testPlanId'] = tl.getVariable('TestPlanId'); |
| 17 | + |
| 18 | + tl.command('results.publish', properties, ''); |
| 19 | +} |
| 20 | + |
| 21 | +export async function publishAutomatedTestResult(listOfAutomatedTestPoints: string) { |
| 22 | + try{ |
| 23 | + const testRunner = "JUnit"; |
| 24 | + const testResultsFiles: string[] = ["**/TEST-*.xml"]; |
| 25 | + const mergeResults = tl.getInput('mergeTestResults'); |
| 26 | + const platform = "any cpu"; |
| 27 | + const publishRunAttachments = tl.getInput('publishRunAttachments'); |
| 28 | + const failTaskOnFailedTests = tl.getInput('failTaskOnFailedTests'); |
| 29 | + const failTaskOnMissingResultsFile: boolean = tl.getBoolInput('failTaskOnMissingResultsFile'); |
| 30 | + const failTaskOnFailureToPublishResults = tl.getInput('failTaskOnFailureToPublishResults'); |
| 31 | + const testRunSystem = "AzureTestPlan : " + tl.getInput("testLanguageInput"); |
| 32 | + |
| 33 | + let searchFolder = tl.getVariable('System.DefaultWorkingDirectory'); |
| 34 | + |
| 35 | + tl.debug('testRunner: ' + testRunner); |
| 36 | + tl.debug('testResultsFiles: ' + testResultsFiles); |
| 37 | + tl.debug('mergeResults: ' + mergeResults); |
| 38 | + tl.debug('platform: ' + platform); |
| 39 | + tl.debug('publishRunAttachments: ' + publishRunAttachments); |
| 40 | + tl.debug('failTaskOnFailedTests: ' + failTaskOnFailedTests); |
| 41 | + tl.debug('failTaskOnMissingResultsFile: ' + failTaskOnMissingResultsFile); |
| 42 | + tl.debug('failTaskOnFailureToPublishResults: ' + failTaskOnFailureToPublishResults); |
| 43 | + |
| 44 | + if(tl.getVariable('System.DefaultWorkingDirectory') && (!path.isAbsolute(searchFolder))) |
| 45 | + { |
| 46 | + searchFolder = path.join(tl.getVariable('System.DefaultWorkingDirectory'),searchFolder); |
| 47 | + } |
| 48 | + |
| 49 | + const findOptions = <tl.FindOptions>{ |
| 50 | + allowBrokenSymbolicLinks: true, |
| 51 | + followSpecifiedSymbolicLink: true, |
| 52 | + followSymbolicLinks: true |
| 53 | + }; |
| 54 | + |
| 55 | + const matchingTestResultsFiles = tl.findMatch(searchFolder, testResultsFiles, findOptions); |
| 56 | + |
| 57 | + const testResultsFilesCount = matchingTestResultsFiles ? matchingTestResultsFiles.length : 0; |
| 58 | + |
| 59 | + tl.debug(`Detected ${testResultsFilesCount} test result files`); |
| 60 | + |
| 61 | + const forceMerge = testResultsFilesCount > constants.MERGE_THRESHOLD; |
| 62 | + if (forceMerge) { |
| 63 | + tl.debug('Detected large number of test result files. Merged all of them into a single file and published a single test run to optimize for test result publish performance instead of publishing hundreds of test runs'); |
| 64 | + } |
| 65 | + |
| 66 | + if (testResultsFilesCount === 0) { |
| 67 | + if (failTaskOnMissingResultsFile) { |
| 68 | + tl.setResult(tl.TaskResult.Failed, tl.loc('NoMatchingFilesFound', testResultsFiles)); |
| 69 | + } else { |
| 70 | + tl.warning(tl.loc('NoMatchingFilesFound', testResultsFiles)); |
| 71 | + } |
| 72 | + } else { |
| 73 | + publish(testRunner, matchingTestResultsFiles, |
| 74 | + forceMerge ? true.toString() : mergeResults, |
| 75 | + failTaskOnFailedTests, |
| 76 | + platform, |
| 77 | + publishRunAttachments, |
| 78 | + testRunSystem, |
| 79 | + failTaskOnFailureToPublishResults, |
| 80 | + listOfAutomatedTestPoints); |
| 81 | + } |
| 82 | + } catch (err) { |
| 83 | + tl.setResult(tl.TaskResult.Failed, err); |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments