From 65b867abd3f49b6af723fca233ee5bb78826f631 Mon Sep 17 00:00:00 2001 From: Johannes Feichtner <343448+Churro@users.noreply.github.com> Date: Tue, 17 Sep 2024 11:44:51 +0200 Subject: [PATCH] refactor(gradle-wrapper): simplify utils and related tests (#31432) --- .../manager/gradle-wrapper/util.spec.ts | 70 ++++++++++--------- lib/modules/manager/gradle-wrapper/utils.ts | 52 +++++++------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/lib/modules/manager/gradle-wrapper/util.spec.ts b/lib/modules/manager/gradle-wrapper/util.spec.ts index 5396da52fdf46e..63d730e59fe509 100644 --- a/lib/modules/manager/gradle-wrapper/util.spec.ts +++ b/lib/modules/manager/gradle-wrapper/util.spec.ts @@ -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, @@ -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 () => { @@ -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', + }); }); }); diff --git a/lib/modules/manager/gradle-wrapper/utils.ts b/lib/modules/manager/gradle-wrapper/utils.ts index 9ebdd68b94e2e6..04bc970ad23f92 100644 --- a/lib/modules/manager/gradle-wrapper/utils.ts +++ b/lib/modules/manager/gradle-wrapper/utils.ts @@ -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'; @@ -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'; } @@ -101,22 +103,18 @@ export async function getJvmConfiguration( // https://regex101.com/r/IcOs7P/1 const DISTRIBUTION_URL_REGEX = regEx( '^(?:distributionUrl\\s*=\\s*)(?\\S*-(?\\d+\\.\\d+(?:\\.\\d+)?(?:-\\w+)*)-(?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',