Skip to content

Commit 612aeea

Browse files
committed
Add package name verification to install scripts and update tests to reflect changes.
Signed-off-by: Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
1 parent d920d84 commit 612aeea

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/Composer/ScriptsInstallerTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ trait ScriptsInstallerTrait
4343
*/
4444
private function installScripts(Composer $composer, IOInterface $io): void
4545
{
46+
$package = $composer->getPackage();
47+
48+
if ($package->getName() !== 'fast-forward/dev-tools') {
49+
return;
50+
}
51+
4652
$io->write('<info>fast-forward/dev-tools: Installing scripts into composer.json</info>');
4753

4854
$file = Factory::getComposerFile();

tests/Composer/PluginTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ protected function setUp(): void
6464

6565
$this->originalComposerEnv = (string) getenv('COMPOSER');
6666
$this->tempComposerFile = tempnam(sys_get_temp_dir(), 'composer_test');
67-
file_put_contents($this->tempComposerFile, json_encode(['name' => 'test/package', 'scripts' => (object) []]));
68-
67+
// O nome do pacote precisa ser fast-forward/dev-tools para que o método installScripts execute a lógica
68+
file_put_contents($this->tempComposerFile, json_encode(['name' => 'fast-forward/dev-tools', 'scripts' => (object) []]));
69+
6970
putenv("COMPOSER={$this->tempComposerFile}");
7071
$_ENV['COMPOSER'] = $this->tempComposerFile;
7172
$_SERVER['COMPOSER'] = $this->tempComposerFile;
@@ -115,6 +116,11 @@ public function onPostPackageInstallWillInstallScripts(): void
115116
{
116117
$event = $this->prophesize(PackageEvent::class);
117118

119+
// Mock RootPackageInterface para getPackage()
120+
$package = $this->prophesize(\Composer\Package\RootPackageInterface::class);
121+
$package->getName()->willReturn('fast-forward/dev-tools');
122+
$this->composer->getPackage()->willReturn($package->reveal());
123+
118124
$event->getComposer()
119125
->willReturn($this->composer->reveal());
120126
$event->getIO()
@@ -127,8 +133,8 @@ public function onPostPackageInstallWillInstallScripts(): void
127133

128134
$data = json_decode(file_get_contents($this->tempComposerFile), true);
129135
self::assertArrayHasKey('scripts', $data);
130-
self::assertSame('./bin/dev-tools', $data['scripts']['dev-tools']);
131-
self::assertSame('./bin/dev-tools --fix', $data['scripts']['dev-tools:fix']);
136+
self::assertSame('dev-tools', $data['scripts']['dev-tools']);
137+
self::assertSame('dev-tools --fix', $data['scripts']['dev-tools:fix']);
132138
}
133139

134140
/**
@@ -139,6 +145,11 @@ public function onPostPackageUpdateWillInstallScripts(): void
139145
{
140146
$event = $this->prophesize(PackageEvent::class);
141147

148+
// Mock RootPackageInterface para getPackage()
149+
$package = $this->prophesize(\Composer\Package\RootPackageInterface::class);
150+
$package->getName()->willReturn('fast-forward/dev-tools');
151+
$this->composer->getPackage()->willReturn($package->reveal());
152+
142153
$event->getComposer()
143154
->willReturn($this->composer->reveal());
144155
$event->getIO()
@@ -151,8 +162,8 @@ public function onPostPackageUpdateWillInstallScripts(): void
151162

152163
$data = json_decode(file_get_contents($this->tempComposerFile), true);
153164
self::assertArrayHasKey('scripts', $data);
154-
self::assertSame('./bin/dev-tools', $data['scripts']['dev-tools']);
155-
self::assertSame('./bin/dev-tools --fix', $data['scripts']['dev-tools:fix']);
165+
self::assertSame('dev-tools', $data['scripts']['dev-tools']);
166+
self::assertSame('dev-tools --fix', $data['scripts']['dev-tools:fix']);
156167
}
157168

158169
/**

0 commit comments

Comments
 (0)