Skip to content

Commit

Permalink
Merge pull request #117 from siketyan/feat/constraint-aware-package-m…
Browse files Browse the repository at this point in the history
…atching

feat: Add multi-constraint package support for yarn and pnpm
  • Loading branch information
siketyan authored Jul 22, 2023
2 parents bf10fd6 + 43ab18f commit 2dbb5d4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/Model/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ class Package
{
public function __construct(
private readonly string $name,
private readonly ?string $constraint = null,
) {
}

public function getName(): string
{
return $this->name;
}

public function getConstraint(): ?string
{
return $this->constraint;
}
}
16 changes: 13 additions & 3 deletions src/Scanner/AbstractPackagePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ public function __construct(
) {
}

public function get(string $name): ?Package
public function get(string $name, ?string $constraint = null): ?Package
{
return $this->packages[$name] ?? null;
return $this->packages[$this->getKey($name, $constraint)] ?? null;
}

public function add(Package $package): void
{
$this->packages[$package->getName()] = $package;
$this->packages[$this->getKey($package->getName(), $package->getConstraint())] = $package;
}

private function getKey(string $name, ?string $constraint): string
{
$key = $name;
if ($constraint !== null) {
$key .= '__' . $constraint;
}

return $key;
}
}
18 changes: 10 additions & 8 deletions src/Scanner/Pnpm/PnpmLockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,28 @@ public function parse(?string $yaml): DependencyCollection

/**
* @var array{
* dependencies?: array<string, array{version: string}|string>,
* devDependencies?: array<string, array{version: string}|string>,
* dependencies?: array<string, array{specifier?: string, specification?: string, version: string}|string>,
* devDependencies?: array<string, array{specifier?: string, specification?: string, version: string}|string>,
* } $assoc
*/
$assoc = Yaml::parse($yaml) ?? [];
$packages = array_merge($assoc['dependencies'] ?? [], $assoc['devDependencies'] ?? []);
$dependencies = [];

foreach ($packages as $name => $version) {
$package = $this->packagePool->get($name);
$constraint = null;
if (\is_array($version)) {
$constraint = $version['specifier'] ?? $version['specification'] ?? null;
$version = $version['version'];
}

$package = $this->packagePool->get($name, $constraint);

if (!$package instanceof Package) {
$package = new Package($name);
$package = new Package($name, $constraint);
$this->packagePool->add($package);
}

if (\is_array($version)) {
$version = $version['version'];
}

/** @var string $version */
$version = preg_replace('/\(.+\)/', '', $version);

Expand Down
4 changes: 2 additions & 2 deletions src/Scanner/Yarn/YarnLockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public function parse(?string $lock): DependencyCollection
/** @var ConstraintInterface $constraint */
foreach ($package->getConstraints()->all() as $constraint) {
$name = $constraint->getName();
$pkg = $this->packagePool->get($name);
$pkg = $this->packagePool->get($name, $constraint->getRange());

if (!$pkg instanceof Package) {
$pkg = new Package($name);
$pkg = new Package($name, $constraint->getRange());
$this->packagePool->add($pkg);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Scanner/AbstractPackagePoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,30 @@ public function test(): void

$package = $this->prophesize(Package::class);
$package->getName()->willReturn($name);
$package->getConstraint()->willReturn(null);
$package = $package->reveal();

$this->pool->add($package);

$this->assertSame($package, $this->pool->get($name));
$this->assertNull($this->pool->get('not/exists'));
}

public function testWithConstraint(): void
{
$name = 'dummy/dummy';
$constraint = '^1.2.3';

$package = $this->prophesize(Package::class);
$package->getName()->willReturn($name);
$package->getConstraint()->willReturn($constraint);
$package = $package->reveal();

$this->pool->add($package);

$this->assertSame($package, $this->pool->get($name, $constraint));
$this->assertNull($this->pool->get($name), '^4.5.6');
$this->assertNull($this->pool->get($name));
$this->assertNull($this->pool->get('not/exists'));
}
}
4 changes: 2 additions & 2 deletions tests/Scanner/Pnpm/PnpmLockParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function test(string $yaml): void
$fooBarVersion = $this->prophesize(SemVerVersion::class)->reveal();
$barBazVersion = $this->prophesize(SemVerVersion::class)->reveal();

$this->packagePool->get('foo')->willReturn(null);
$this->packagePool->get('bar')->willReturn($cache);
$this->packagePool->get('foo', Argument::any())->willReturn(null);
$this->packagePool->get('bar', Argument::any())->willReturn($cache);
$this->packagePool->add(Argument::type(Package::class))->shouldBeCalledOnce();

$this->versionParser->parse('1.2.3-dev')->willReturn($fooBarVersion);
Expand Down
4 changes: 2 additions & 2 deletions tests/Scanner/Yarn/YarnLockParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function test(): void
$fooBarVersion = $this->prophesize(SemVerVersion::class)->reveal();
$barBazVersion = $this->prophesize(SemVerVersion::class)->reveal();

$this->packagePool->get('@types/node')->willReturn(null);
$this->packagePool->get('typescript')->willReturn($cache);
$this->packagePool->get('@types/node', '^18')->willReturn(null);
$this->packagePool->get('typescript', '^5')->willReturn($cache);
$this->packagePool->add(Argument::type(Package::class))->shouldBeCalledOnce();

$this->versionParser->parse('18.16.16')->willReturn($fooBarVersion);
Expand Down

0 comments on commit 2dbb5d4

Please sign in to comment.