Skip to content

Commit

Permalink
refactor(pip-compile): Move matchManager to common.ts (#29359)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbudnek committed May 30, 2024
1 parent 2d7bf51 commit 75b7ee5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
20 changes: 20 additions & 0 deletions lib/modules/manager/pip-compile/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
extractHeaderCommand,
extractPythonVersion,
getRegistryCredVarsFromPackageFile,
matchManager,
} from './common';
import { inferCommandExecDir } from './utils';

Expand Down Expand Up @@ -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');
});
});
});
19 changes: 18 additions & 1 deletion lib/modules/manager/pip-compile/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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';
}
25 changes: 2 additions & 23 deletions lib/modules/manager/pip-compile/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 75b7ee5

Please sign in to comment.