|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fs from 'fs'; |
| 3 | + |
| 4 | +import { TaskMockRunner } from 'azure-pipelines-task-lib/mock-run'; |
| 5 | +import { TaskLibAnswers } from 'azure-pipelines-task-lib/mock-answer'; |
| 6 | + |
| 7 | +import { getTempDir, initializeTest, MavenTaskInputs, setInputs } from './TestUtils'; |
| 8 | + |
| 9 | +const taskPath = path.join(__dirname, '..', 'maventask.js'); |
| 10 | + |
| 11 | +const taskRunner = new TaskMockRunner(taskPath); |
| 12 | + |
| 13 | +// Set environment variable to ENABLE the RemoveDuplicateMavenRun feature flag |
| 14 | +// getPipelineFeature() looks for DISTRIBUTEDTASK_TASKS_<FeatureName> |
| 15 | +process.env['DISTRIBUTEDTASK_TASKS_REMOVEDUPLICATEMAVENRUN'] = 'true'; |
| 16 | + |
| 17 | +// Common initial setup |
| 18 | +initializeTest(taskRunner); |
| 19 | + |
| 20 | +// Set Inputs |
| 21 | +const inputs: MavenTaskInputs = { |
| 22 | + mavenVersionSelection: 'Default', |
| 23 | + mavenPOMFile: 'pom.xml', |
| 24 | + options: '', |
| 25 | + goals: 'package', |
| 26 | + javaHomeSelection: 'JDKVersion', |
| 27 | + jdkVersion: 'default', |
| 28 | + publishJUnitResults: true, |
| 29 | + testResultsFiles: '**/TEST-*.xml', |
| 30 | + mavenOpts: '-Xmx2048m', |
| 31 | + checkstyleAnalysisEnabled: false, |
| 32 | + pmdAnalysisEnabled: false, |
| 33 | + findbugsAnalysisEnabled: false, |
| 34 | + mavenFeedAuthenticate: true, |
| 35 | + codeCoverageTool: 'JaCoCo', |
| 36 | + restoreOriginalPomXml: true |
| 37 | +}; |
| 38 | +setInputs(taskRunner, inputs); |
| 39 | + |
| 40 | +// Set up environment variables (task-lib does not support mocking getVariable) |
| 41 | +// Env vars in the mock framework must replace '.' with '_' |
| 42 | +delete process.env.M2_HOME; // Remove in case process running this test has it already set |
| 43 | + |
| 44 | +// Provide answers for task mock |
| 45 | +const answers: TaskLibAnswers = { |
| 46 | + which: { |
| 47 | + mvn: '/home/bin/maven/bin/mvn' |
| 48 | + }, |
| 49 | + checkPath: { |
| 50 | + '/home/bin/maven/bin/mvn': true, |
| 51 | + 'pom.xml': true |
| 52 | + }, |
| 53 | + exec: { |
| 54 | + '/home/bin/maven/bin/mvn -version': { |
| 55 | + code: 0, |
| 56 | + stdout: 'Maven version 1.0.0' |
| 57 | + }, |
| 58 | + '/home/bin/maven/bin/mvn -f pom.xml help:effective-pom': { |
| 59 | + code: 0, |
| 60 | + stdout: |
| 61 | + '<Configuration>\r\n<project test>\r\n</project>\r\n</Configuration>\r\nEffective POMs, after inheritance, interpolation, and profiles are applied:\r\n\r\n<!-- ====================================================================== -->\r\n<!-- -->\r\n<!-- Generated by Maven Help Plugin on 2017-06-29T09:43:41 -->\r\n<!-- See: http://maven.apache.org/plugins/maven-help-plugin/ -->\r\n<!-- -->\r\n<!-- ====================================================================== -->\r\n\r\n<!-- ====================================================================== -->\r\n<!-- -->\r\n<!-- Effective POM for project -->\r\n<!-- \'com.microsoft.xplatalm:xplatalmApp:jar:1.0-SNAPSHOT\' -->\r\n<!-- -->\r\n<!-- ====================================================================== -->\r\n\r\n<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\r\n</project>\r\n\r\n[INFO] ------------------------------------------------------------------------\r\n[INFO] BUILD SUCCESS\r\n[INFO] ------------------------------------------------------------------------\r\n[INFO] Total time: 0.927 s\r\n[INFO] Finished at: 2017-06-29T09:43:41-04:00\r\n[INFO] Final Memory: 7M/18M\r\n[INFO] ------------------------------------------------------------------------' |
| 62 | + }, |
| 63 | + '/home/bin/maven/bin/mvn -f pom.xml clean verify': { |
| 64 | + code: 0, |
| 65 | + stdout: 'Maven verify done' |
| 66 | + } |
| 67 | + }, |
| 68 | + findMatch: { |
| 69 | + '**/TEST-*.xml': [ |
| 70 | + '/user/build/fun/test-123.xml' |
| 71 | + ] |
| 72 | + }, |
| 73 | + exist: { |
| 74 | + [path.join(getTempDir(), '.mavenInfo')] : true, |
| 75 | + [path.join('CCReport43F6D5EF', 'jacoco.xml')]: true, |
| 76 | + 'CCReportPomA4D283EG.xml': true |
| 77 | + }, |
| 78 | + rmRF: { |
| 79 | + target: { success: true }, |
| 80 | + CCReport43F6D5EF: { success: true }, |
| 81 | + [path.join('CCReport43F6D5EF', "pom.xml")]: { success: true }, |
| 82 | + 'CCReportPomA4D283EG.xml': { success: true } |
| 83 | + } |
| 84 | +}; |
| 85 | +taskRunner.setAnswers(answers); |
| 86 | + |
| 87 | +taskRunner.registerMock('azure-pipelines-tasks-codecoverage-tools/codecoveragefactory', { |
| 88 | + CodeCoverageEnablerFactory: class { |
| 89 | + public getTool(buildTool: string, ccTool: string) { |
| 90 | + if (buildTool.toLowerCase() !== 'maven' || ccTool.toLowerCase() !== 'jacoco') { |
| 91 | + throw new Error(`Should use maven-jacoco but called ${buildTool}-${ccTool}`); |
| 92 | + } |
| 93 | + |
| 94 | + return { |
| 95 | + enableCodeCoverage() { |
| 96 | + console.log('Writing modified pom.xml contents'); |
| 97 | + return Promise.resolve('CCReport43F6D5EF/target/site/jacoco-aggregate'); |
| 98 | + } |
| 99 | + }; |
| 100 | + } |
| 101 | + } |
| 102 | +}); |
| 103 | + |
| 104 | +const originalPomXmlContents = 'original pom.xml contents'; |
| 105 | + |
| 106 | +const fsClone = Object.assign({}, fs); |
| 107 | +Object.assign(fsClone, { |
| 108 | + readFileSync(filename: string, encoding: BufferEncoding ): string { |
| 109 | + if (filename === 'pom.xml' && encoding === 'utf8') { |
| 110 | + console.log('Reading original pom.xml'); |
| 111 | + return originalPomXmlContents; |
| 112 | + } |
| 113 | + |
| 114 | + return fs.readFileSync(filename, encoding); |
| 115 | + }, |
| 116 | + writeFileSync(filename: string, data: any): void { |
| 117 | + if (filename === 'pom.xml') { |
| 118 | + if (data === originalPomXmlContents) { |
| 119 | + console.log('Writing original pom.xml contents'); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + throw new Error(`Trying to write unknown data into pom.xml; data=${data}`); |
| 124 | + } |
| 125 | + |
| 126 | + fs.writeFileSync(filename, data); |
| 127 | + } |
| 128 | +}); |
| 129 | +taskRunner.registerMock('fs', fsClone); |
| 130 | + |
| 131 | +// We only register this mock to prevent import conflicts with already mocked fs |
| 132 | +taskRunner.registerMock('fs-extra', { |
| 133 | + mkdirpSync(dir: string): void { |
| 134 | + // This should never be called |
| 135 | + } |
| 136 | +}); |
| 137 | + |
| 138 | +// Run task |
| 139 | +taskRunner.run(); |
0 commit comments