Skip to content

Commit

Permalink
refactor: PackageFile types (#20498)
Browse files Browse the repository at this point in the history
Co-authored-by: RahulGautamSingh <rahultesnik@gmail.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
3 people authored Feb 20, 2023
1 parent 43848b7 commit cc33d34
Show file tree
Hide file tree
Showing 45 changed files with 245 additions and 220 deletions.
2 changes: 1 addition & 1 deletion lib/modules/manager/batect/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('modules/manager/batect/extract', () => {

// TODO: #7154
expect(
result?.sort((a, b) => a.packageFile!.localeCompare(b.packageFile!))
result?.sort((a, b) => a.packageFile.localeCompare(b.packageFile))
).toEqual([
{
packageFile: `${fixturesDir}/valid/another-include.yml`,
Expand Down
10 changes: 3 additions & 7 deletions lib/modules/manager/batect/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { GitTagsDatasource } from '../../datasource/git-tags';
import { id as dockerVersioning } from '../../versioning/docker';
import { id as semverVersioning } from '../../versioning/semver';
import { getDep } from '../dockerfile/extract';
import type {
ExtractConfig,
PackageDependency,
PackageFileContent,
} from '../types';
import type { ExtractConfig, PackageDependency, PackageFile } from '../types';
import type {
BatectConfig,
BatectFileInclude,
Expand Down Expand Up @@ -153,10 +149,10 @@ export function extractPackageFile(
export async function extractAllPackageFiles(
config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFileContent[] | null> {
): Promise<PackageFile[] | null> {
const filesToExamine = new Set<string>(packageFiles);
const filesAlreadyExamined = new Set<string>();
const results: PackageFileContent[] = [];
const results: PackageFile[] = [];

while (filesToExamine.size > 0) {
const packageFile = filesToExamine.values().next().value;
Expand Down
5 changes: 3 additions & 2 deletions lib/modules/manager/flux/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getDep } from '../dockerfile/extract';
import type {
ExtractConfig,
PackageDependency,
PackageFile,
PackageFileContent,
} from '../types';
import { isSystemManifest } from './common';
Expand Down Expand Up @@ -268,9 +269,9 @@ export function extractPackageFile(
export async function extractAllPackageFiles(
_config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFileContent<FluxManagerData>[] | null> {
): Promise<PackageFile<FluxManagerData>[] | null> {
const manifests: FluxManifest[] = [];
const results: PackageFileContent<FluxManagerData>[] = [];
const results: PackageFile<FluxManagerData>[] = [];

for (const file of packageFiles) {
const content = await readLocalFile(file, 'utf8');
Expand Down
5 changes: 3 additions & 2 deletions lib/modules/manager/gitlabci/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { trimLeadingSlash } from '../../../util/url';
import type {
ExtractConfig,
PackageDependency,
PackageFile,
PackageFileContent,
} from '../types';
import { isGitlabIncludeLocal } from './common';
Expand Down Expand Up @@ -123,10 +124,10 @@ export function extractPackageFile(
export async function extractAllPackageFiles(
config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFileContent[] | null> {
): Promise<PackageFile[] | null> {
const filesToExamine = [...packageFiles];
const seen = new Set<string>(packageFiles);
const results: PackageFileContent[] = [];
const results: PackageFile[] = [];

// extract all includes from the files
while (filesToExamine.length > 0) {
Expand Down
12 changes: 4 additions & 8 deletions lib/modules/manager/gradle/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import upath from 'upath';
import { logger } from '../../../logger';
import { getLocalFiles } from '../../../util/fs';
import { MavenDatasource } from '../../datasource/maven';
import type {
ExtractConfig,
PackageDependency,
PackageFileContent,
} from '../types';
import type { ExtractConfig, PackageDependency, PackageFile } from '../types';
import { parseCatalog } from './extract/catalog';
import {
isGcvPropsFile,
Expand Down Expand Up @@ -52,10 +48,10 @@ function getRegistryUrlsForDep(
export async function extractAllPackageFiles(
config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFileContent[] | null> {
): Promise<PackageFile[] | null> {
const extractedDeps: PackageDependency<GradleManagerData>[] = [];
const varRegistry: VariableRegistry = {};
const packageFilesByName: Record<string, PackageFileContent> = {};
const packageFilesByName: Record<string, PackageFile> = {};
const packageRegistries: PackageRegistry[] = [];
const reorderedFiles = reorderFiles(packageFiles);
const fileContents = await getLocalFiles(packageFiles);
Expand Down Expand Up @@ -131,7 +127,7 @@ export async function extractAllPackageFiles(
const key = dep.managerData?.packageFile;
// istanbul ignore else
if (key) {
let pkgFile: PackageFileContent = packageFilesByName[key];
let pkgFile: PackageFile = packageFilesByName[key];
// istanbul ignore if: won't happen if "apply from" processes only initially known files
if (!pkgFile) {
pkgFile = {
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ExtractConfig,
GlobalManagerConfig,
ManagerApi,
PackageFile,
PackageFileContent,
RangeConfig,
Result,
Expand Down Expand Up @@ -40,7 +41,7 @@ export async function extractAllPackageFiles(
manager: string,
config: ExtractConfig,
files: string[]
): Promise<PackageFileContent[] | null> {
): Promise<PackageFile[] | null> {
if (!managers.has(manager)) {
return null;
}
Expand Down
9 changes: 5 additions & 4 deletions lib/modules/manager/maven/__snapshots__/extract.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,22 @@ exports[`modules/manager/maven/extract extractDependencies extract dependencies
"mavenProps": {
"quuxGroup": {
"fileReplacePosition": 631,
"packageFile": null,
"packageFile": "some-file",
"val": "org.example",
},
"quuxId": {
"fileReplacePosition": 667,
"packageFile": null,
"packageFile": "some-file",
"val": "quux",
},
"quuxVersion": {
"fileReplacePosition": 698,
"packageFile": null,
"packageFile": "some-file",
"val": "1.2.3.4",
},
},
"packageFile": null,
"packageFile": "some-file",
"packageFileVersion": "0.0.1",
"parent": "../pom.xml",
}
`;
22 changes: 11 additions & 11 deletions lib/modules/manager/maven/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const complexSettingsContent = Fixtures.get(`complex.settings.xml`);
describe('modules/manager/maven/extract', () => {
describe('extractDependencies', () => {
it('returns null for invalid XML', () => {
expect(extractPackage('')).toBeNull();
expect(extractPackage('invalid xml content')).toBeNull();
expect(extractPackage('<foobar></foobar>')).toBeNull();
expect(extractPackage('<project></project>')).toBeNull();
expect(extractPackage('', 'some-file')).toBeNull();
expect(extractPackage('invalid xml content', 'some-file')).toBeNull();
expect(extractPackage('<foobar></foobar>', 'some-file')).toBeNull();
expect(extractPackage('<project></project>', 'some-file')).toBeNull();
});

it('extract dependencies from any XML position', () => {
const res = extractPackage(simpleContent);
const res = extractPackage(simpleContent, 'some-file');
expect(res).toMatchSnapshot({
deps: [
{
Expand Down Expand Up @@ -113,29 +113,29 @@ describe('modules/manager/maven/extract', () => {
],
mavenProps: {
quuxGroup: {
packageFile: null,
packageFile: 'some-file',
val: 'org.example',
},
quuxId: {
packageFile: null,
packageFile: 'some-file',
val: 'quux',
},
quuxVersion: {
packageFile: null,
packageFile: 'some-file',
val: '1.2.3.4',
},
},
packageFile: null,
packageFile: 'some-file',
});
});

it('tries minimum manifests', () => {
const res = extractPackage(minimumContent);
const res = extractPackage(minimumContent, 'some-file');
expect(res).toEqual({
datasource: 'maven',
deps: [],
mavenProps: {},
packageFile: null,
packageFile: 'some-file',
packageFileVersion: '1',
});
});
Expand Down
26 changes: 9 additions & 17 deletions lib/modules/manager/maven/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { readLocalFile } from '../../../util/fs';
import { regEx } from '../../../util/regex';
import { MavenDatasource } from '../../datasource/maven';
import { MAVEN_REPO } from '../../datasource/maven/common';
import type {
ExtractConfig,
PackageDependency,
PackageFileContent,
} from '../types';
import type { ExtractConfig, PackageDependency, PackageFile } from '../types';
import type { MavenProp } from './types';

export function parsePom(raw: string): XmlDocument | null {
Expand Down Expand Up @@ -249,15 +245,15 @@ function resolveParentFile(packageFile: string, parentPath: string): string {
return upath.normalize(upath.join(dir, parentDir, parentFile));
}

interface MavenInterimPackageFile extends PackageFileContent {
interface MavenInterimPackageFile extends PackageFile {
mavenProps?: Record<string, any>;
parent?: string;
}

export function extractPackage(
rawContent: string,
packageFile: string | null = null
): PackageFileContent<Record<string, any>> | null {
packageFile: string
): PackageFile | null {
if (!rawContent) {
return null;
}
Expand Down Expand Up @@ -373,16 +369,14 @@ export function parseSettings(raw: string): XmlDocument | null {
return null;
}

export function resolveParents(
packages: PackageFileContent[]
): PackageFileContent[] {
export function resolveParents(packages: PackageFile[]): PackageFile[] {
const packageFileNames: string[] = [];
const extractedPackages: Record<string, MavenInterimPackageFile> = {};
const extractedDeps: Record<string, PackageDependency[]> = {};
const extractedProps: Record<string, MavenProp> = {};
const registryUrls: Record<string, Set<string>> = {};
packages.forEach((pkg) => {
const name = pkg.packageFile!;
const name = pkg.packageFile;
packageFileNames.push(name);
extractedPackages[name] = pkg;
extractedDeps[name] = [];
Expand Down Expand Up @@ -460,9 +454,7 @@ export function resolveParents(
return packageFiles;
}

function cleanResult(
packageFiles: MavenInterimPackageFile[]
): PackageFileContent<Record<string, any>>[] {
function cleanResult(packageFiles: MavenInterimPackageFile[]): PackageFile[] {
packageFiles.forEach((packageFile) => {
delete packageFile.mavenProps;
delete packageFile.parent;
Expand All @@ -476,8 +468,8 @@ function cleanResult(
export async function extractAllPackageFiles(
_config: ExtractConfig,
packageFiles: string[]
): Promise<PackageFileContent[]> {
const packages: PackageFileContent[] = [];
): Promise<PackageFile[]> {
const packages: PackageFile[] = [];
const additionalRegistryUrls: string[] = [];

for (const packageFile of packageFiles) {
Expand Down
29 changes: 19 additions & 10 deletions lib/modules/manager/maven/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,16 @@ describe('modules/manager/maven/index', () => {
it('should update an existing dependency', () => {
const newValue = '9.9.9.9-final';

const { deps } = extractPackage(pomContent)!;
const { deps } = extractPackage(pomContent, 'some-file')!;
const dep = selectDep(deps);
const upgrade = { ...dep, newValue };
const updatedContent = updateDependency({
fileContent: pomContent,
upgrade,
})!;
const updatedDep = selectDep(extractPackage(updatedContent)!.deps);
const updatedDep = selectDep(
extractPackage(updatedContent, 'some-file')!.deps
);

expect(updatedDep?.currentValue).toEqual(newValue);
});
Expand Down Expand Up @@ -252,7 +254,7 @@ describe('modules/manager/maven/index', () => {

it('should apply props recursively', () => {
const [{ deps }] = resolveParents([
extractPackage(Fixtures.get('recursive_props.pom.xml'))!,
extractPackage(Fixtures.get('recursive_props.pom.xml'), 'some-file')!,
]);
expect(deps).toMatchObject([
{
Expand All @@ -264,7 +266,10 @@ describe('modules/manager/maven/index', () => {

it('should apply props multiple times', () => {
const [{ deps }] = resolveParents([
extractPackage(Fixtures.get('multiple_usages_props.pom.xml'))!,
extractPackage(
Fixtures.get('multiple_usages_props.pom.xml'),
'some-file'
)!,
]);
expect(deps).toMatchObject([
{
Expand All @@ -276,7 +281,10 @@ describe('modules/manager/maven/index', () => {

it('should detect props infinitely recursing props', () => {
const [{ deps }] = resolveParents([
extractPackage(Fixtures.get('infinite_recursive_props.pom.xml'))!,
extractPackage(
Fixtures.get('infinite_recursive_props.pom.xml'),
'some-file'
)!,
]);
expect(deps).toMatchObject([
{
Expand Down Expand Up @@ -317,7 +325,7 @@ describe('modules/manager/maven/index', () => {
it('should not touch content if new and old versions are equal', () => {
const newValue = '1.2.3';

const { deps } = extractPackage(pomContent)!;
const { deps } = extractPackage(pomContent, 'some-file')!;
const dep = selectDep(deps);
const upgrade = { ...dep, newValue };
const updatedContent = updateDependency({
Expand Down Expand Up @@ -388,7 +396,7 @@ describe('modules/manager/maven/index', () => {
const currentValue = '1.2.2';
const newValue = '1.2.4';

const { deps } = extractPackage(pomContent)!;
const { deps } = extractPackage(pomContent, 'some-file')!;
const dep = selectDep(deps);
const upgrade = { ...dep, currentValue, newValue };
const updatedContent = updateDependency({
Expand All @@ -403,11 +411,12 @@ describe('modules/manager/maven/index', () => {
const newValue = '[1.2.3]';
const select = (depSet: PackageFileContent) =>
selectDep(depSet.deps, 'org.example:hard-range');
const oldContent = extractPackage(pomContent);
const oldContent = extractPackage(pomContent, 'some-file');
const dep = select(oldContent!);
const upgrade = { ...dep, newValue };
const newContent = extractPackage(
updateDependency({ fileContent: pomContent, upgrade })!
updateDependency({ fileContent: pomContent, upgrade })!,
'some-file'
);
const newDep = select(newContent!);
expect(newDep?.currentValue).toEqual(newValue);
Expand All @@ -417,7 +426,7 @@ describe('modules/manager/maven/index', () => {
const newValue = '[1.0.0]';
const select = (depSet: PackageFileContent) =>
depSet?.deps ? selectDep(depSet.deps, 'org.example:hard-range') : null;
const oldContent = extractPackage(pomContent);
const oldContent = extractPackage(pomContent, 'some-file');
const dep = select(oldContent!);
expect(dep).not.toBeNull();
const upgrade = { ...dep, newValue };
Expand Down
Loading

0 comments on commit cc33d34

Please sign in to comment.