-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(pip_requirements): Move flags extraction to common.ts (#29360)
- Loading branch information
Showing
3 changed files
with
139 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { extractPackageFileFlags } from './common'; | ||
|
||
describe('modules/manager/pip_requirements/common', () => { | ||
describe('extractPackageFileFlags()', () => { | ||
it('extracts --index-url flag', () => { | ||
const res = extractPackageFileFlags( | ||
'--index-url https://example.com/pypi', | ||
); | ||
expect(res).toMatchObject({ | ||
deps: [], | ||
registryUrls: ['https://example.com/pypi'], | ||
}); | ||
}); | ||
|
||
it('extracts --index-url short code', () => { | ||
const requirements = `-i http://example.com/private-pypi/ | ||
some-package==0.3.1`; | ||
|
||
const res = extractPackageFileFlags(requirements); | ||
|
||
expect(res).toMatchObject({ | ||
deps: [], | ||
registryUrls: ['http://example.com/private-pypi/'], | ||
}); | ||
}); | ||
|
||
it('extracts --extra-index-url flag', () => { | ||
const res = extractPackageFileFlags( | ||
'--extra-index-url https://example.com/pypi', | ||
); | ||
expect(res).toMatchObject({ | ||
deps: [], | ||
additionalRegistryUrls: ['https://example.com/pypi'], | ||
}); | ||
}); | ||
|
||
it('extracts --requirement short code option', () => { | ||
const requirements = `-r base.txt | ||
some-package==0.3.1`; | ||
|
||
const res = extractPackageFileFlags(requirements); | ||
|
||
expect(res).toHaveProperty('managerData', { | ||
requirementsFiles: ['base.txt'], | ||
}); | ||
}); | ||
|
||
it('extracts --constraints short code option', () => { | ||
const requirements = `-c constrain.txt | ||
some-package==0.3.1`; | ||
|
||
const res = extractPackageFileFlags(requirements); | ||
|
||
expect(res).toHaveProperty('managerData', { | ||
constraintsFiles: ['constrain.txt'], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { GlobalConfig } from '../../../config/global'; | ||
import { newlineRegex, regEx } from '../../../util/regex'; | ||
import type { PackageFileContent } from '../types'; | ||
import type { PipRequirementsManagerData } from './types'; | ||
|
||
function cleanRegistryUrls(registryUrls: string[]): string[] { | ||
return registryUrls.map((url) => { | ||
// handle the optional quotes in eg. `--extra-index-url "https://foo.bar"` | ||
const cleaned = url.replace(regEx(/^"/), '').replace(regEx(/"$/), ''); | ||
if (!GlobalConfig.get('exposeAllEnv')) { | ||
return cleaned; | ||
} | ||
// interpolate any environment variables | ||
return cleaned.replace( | ||
regEx(/(\$[A-Za-z\d_]+)|(\${[A-Za-z\d_]+})/g), | ||
(match) => { | ||
const envvar = match | ||
.substring(1) | ||
.replace(regEx(/^{/), '') | ||
.replace(regEx(/}$/), ''); | ||
const sub = process.env[envvar]; | ||
return sub ?? match; | ||
}, | ||
); | ||
}); | ||
} | ||
|
||
export function extractPackageFileFlags( | ||
content: string, | ||
): PackageFileContent<PipRequirementsManagerData> { | ||
let registryUrls: string[] = []; | ||
const additionalRegistryUrls: string[] = []; | ||
const additionalRequirementsFiles: string[] = []; | ||
const additionalConstraintsFiles: string[] = []; | ||
content.split(newlineRegex).forEach((line) => { | ||
if (line.startsWith('-i ') || line.startsWith('--index-url ')) { | ||
registryUrls = [line.split(' ')[1]]; | ||
} else if (line.startsWith('--extra-index-url ')) { | ||
const extraUrl = line | ||
.substring('--extra-index-url '.length) | ||
.split(' ')[0]; | ||
additionalRegistryUrls.push(extraUrl); | ||
} else if (line.startsWith('-r ')) { | ||
additionalRequirementsFiles.push(line.split(' ')[1]); | ||
} else if (line.startsWith('-c ')) { | ||
additionalConstraintsFiles.push(line.split(' ')[1]); | ||
} | ||
}); | ||
|
||
const res: PackageFileContent<PipRequirementsManagerData> = { deps: [] }; | ||
if (registryUrls.length > 0) { | ||
res.registryUrls = cleanRegistryUrls(registryUrls); | ||
} | ||
if (additionalRegistryUrls.length) { | ||
res.additionalRegistryUrls = cleanRegistryUrls(additionalRegistryUrls); | ||
} | ||
if (additionalRequirementsFiles.length) { | ||
if (!res.managerData) { | ||
res.managerData = {}; | ||
} | ||
res.managerData.requirementsFiles = additionalRequirementsFiles; | ||
} | ||
if (additionalConstraintsFiles.length) { | ||
if (!res.managerData) { | ||
res.managerData = {}; | ||
} | ||
res.managerData.constraintsFiles = additionalConstraintsFiles; | ||
} | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters