Skip to content

Commit

Permalink
Add --dry-run option to install/update command (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin authored Aug 19, 2022
1 parent a790af3 commit 4da3c50
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Commands/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class Update extends BaseCommand
*/
protected bool $ignoreScripts = false;

/**
* Use dry-run mode
*/
protected bool $dryRun = false;

/**
* Whether this command has already been executed
*/
Expand Down Expand Up @@ -102,7 +107,8 @@ public function handle(
bool $lockFileOnly = false,
bool $ignorePlatformReqs = false,
string $installPreference = 'none',
bool $ignoreScripts = false
bool $ignoreScripts = false,
bool $dryRun = false
) {
if ($this->executed) {
return;
Expand All @@ -112,6 +118,7 @@ public function handle(
$this->lockFileOnly = $lockFileOnly;
$this->ignorePlatformReqs = $ignorePlatformReqs;
$this->ignoreScripts = $ignoreScripts;
$this->dryRun = $dryRun;

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

public function getRawOutput(): array
{
return $this->rawOutput;
}

/**
* Returns installed packages.
*
Expand Down Expand Up @@ -335,6 +347,10 @@ public function arguments(): array
{
$arguments = [];

if ($this->dryRun) {
$arguments['--dry-run'] = true;
}

if ($this->includeDev) {
$arguments['--dev'] = true;
} else {
Expand Down
25 changes: 25 additions & 0 deletions tests/Cases/Commands/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,29 @@ public function itFailsWhenComposerJsonIsInvalidFormat(): void

$this->composer->update();
}

/**
* @test
* @testdox can run a (real) update with --dry-run option
* @covers ::handle
* @covers ::execute
*/
public function itCanRunAnUpdateRealWithDryRun()
{
$this->copyToWorkDir($this->testBasePath() . '/fixtures/valid/simple/composer.json');

// call with dryRun = true
$update = $this->composer->update(true, false, false, 'none', false, true);

// make sure no lock file gets created
$this->assertFileDoesNotExist($this->workDir . '/composer.lock');

// make sure no vendor folder gets created
$this->assertDirectoryDoesNotExist($this->workDir . '/vendor');

$this->assertNotEmpty($update->getRawOutput());

$this->assertEquals(2, $update->getLockInstalledCount());
$this->assertEquals(2, $update->getInstalledCount());
}
}

0 comments on commit 4da3c50

Please sign in to comment.