Skip to content

Commit 4da3c50

Browse files
authored
Add --dry-run option to install/update command (#2)
1 parent a790af3 commit 4da3c50

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Commands/Update.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class Update extends BaseCommand
4848
*/
4949
protected bool $ignoreScripts = false;
5050

51+
/**
52+
* Use dry-run mode
53+
*/
54+
protected bool $dryRun = false;
55+
5156
/**
5257
* Whether this command has already been executed
5358
*/
@@ -102,7 +107,8 @@ public function handle(
102107
bool $lockFileOnly = false,
103108
bool $ignorePlatformReqs = false,
104109
string $installPreference = 'none',
105-
bool $ignoreScripts = false
110+
bool $ignoreScripts = false,
111+
bool $dryRun = false
106112
) {
107113
if ($this->executed) {
108114
return;
@@ -112,6 +118,7 @@ public function handle(
112118
$this->lockFileOnly = $lockFileOnly;
113119
$this->ignorePlatformReqs = $ignorePlatformReqs;
114120
$this->ignoreScripts = $ignoreScripts;
121+
$this->dryRun = $dryRun;
115122

116123
if (in_array($installPreference, [self::PREFER_NONE, self::PREFER_DIST, self::PREFER_SOURCE])) {
117124
$this->installPreference = $installPreference;
@@ -166,6 +173,11 @@ public function isSuccessful(): bool
166173
return $this->successful === true;
167174
}
168175

176+
public function getRawOutput(): array
177+
{
178+
return $this->rawOutput;
179+
}
180+
169181
/**
170182
* Returns installed packages.
171183
*
@@ -335,6 +347,10 @@ public function arguments(): array
335347
{
336348
$arguments = [];
337349

350+
if ($this->dryRun) {
351+
$arguments['--dry-run'] = true;
352+
}
353+
338354
if ($this->includeDev) {
339355
$arguments['--dev'] = true;
340356
} else {

tests/Cases/Commands/UpdateTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,29 @@ public function itFailsWhenComposerJsonIsInvalidFormat(): void
359359

360360
$this->composer->update();
361361
}
362+
363+
/**
364+
* @test
365+
* @testdox can run a (real) update with --dry-run option
366+
* @covers ::handle
367+
* @covers ::execute
368+
*/
369+
public function itCanRunAnUpdateRealWithDryRun()
370+
{
371+
$this->copyToWorkDir($this->testBasePath() . '/fixtures/valid/simple/composer.json');
372+
373+
// call with dryRun = true
374+
$update = $this->composer->update(true, false, false, 'none', false, true);
375+
376+
// make sure no lock file gets created
377+
$this->assertFileDoesNotExist($this->workDir . '/composer.lock');
378+
379+
// make sure no vendor folder gets created
380+
$this->assertDirectoryDoesNotExist($this->workDir . '/vendor');
381+
382+
$this->assertNotEmpty($update->getRawOutput());
383+
384+
$this->assertEquals(2, $update->getLockInstalledCount());
385+
$this->assertEquals(2, $update->getInstalledCount());
386+
}
362387
}

0 commit comments

Comments
 (0)