Skip to content

Commit

Permalink
Merge pull request #10 from InvisibleSmiley/Issue-9
Browse files Browse the repository at this point in the history
Fix splitting version strings with userinfo in URL
  • Loading branch information
PRGfx authored Oct 25, 2024
2 parents 8f2dbe7 + c2e5432 commit 1444dd5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
23 changes: 12 additions & 11 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,21 @@ function ($e) {
}

/**
* To avoid splitting on scoped package-names, every but the last @ are considered
* package name.
*
* @param string $versionString
*
* @return string[]
* @return array{string, string}
* @throws ParserException
*/
public static function splitVersionString($versionString)
{
$parts = explode('@', $versionString);
$version = array_pop($parts);
return [
implode('@', $parts),
$version
];
// Scoped package names start with an "@" → skip the first character.
// Note: $versionString may contain even more than two "@" if version is "protocol://user@host..."
$location = strpos($versionString, '@', 1);
if ($location > 0) {
$name = substr($versionString, 0, $location);
$version = substr($versionString, $location + 1);
return [$name, $version];
}

throw new ParserException('Invalid version string: ' . $versionString, 1729855362);
}
}
64 changes: 54 additions & 10 deletions tests/src/Unit/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,65 @@ public function testYarnExampleArray()
}

/**
* Scoped packages names should not be split at the first '@'
* The parser should split the package name and version parts
*
* Scoped packages names (prefixed with @) and Git version references should be detected properly.
*
* @param string $versionString
* @param array{string, string} $expectedResult
* @dataProvider provideVersionsForSplitting
* @return void
*/
public function testVersionSplitting()
public function testVersionSplitting($versionString, array $expectedResult)
{
static::assertSame(
['gulp-sourcemaps', '2.6.4'],
Parser::splitVersionString('[email protected]')
);
self::assertSame($expectedResult, Parser::splitVersionString($versionString));
}

static::assertSame(
['@gulp-sourcemaps/identity-map', '1.X'],
Parser::splitVersionString('@gulp-sourcemaps/[email protected]')
);
/**
* @return array<string, array{
* versionString: string,
* expectedResult: array{string, string}
* }>
*/
public static function provideVersionsForSplitting(): array
{
return [
'simple package, simple version' => [
'versionString' => '[email protected]',
'expectedResult' => ['gulp-sourcemaps', '2.6.4'],
],
'namespaced package, simple version' => [
'versionString' => '@gulp-sourcemaps/[email protected]',
'expectedResult' => ['@gulp-sourcemaps/identity-map', '1.X'],
],
'simple package, Git semver reference' => [
'versionString' => 'foo-bar@git+ssh://user@host:1234/foo/bar#semver:^1.2.3',
'expectedResult' => ['foo-bar', 'git+ssh://user@host:1234/foo/bar#semver:^1.2.3'],
],
'namespaced package, Git semver reference' => [
'versionString' => '@foo/bar@git+ssh://user@host:1234/foo/bar#semver:^1.2.3',
'expectedResult' => ['@foo/bar', 'git+ssh://user@host:1234/foo/bar#semver:^1.2.3'],
],
'simple package, Git tag reference' => [
'versionString' => 'foo-bar@git://user@host/foo/bar.git#v1.2.3',
'expectedResult' => ['foo-bar', 'git://user@host/foo/bar.git#v1.2.3'],
],
'namespaced package, Git tag reference' => [
'versionString' => '@foo/bar@git://user@host/foo/bar.git#v1.2.3',
'expectedResult' => ['@foo/bar', 'git://user@host/foo/bar.git#v1.2.3'],
],
'simple package, file reference' => [
'versionString' => 'foo-bar@file:vendor/foo/bar',
'expectedResult' => ['foo-bar', 'file:vendor/foo/bar'],
],
'namespaced package, file reference' => [
'versionString' => '@foo/bar@file:vendor/foo/bar',
'expectedResult' => ['@foo/bar', 'file:vendor/foo/bar'],
],
];
}


/**
* Single-value keys should not be split at spaces if they are surrounded with quotes
* @throws ParserException
Expand Down

0 comments on commit 1444dd5

Please sign in to comment.