Skip to content

Commit

Permalink
refactor(gradle-wrapper): simplify utils and related tests (#31432)
Browse files Browse the repository at this point in the history
  • Loading branch information
Churro authored Sep 17, 2024
1 parent edad9f1 commit 65b867a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
70 changes: 36 additions & 34 deletions lib/modules/manager/gradle-wrapper/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Stats } from 'node:fs';
import os from 'node:os';
import { codeBlock } from 'common-tags';
import { fs, partial } from '../../../../test/util';
import { GlobalConfig } from '../../../config/global';
import {
extractGradleVersion,
getJavaConstraint,
Expand All @@ -15,37 +14,25 @@ const platform = jest.spyOn(os, 'platform');
jest.mock('../../../util/fs');

describe('modules/manager/gradle-wrapper/util', () => {
beforeEach(() => GlobalConfig.reset());

describe('getJavaConstraint()', () => {
it('return ^8.0.0 for global mode', async () => {
expect(await getJavaConstraint('4', '')).toBe('^8.0.0');
});

it('return ^11.0.0 for docker mode and undefined gradle', async () => {
GlobalConfig.set({ binarySource: 'docker' });
expect(await getJavaConstraint('', '')).toBe('^11.0.0');
});

it('return ^8.0.0 for docker gradle < 5', async () => {
GlobalConfig.set({ binarySource: 'docker' });
expect(await getJavaConstraint('4.9', '')).toBe('^8.0.0');
});

it('return ^11.0.0 for docker gradle >=5 && <7', async () => {
GlobalConfig.set({ binarySource: 'docker' });
expect(await getJavaConstraint('6.0', '')).toBe('^11.0.0');
});

it('return ^16.0.0 for docker gradle >= 7', async () => {
GlobalConfig.set({ binarySource: 'docker' });
expect(await getJavaConstraint('7.0.1', '')).toBe('^16.0.0');
});

it('return ^17.0.0 for docker gradle >= 7.3', async () => {
GlobalConfig.set({ binarySource: 'docker' });
expect(await getJavaConstraint('7.3.0', '')).toBe('^17.0.0');
expect(await getJavaConstraint('8.0.1', '')).toBe('^17.0.0');
describe('returns Java constraint based on gradle support', () => {
it.each`
gradleVersion | javaConstraint
${''} | ${'^11.0.0'}
${'4'} | ${'^8.0.0'}
${'4.9'} | ${'^8.0.0'}
${'6.0'} | ${'^11.0.0'}
${'7.0.1'} | ${'^16.0.0'}
${'7.3.0'} | ${'^17.0.0'}
${'8.0.1'} | ${'^17.0.0'}
`(
'$gradleVersion | $javaConstraint',
async ({ gradleVersion, javaConstraint }) => {
expect(await getJavaConstraint(gradleVersion, '')).toBe(
javaConstraint,
);
},
);
});

it('returns toolChainVersion constraint if daemon JVM configured', async () => {
Expand Down Expand Up @@ -79,9 +66,24 @@ describe('modules/manager/gradle-wrapper/util', () => {
});

describe('extractGradleVersion()', () => {
it('works for undefined', () => {
// TODO #22198
expect(extractGradleVersion(undefined as never)).toBeNull();
it('returns null', () => {
const properties = codeBlock`
distributionSha256Sum=038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768
zipStoreBase=GRADLE_USER_HOME
`;
expect(extractGradleVersion(properties)).toBeNull();
});

it('returns gradle version', () => {
const properties = codeBlock`
distributionSha256Sum=038794feef1f4745c6347107b6726279d1c824f3fc634b60f86ace1e9fbd1768
distributionUrl=https\\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
`;
expect(extractGradleVersion(properties)).toStrictEqual({
url: 'https\\://services.gradle.org/distributions/gradle-6.3-bin.zip',
version: '6.3',
});
});
});

Expand Down
52 changes: 25 additions & 27 deletions lib/modules/manager/gradle-wrapper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { dirname, join } from 'upath';
import { GlobalConfig } from '../../../config/global';
import { logger } from '../../../logger';
import { chmodLocalFile, readLocalFile, statLocalFile } from '../../../util/fs';
import { newlineRegex, regEx } from '../../../util/regex';
import { regEx } from '../../../util/regex';
import gradleVersioning from '../../versioning/gradle';
import type { GradleVersionExtract } from './types';

Expand Down Expand Up @@ -52,24 +52,26 @@ export async function getJavaConstraint(
const major = gradleVersion ? gradleVersioning.getMajor(gradleVersion) : null;
const minor = gradleVersion ? gradleVersioning.getMinor(gradleVersion) : null;

// https://docs.gradle.org/8.8/release-notes.html#daemon-toolchains
if (major && (major > 8 || (major === 8 && minor && minor >= 8))) {
const toolChainVersion = await getJvmConfiguration(gradlewFile);
if (toolChainVersion) {
return `^${toolChainVersion}.0.0`;
if (major) {
// https://docs.gradle.org/8.8/release-notes.html#daemon-toolchains
if (major > 8 || (major === 8 && minor && minor >= 8)) {
const toolChainVersion = await getJvmConfiguration(gradlewFile);
if (toolChainVersion) {
return `^${toolChainVersion}.0.0`;
}
}
if (major > 7 || (major === 7 && minor && minor >= 3)) {
return '^17.0.0';
}
if (major === 7) {
return '^16.0.0';
}
// first public gradle version was 2.0
if (major > 0 && major < 5) {
return '^8.0.0';
}
}

if (major && (major > 7 || (major >= 7 && minor && minor >= 3))) {
return '^17.0.0';
}
if (major && major >= 7) {
return '^16.0.0';
}
// first public gradle version was 2.0
if (major && major > 0 && major < 5) {
return '^8.0.0';
}
return '^11.0.0';
}

Expand Down Expand Up @@ -101,22 +103,18 @@ export async function getJvmConfiguration(
// https://regex101.com/r/IcOs7P/1
const DISTRIBUTION_URL_REGEX = regEx(
'^(?:distributionUrl\\s*=\\s*)(?<url>\\S*-(?<version>\\d+\\.\\d+(?:\\.\\d+)?(?:-\\w+)*)-(?<type>bin|all)\\.zip)\\s*$',
'm',
);

export function extractGradleVersion(
fileContent: string,
): GradleVersionExtract | null {
const lines = fileContent?.split(newlineRegex) ?? [];

for (const line of lines) {
const distributionUrlMatch = DISTRIBUTION_URL_REGEX.exec(line);

if (distributionUrlMatch?.groups) {
return {
url: distributionUrlMatch.groups.url,
version: distributionUrlMatch.groups.version,
};
}
const distributionUrlMatch = DISTRIBUTION_URL_REGEX.exec(fileContent);
if (distributionUrlMatch?.groups) {
return {
url: distributionUrlMatch.groups.url,
version: distributionUrlMatch.groups.version,
};
}
logger.debug(
'Gradle wrapper version and url could not be extracted from properties - skipping update',
Expand Down

0 comments on commit 65b867a

Please # to comment.