Skip to content

Commit

Permalink
fix(gradle): gradle/libs.versions.toml should replace version not com…
Browse files Browse the repository at this point in the history
…ment (#15330)
  • Loading branch information
PhilipAbed committed May 11, 2022
1 parent b7732b3 commit dad6a1f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/modules/manager/gradle/__fixtures__/3/libs.versions.toml
@@ -0,0 +1,10 @@
[versions]
# Releases: http://someWebsite.com/junit/1.4.9
mocha-junit-reporter = "2.0.2"
# JUnit 1.4.9 is awesome!
junit = "1.4.9"


[libraries]
junit-legacy = { module = "junit:junit", version.ref = "junit" }
mocha-junit = { module = "mocha-junit:mocha-junit", version.ref = "mocha-junit-reporter" }
42 changes: 42 additions & 0 deletions lib/modules/manager/gradle/extract.spec.ts
Expand Up @@ -505,4 +505,46 @@ describe('modules/manager/gradle/extract', () => {
},
]);
});

it('should change the dependency version not the comment version', async () => {
const tomlFile = loadFixture('3/libs.versions.toml');
const fsMock = {
'gradle/libs.versions.toml': tomlFile,
};
mockFs(fsMock);
const res = await extractAllPackageFiles(
{} as ExtractConfig,
Object.keys(fsMock)
);
expect(res).toMatchObject([
{
packageFile: 'gradle/libs.versions.toml',
datasource: 'maven',
deps: [
{
depName: 'junit:junit',
groupName: 'junit',
currentValue: '1.4.9',
managerData: {
fileReplacePosition: 124,
packageFile: 'gradle/libs.versions.toml',
},
fileReplacePosition: 124,
registryUrls: ['https://repo.maven.apache.org/maven2'],
},
{
depName: 'mocha-junit:mocha-junit',
groupName: 'mocha-junit-reporter',
currentValue: '2.0.2',
managerData: {
fileReplacePosition: 82,
packageFile: 'gradle/libs.versions.toml',
},
fileReplacePosition: 82,
registryUrls: ['https://repo.maven.apache.org/maven2'],
},
],
},
]);
});
});
22 changes: 21 additions & 1 deletion lib/modules/manager/gradle/extract/catalog.ts
Expand Up @@ -3,6 +3,7 @@ import is from '@sindresorhus/is';
import deepmerge from 'deepmerge';
import type { SkipReason } from '../../../../types';
import { hasKey } from '../../../../util/object';
import { escapeRegExp, regEx } from '../../../../util/regex';
import type { PackageDependency } from '../../types';
import type {
GradleCatalog,
Expand All @@ -14,6 +15,25 @@ import type {
VersionPointer,
} from '../types';

function findVersionIndex(
content: string,
depName: string,
version: string
): number {
const eDn = escapeRegExp(depName);
const eVer = escapeRegExp(version);
const re = regEx(
`(?:id\\s*=\\s*)?['"]?${eDn}["']?(?:(?:\\s*=\\s*)|:|,\\s*)(?:.*version(?:\\.ref)?(?:\\s*\\=\\s*))?["']?${eVer}['"]?`
);
const match = re.exec(content);
if (match) {
return match.index + content.slice(match.index).indexOf(version);
}
// ignoring Fallback because I can't reach it in tests, and code is not supposed to reach it but just in case.
/* istanbul ignore next */
return findIndexAfter(content, depName, version);
}

function findIndexAfter(
content: string,
sliceAfter: string,
Expand Down Expand Up @@ -91,7 +111,7 @@ function extractLiteralVersion({
return { skipReason: 'no-version' };
} else if (is.string(version)) {
const fileReplacePosition =
depStartIndex + findIndexAfter(depSubContent, sectionKey, version);
depStartIndex + findVersionIndex(depSubContent, sectionKey, version);
return { currentValue: version, fileReplacePosition };
} else if (is.plainObject(version)) {
// https://github.com/gradle/gradle/blob/d9adf33a57925582988fc512002dcc0e8ce4db95/subprojects/core/src/main/java/org/gradle/api/internal/catalog/parser/TomlCatalogFileParser.java#L368
Expand Down

0 comments on commit dad6a1f

Please sign in to comment.