From 75b7ee545fc556749d189ab38b53008b4f5cc1ba Mon Sep 17 00:00:00 2001 From: Miles Budnek Date: Thu, 30 May 2024 15:04:24 -0400 Subject: [PATCH] refactor(pip-compile): Move matchManager to common.ts (#29359) --- .../manager/pip-compile/common.spec.ts | 20 +++++++++++++++ lib/modules/manager/pip-compile/common.ts | 19 +++++++++++++- lib/modules/manager/pip-compile/extract.ts | 25 ++----------------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/lib/modules/manager/pip-compile/common.spec.ts b/lib/modules/manager/pip-compile/common.spec.ts index 032706d2de6ead..165b9cededc34a 100644 --- a/lib/modules/manager/pip-compile/common.spec.ts +++ b/lib/modules/manager/pip-compile/common.spec.ts @@ -6,6 +6,7 @@ import { extractHeaderCommand, extractPythonVersion, getRegistryCredVarsFromPackageFile, + matchManager, } from './common'; import { inferCommandExecDir } from './utils'; @@ -283,4 +284,23 @@ describe('modules/manager/pip-compile/common', () => { ).toEqual({}); }); }); + + describe('matchManager()', () => { + it('matches pip_setup setup.py', () => { + expect(matchManager('setup.py')).toBe('pip_setup'); + }); + + it('matches setup-cfg setup.cfg', () => { + expect(matchManager('setup.cfg')).toBe('setup-cfg'); + }); + + it('matches pep621 pyproject.toml', () => { + expect(matchManager('pyproject.toml')).toBe('pep621'); + }); + + it('matches pip_requirements any .in file', () => { + expect(matchManager('file.in')).toBe('pip_requirements'); + expect(matchManager('another_file.in')).toBe('pip_requirements'); + }); + }); }); diff --git a/lib/modules/manager/pip-compile/common.ts b/lib/modules/manager/pip-compile/common.ts index b80607fa760e3c..5f9e7255387012 100644 --- a/lib/modules/manager/pip-compile/common.ts +++ b/lib/modules/manager/pip-compile/common.ts @@ -9,7 +9,7 @@ import { ensureLocalPath } from '../../../util/fs/util'; import * as hostRules from '../../../util/host-rules'; import { regEx } from '../../../util/regex'; import type { PackageFileContent, UpdateArtifactsConfig } from '../types'; -import type { PipCompileArgs } from './types'; +import type { PipCompileArgs, SupportedManagers } from './types'; export function getPythonVersionConstraint( config: UpdateArtifactsConfig, @@ -304,3 +304,20 @@ export function getRegistryCredVarsFromPackageFile( return allCreds; } + +export function matchManager(filename: string): SupportedManagers | 'unknown' { + if (filename.endsWith('setup.py')) { + return 'pip_setup'; + } + if (filename.endsWith('setup.cfg')) { + return 'setup-cfg'; + } + if (filename.endsWith('pyproject.toml')) { + return 'pep621'; + } + // naive, could be improved, maybe use pip_requirements.fileMatch + if (filename.endsWith('.in')) { + return 'pip_requirements'; + } + return 'unknown'; +} diff --git a/lib/modules/manager/pip-compile/extract.ts b/lib/modules/manager/pip-compile/extract.ts index 977b5c7c66d40b..8381113fb41ce1 100644 --- a/lib/modules/manager/pip-compile/extract.ts +++ b/lib/modules/manager/pip-compile/extract.ts @@ -11,35 +11,14 @@ import type { PackageFile, PackageFileContent, } from '../types'; -import { extractHeaderCommand } from './common'; -import type { - DependencyBetweenFiles, - PipCompileArgs, - SupportedManagers, -} from './types'; +import { extractHeaderCommand, matchManager } from './common'; +import type { DependencyBetweenFiles, PipCompileArgs } from './types'; import { generateMermaidGraph, inferCommandExecDir, sortPackageFiles, } from './utils'; -function matchManager(filename: string): SupportedManagers | 'unknown' { - if (filename.endsWith('setup.py')) { - return 'pip_setup'; - } - if (filename.endsWith('setup.cfg')) { - return 'setup-cfg'; - } - if (filename.endsWith('pyproject.toml')) { - return 'pep621'; - } - // naive, could be improved, maybe use pip_requirements.fileMatch - if (filename.endsWith('.in')) { - return 'pip_requirements'; - } - return 'unknown'; -} - export function extractPackageFile( content: string, packageFile: string,