Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(manager): add cpanfile manager (#21152)
- Loading branch information
Showing
7 changed files
with
511 additions
and
0 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
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,285 @@ | ||
import { codeBlock } from 'common-tags'; | ||
import { extractPackageFile } from './extract'; | ||
|
||
describe('modules/manager/cpanfile/extract', () => { | ||
describe('extractPackageFile()', () => { | ||
it('returns null for empty', () => { | ||
expect(extractPackageFile('', 'cpanfile')).toBeNull(); | ||
expect(extractPackageFile('nothing here', 'cpanfile')).toBeNull(); | ||
}); | ||
|
||
describe('parse perl', () => { | ||
test.each` | ||
version | expected | ||
${'5.012005'} | ${'5.012005'} | ||
${`'5.008001'`} | ${'5.008001'} | ||
${`"5.008001"`} | ${'5.008001'} | ||
`('$version', ({ version, expected }) => { | ||
expect( | ||
extractPackageFile( | ||
`requires 'perl', ${version as string};`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
versioning: 'perl', | ||
depName: 'perl', | ||
packageName: 'Perl/perl5', | ||
currentValue: expected, | ||
datasource: 'github-tags', | ||
extractVersion: '^v(?<version>\\S+)', | ||
}, | ||
], | ||
extractedConstraints: { perl: expected }, | ||
}); | ||
}); | ||
}); | ||
|
||
it('parse modules with requires', () => { | ||
expect( | ||
extractPackageFile( | ||
codeBlock` | ||
requires 'Try::Tiny'; | ||
requires 'URI', '1.59'; | ||
requires 'HTTP::Tiny', 0.034; | ||
requires "Capture::Tiny" => "0"; | ||
`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Try::Tiny', | ||
skipReason: 'no-version', | ||
}, | ||
{ | ||
datasource: 'cpan', | ||
depName: 'URI', | ||
currentValue: '1.59', | ||
}, | ||
{ | ||
datasource: 'cpan', | ||
depName: 'HTTP::Tiny', | ||
currentValue: '0.034', | ||
}, | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Capture::Tiny', | ||
currentValue: '0', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('parse modules with recommends', () => { | ||
expect( | ||
extractPackageFile( | ||
codeBlock` | ||
recommends 'Crypt::URandom'; | ||
recommends 'HTTP::XSCookies', '0.000015'; | ||
`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Crypt::URandom', | ||
skipReason: 'no-version', | ||
}, | ||
{ | ||
datasource: 'cpan', | ||
depName: 'HTTP::XSCookies', | ||
currentValue: '0.000015', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
it('parse modules with suggests', () => { | ||
expect( | ||
extractPackageFile( | ||
codeBlock` | ||
suggests 'Test::MockTime::HiRes', '0.06'; | ||
suggests 'Authen::Simple::Passwd'; | ||
`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Test::MockTime::HiRes', | ||
currentValue: '0.06', | ||
}, | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Authen::Simple::Passwd', | ||
skipReason: 'no-version', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
describe('parse modules with phases', () => { | ||
test('configure phase', () => { | ||
expect( | ||
extractPackageFile( | ||
codeBlock` | ||
on 'configure' => sub { | ||
requires "ExtUtils::MakeMaker" => "0"; | ||
}; | ||
`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'ExtUtils::MakeMaker', | ||
currentValue: '0', | ||
depType: 'configure', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test('build phase', () => { | ||
expect( | ||
extractPackageFile( | ||
codeBlock` | ||
on build => sub { | ||
requires 'Test::More', '0.98'; | ||
}; | ||
`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Test::More', | ||
currentValue: '0.98', | ||
depType: 'build', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test('test phase', () => { | ||
expect( | ||
extractPackageFile( | ||
codeBlock` | ||
on test => sub { | ||
requires 'Test::More', '0.88'; | ||
requires 'Test::Requires'; | ||
}; | ||
`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Test::More', | ||
currentValue: '0.88', | ||
depType: 'test', | ||
}, | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Test::Requires', | ||
depType: 'test', | ||
skipReason: 'no-version', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test('runtime phase', () => { | ||
expect( | ||
extractPackageFile( | ||
codeBlock` | ||
on runtime => sub { | ||
suggests 'FCGI'; | ||
suggests 'FCGI::ProcManager'; | ||
}; | ||
`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'FCGI', | ||
depType: 'runtime', | ||
skipReason: 'no-version', | ||
}, | ||
{ | ||
datasource: 'cpan', | ||
depName: 'FCGI::ProcManager', | ||
depType: 'runtime', | ||
skipReason: 'no-version', | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
test('develop phase', () => { | ||
expect( | ||
extractPackageFile( | ||
codeBlock` | ||
on 'develop' => sub { | ||
requires "IPC::Open3" => "0"; | ||
requires "Term::Table" => "0.013"; | ||
}; | ||
`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'IPC::Open3', | ||
currentValue: '0', | ||
depType: 'develop', | ||
}, | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Term::Table', | ||
currentValue: '0.013', | ||
depType: 'develop', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
|
||
describe('parse modules with phase shortcuts', () => { | ||
test.each` | ||
shortcut | phase | ||
${'configure_requires'} | ${'configure'} | ||
${'build_requires'} | ${'build'} | ||
${'test_requires'} | ${'test'} | ||
${'author_requires'} | ${'develop'} | ||
`('$shortcut', ({ shortcut, phase }) => { | ||
expect( | ||
extractPackageFile( | ||
`${shortcut as string} 'Capture::Tiny', '0.12';`, | ||
'cpanfile' | ||
) | ||
).toEqual({ | ||
deps: [ | ||
{ | ||
datasource: 'cpan', | ||
depName: 'Capture::Tiny', | ||
currentValue: '0.12', | ||
depType: phase, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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,16 @@ | ||
import type { PackageFileContent } from '../types'; | ||
import { parse } from './parser'; | ||
|
||
export function extractPackageFile( | ||
content: string, | ||
packageFile?: string | ||
): PackageFileContent | null { | ||
const result = parse(content); | ||
if (!result?.deps.length) { | ||
return null; | ||
} | ||
|
||
const { deps, perlVersion } = result; | ||
const extractedConstraints = perlVersion ? { perl: perlVersion } : undefined; | ||
return { deps, ...(extractedConstraints && { extractedConstraints }) }; | ||
} |
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,17 @@ | ||
import { CpanDatasource } from '../../datasource/cpan'; | ||
import { GithubTagsDatasource } from '../../datasource/github-tags'; | ||
|
||
export { extractPackageFile } from './extract'; | ||
|
||
export const displayName = 'cpanfile'; | ||
export const url = | ||
'https://metacpan.org/dist/Module-CPANfile/view/lib/cpanfile.pod'; | ||
|
||
export const defaultConfig = { | ||
fileMatch: ['(^|/)cpanfile$'], | ||
}; | ||
|
||
export const supportedDatasources = [ | ||
CpanDatasource.id, | ||
GithubTagsDatasource.id, | ||
]; |
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,44 @@ | ||
import { lexer as l, lang, parser as p } from 'good-enough-parser'; | ||
|
||
/** | ||
* @see https://perldoc.perl.org/perldata#Scalar-value-constructors | ||
*/ | ||
const bindigit = '[01]'; | ||
const octdigit = '[0-7]'; | ||
const digit = '[0-9]'; | ||
const nonzerodigit = '[1-9]'; | ||
const hexdigit = `(?:${digit}|[a-fA-F])`; | ||
|
||
const bininteger = `(?:0[bB](?:_?${bindigit})+)`; | ||
const octinteger = `(?:0(?:_?${octdigit})+)`; | ||
const hexinteger = `(?:0[xX](?:_?${hexdigit})+)`; | ||
const decinteger = `(?:${nonzerodigit}(?:_?${digit})*|0+(?:_?0)*)`; | ||
const integer = `(?:${decinteger}|${bininteger}|${octinteger}|${hexinteger})`; | ||
|
||
const digitpart = `(?:${digit}(?:_?${digit})*)`; | ||
const fraction = `(?:\\.${digitpart})`; | ||
const exponent = `(?:[eE][-+]?${digitpart})`; | ||
const pointfloat = `(?:${digitpart}?${fraction}|${digitpart}\\.)`; | ||
const exponentfloat = `(?:(?:${digitpart}|${pointfloat})${exponent})`; | ||
const floatnumber = `(?:${pointfloat}|${exponentfloat})`; | ||
|
||
const numbers = new RegExp(`(?:${floatnumber}|${integer})`); | ||
|
||
const lexer: l.LexerConfig = { | ||
joinLines: null, | ||
comments: [{ type: 'line-comment', startsWith: '#' }], | ||
symbols: /[_a-zA-Z][_a-zA-Z0-9]*/, | ||
numbers, | ||
operators: ['==', '>=', '>', '=>', ',', ';'], | ||
brackets: [ | ||
{ startsWith: '{', endsWith: '}' }, | ||
{ startsWith: '(', endsWith: ')' }, | ||
], | ||
strings: [{ startsWith: "'" }, { startsWith: '"' }], | ||
}; | ||
|
||
const parser: p.ParserConfig = { | ||
useIndentBlocks: false, | ||
}; | ||
|
||
export const cpanfile = lang.createLang({ lexer, parser }); |
Oops, something went wrong.