Skip to content

Commit

Permalink
[GradleV3] Add getGradleVersion method (#18927)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-zaytsev committed Oct 23, 2023
1 parent a139d34 commit c5e3be9
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 28 deletions.
1 change: 1 addition & 0 deletions Tasks/GradleV3/Modules/code-coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function enableCodeCoverageAsync(settings: ICodeCoverageSettings): Q.Prom
buildProperties['reportdirectory'] = settings.reportDirectoryName;
buildProperties['ismultimodule'] = String(settings.isMultiModule);
buildProperties['gradle5xOrHigher'] = String(settings.gradle5xOrHigher);
buildProperties['gradleVersion'] = settings.gradleVersion;

const codeCoverageEnabler: ICodeCoverageEnabler = new CodeCoverageEnablerFactory().getTool('gradle', settings.codeCoverageTool.toLowerCase());
return codeCoverageEnabler.enableCodeCoverage(buildProperties);
Expand Down
29 changes: 28 additions & 1 deletion Tasks/GradleV3/Modules/environment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as tl from 'azure-pipelines-task-lib/task';
import * as javaCommon from 'azure-pipelines-tasks-java-common/java-common';
import { IExecOptions } from 'azure-pipelines-task-lib/toolrunner';
import { IExecOptions, ToolRunner } from 'azure-pipelines-task-lib/toolrunner';

// Setting the access token env var to both VSTS and AZURE_ARTIFACTS for
// backwards compatibility with repos that already use the older env var.
Expand Down Expand Up @@ -88,3 +88,30 @@ export function setGradleOpts(gradleOptions: string): void {
tl.debug(`GRADLE_OPTS is now set to ${gradleOptions}`);
}
}

/**
* Determine Gradle version by running ./gradlew --version
* @param {string} wrapperScript - Relative path from the repository root to the Gradle Wrapper script.
* @returns {string} Gradle version
*/
export function getGradleVersion(wrapperScript: string): string {
const gradleVersionRunner: ToolRunner = tl.tool(wrapperScript);
gradleVersionRunner.arg('--version');

const gradleOutput: string = gradleVersionRunner.execSync().stdout;
const gradleVersion: string = extractGradleVersion(gradleOutput);

if (gradleVersion === 'unknown'){
tl.warning(tl.loc('UnableToExtractGradleVersion'));
}

tl.debug(`Gradle version: ${gradleVersion}`);

return gradleVersion;
}

export function extractGradleVersion(str: string): string {
const regex = /^Gradle (?<version>\d+\.\d+(?:\.\d+)?.*$)/m;
const match = str.match(regex);
return match?.groups?.version || 'unknown';
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@
"loc.messages.FileNotFound": "File or folder doesn't exist: %s",
"loc.messages.FailedToAppendCC": "Unable to append code coverage data: %s",
"loc.messages.NoTestResults": "No test result files matching %s were found, so publishing JUnit test results is being skipped.",
"loc.messages.chmodGradlew": "Used 'chmod' method for gradlew file to make it executable."
"loc.messages.chmodGradlew": "Used 'chmod' method for gradlew file to make it executable.",
"loc.messages.UnableToExtractGradleVersion": "Unable to extract Gradle version from gradle output."
}
27 changes: 23 additions & 4 deletions Tasks/GradleV3/Tests/L0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { PmdTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/PmdToo
import { CheckstyleTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/CheckstyleTool';
import { FindbugsTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/FindbugsTool';
import { AnalysisResult } from 'azure-pipelines-tasks-codeanalysis-common/Common/AnalysisResult';
import { extractGradleVersion } from '../Modules/environment';

let isWindows: RegExpMatchArray = os.type().match(/^Win/);
let gradleWrapper: string = isWindows ? 'gradlew.bat' : 'gradlew';
Expand Down Expand Up @@ -767,7 +768,7 @@ describe('Gradle L0 Suite', function () {
tr.run();

assert(tr.succeeded, 'task should have succeeded');
assert(tr.invokedToolCount === 2, 'should have only run gradle 2 times');
assert(tr.invokedToolCount === 3, 'should have only run gradle 3 times');
assert(tr.stderr.length === 0, 'should not have written to stderr');
assert(tr.ran(gradleWrapper + ` properties`), 'should have run Gradle with properties');
assert(tr.ran(gradleWrapper + ` clean build jacocoTestReport`), 'should have run Gradle with code coverage');
Expand All @@ -794,7 +795,7 @@ describe('Gradle L0 Suite', function () {
tr.run();

assert(tr.failed, 'task should have failed');
assert(tr.invokedToolCount === 2, 'should have only run gradle 2 times');
assert(tr.invokedToolCount === 3, 'should have only run gradle 3 times');
assert(tr.stderr.length === 0, 'should not have written to stderr');
assert(tr.stdout.indexOf('loc_mock_NoCodeCoverage') > -1, 'should have given an error message');
assert(tr.ran(gradleWrapper + ` properties`), 'should have run Gradle with properties');
Expand All @@ -820,7 +821,7 @@ describe('Gradle L0 Suite', function () {
tr.run();

assert(tr.succeeded, 'task should have succeeded');
assert(tr.invokedToolCount === 2, 'should have only run gradle 2 times');
assert(tr.invokedToolCount === 3, 'should have only run gradle 3 times');
assert(tr.stderr.length === 0, 'should not have written to stderr');
assert(tr.ran(`${gradleWrapper} properties`), 'should have run Gradle with properties');
assert(tr.ran(`${gradleWrapper} clean build jacocoTestReport`), 'should have run Gradle with code coverage');
Expand All @@ -846,7 +847,7 @@ describe('Gradle L0 Suite', function () {
tr.run();

assert(tr.succeeded, 'task should have succeeded');
assert(tr.invokedToolCount === 2, 'should have only run gradle 2 times');
assert(tr.invokedToolCount === 3, 'should have only run gradle 3 times');
assert(tr.stderr.length === 0, 'should not have written to stderr');
assert(tr.ran(`${gradleWrapper} properties`), 'should have run Gradle with properties');
assert(tr.ran(`${gradleWrapper} clean build jacocoTestReport`), 'should have run Gradle with code coverage');
Expand Down Expand Up @@ -924,4 +925,22 @@ describe('Gradle L0 Suite', function () {
});
// /* END Tools tests */

it('extractGradleVersion returns correct results', (done) => {
const log1: string = 'Gradle 4.0.1';
const log2: string = 'Gradle 4.0';
const log3: string = 'Gradle 3.5-rc-2';
const log4: string = 'Gradle 8.5-20230916222118+0000';
const log5: string = 'Gradle 8.5-20230916222118-0000';
const log6: string = 'Gradle 8.4-branch-ljacomet_kotlin_kotlin_1_9_10-20230901164331+0000'
const log7: string = '';
assert(extractGradleVersion(log1) === '4.0.1', 'extractGradleVersion should return 4.0.1');
assert(extractGradleVersion(log2) === '4.0', 'extractGradleVersion should return 4.0');
assert(extractGradleVersion(log3) === '3.5-rc-2', 'extractGradleVersion should return 3.5-rc-2');
assert(extractGradleVersion(log4) === '8.5-20230916222118+0000', 'extractGradleVersion should return 8.5-20230916222118+0000');
assert(extractGradleVersion(log5) === '8.5-20230916222118-0000', 'extractGradleVersion should return 8.5-20230916222118-0000');
assert(extractGradleVersion(log6) === '8.4-branch-ljacomet_kotlin_kotlin_1_9_10-20230901164331+0000', 'extractGradleVersion should return 8.4-branch-ljacomet_kotlin_kotlin_1_9_10-20230901164331+0000');
assert(extractGradleVersion(log7) === 'unknown', 'extractGradleVersion should return unknown');

done();
});
});
8 changes: 8 additions & 0 deletions Tasks/GradleV3/Tests/L0JacocoGradle4x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
'/home/repo/src': true
},
'exec': {
'gradlew.bat --version': {
'code': 0,
'stdout': 'More sample gradle output'
},
'gradlew --version': {
'code': 0,
'stdout': 'More sample gradle output'
},
'gradlew.bat properties': {
'code': 0,
'stdout': 'More sample gradle output'
Expand Down
8 changes: 8 additions & 0 deletions Tasks/GradleV3/Tests/L0JacocoGradle5x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ let a: ma.TaskLibAnswers = <ma.TaskLibAnswers>{
'/home/repo/src': true
},
'exec': {
'gradlew.bat --version': {
'code': 0,
'stdout': 'More sample gradle output'
},
'gradlew --version': {
'code': 0,
'stdout': 'More sample gradle output'
},
'gradlew.bat properties': {
'code': 0,
'stdout': 'More sample gradle output'
Expand Down
11 changes: 9 additions & 2 deletions Tasks/GradleV3/gradletask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { CheckstyleTool } from 'azure-pipelines-tasks-codeanalysis-common/Common
import { FindbugsTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/FindbugsTool';
import { SpotbugsTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/SpotbugsTool';
import { IAnalysisTool } from 'azure-pipelines-tasks-codeanalysis-common/Common/IAnalysisTool';
import { emitTelemetry } from 'azure-pipelines-tasks-utility-common/telemetry';
import { ToolRunner } from 'azure-pipelines-task-lib/toolrunner';
import { getExecOptions, setJavaHome, setGradleOpts } from './Modules/environment';
import { getExecOptions, setJavaHome, setGradleOpts, getGradleVersion } from './Modules/environment';
import { configureWrapperScript, isMultiModuleProject } from './Modules/project-configuration';
import { enableCodeCoverageAsync, publishTestResults, publishCodeCoverageResultsAsync, resolveCodeCoveragePreset } from './Modules/code-coverage';
import { ICodeAnalysisResult, ICodeCoveragePreset, ICodeCoverageSettings, IPublishCodeCoverageSettings, ITaskResult } from './interfaces';
Expand Down Expand Up @@ -75,6 +76,9 @@ async function run() {
reportingTaskName = codeCoveragePreset.reportingTaskName;
// END: determine isMultiModule

// Determine gradle version
const gradleVersion: string = getGradleVersion(wrapperScript);

// Clean the report directory before enabling code coverage
tl.rmRF(reportDirectory);

Expand All @@ -88,9 +92,12 @@ async function run() {
reportDirectoryName: reportDirectoryName,
summaryFileName: codeCoveragePreset.summaryFileName,
isMultiModule: isMultiModule,
gradle5xOrHigher: gradle5xOrHigher
gradle5xOrHigher: gradle5xOrHigher,
gradleVersion: gradleVersion
};

emitTelemetry('TaskHub', 'GradleV3', { codeCoverageSettings: codeCoverageSettings });

await enableCodeCoverageAsync(codeCoverageSettings);
}

Expand Down
1 change: 1 addition & 0 deletions Tasks/GradleV3/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface ICodeCoverageSettings {
summaryFileName: string;
isMultiModule: boolean;
gradle5xOrHigher: boolean;
gradleVersion: string;
}

export interface IPublishCodeCoverageSettings {
Expand Down
Loading

0 comments on commit c5e3be9

Please sign in to comment.