Skip to content

Commit

Permalink
Merge pull request #28 from localheinz/feature/no-update-lock
Browse files Browse the repository at this point in the history
Enhancement: Add --no-update-lock option
  • Loading branch information
localheinz authored Jan 20, 2018
2 parents 7ea2ac4 + d2ef356 commit 4840417
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ The `NormalizeCommand` provided by the `NormalizePlugin` within this package wil
* write the normalized content of `composer.json` back to the file
* update the hash in `composer.lock` if it exists and if an update is necessary

### Options

* `--no-update-lock`: Do not update lock file if it exists

## Normalizers

The `ComposerJsonNormalizer` composes normalizers provided by [`localheinz/json-normalizer`](https://github.com/localheinz/json-normalizer):
Expand Down
20 changes: 15 additions & 5 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function __construct(Normalizer\NormalizerInterface $normalizer)
protected function configure()
{
$this->setDescription('Normalizes composer.json according to its JSON schema (https://getcomposer.org/schema.json).');
$this->setDefinition([
new Console\Input\InputOption(
'no-update-lock',
null,
Console\Input\InputOption::VALUE_NONE,
'Do not update lock file if it exists'
),
]);
}

protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
Expand Down Expand Up @@ -116,13 +124,15 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
$file
));

if (!$locker->isLocked()) {
return 0;
}
$noUpdateLock = $input->getOption('no-update-lock');

$io->write('<info>Updating lock file.</info>');
if (!$noUpdateLock && $locker->isLocked()) {
$io->write('<info>Updating lock file.</info>');

return $this->updateLocker($output);
}

return $this->updateLocker($output);
return 0;
}

/**
Expand Down
90 changes: 88 additions & 2 deletions test/Unit/Command/NormalizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,20 @@ public function testHasNoArguments()
$this->assertCount(0, $definition->getArguments());
}

public function testHasNoOptions()
public function testHasNoUpdateLockOption()
{
$command = new NormalizeCommand($this->prophesize(Normalizer\NormalizerInterface::class)->reveal());

$definition = $command->getDefinition();

$this->assertCount(0, $definition->getOptions());
$this->assertTrue($definition->hasOption('no-update-lock'));

$option = $definition->getOption('no-update-lock');

$this->assertNull($option->getShortcut());
$this->assertFalse($option->isValueRequired());
$this->assertFalse($option->getDefault());
$this->assertSame('Do not update lock file if it exists', $option->getDescription());
}

public function testExecuteFailsIfComposerFileDoesNotExist()
Expand Down Expand Up @@ -605,6 +612,85 @@ public function testExecuteSucceedsIfLockerIsLockedAndLockerCouldBeUpdatedAfterN
$this->assertStringEqualsFile($composerFile, $normalized);
}

public function testExecuteSucceedsIfLockerIsLockedButSkipsUpdatingLockerIfNoUpdateLockOptionIsUsed()
{
$original = $this->composerFileContent();

$normalized = \json_encode(\array_reverse(\json_decode(
$original,
true
)));

$composerFile = $this->pathToComposerFileWithContent($original);

$io = $this->prophesize(IO\ConsoleIO::class);

$io
->write(Argument::is(\sprintf(
'<info>Successfully normalized %s.</info>',
$composerFile
)))
->shouldBeCalled();

$locker = $this->prophesize(Package\Locker::class);

$locker
->isLocked()
->shouldBeCalled()
->willReturn(true);

$locker
->isFresh()
->shouldBeCalled()
->willReturn(true);

$composer = $this->prophesize(Composer::class);

$composer
->getLocker()
->shouldBeCalled()
->willReturn($locker);

$application = $this->prophesize(Application::class);

$application
->getHelperSet()
->shouldBeCalled()
->willReturn(new Console\Helper\HelperSet());

$application
->getDefinition()
->shouldBeCalled()
->willReturn($this->createDefinitionMock());

$application
->run()
->shouldNotBeCalled();

$normalizer = $this->prophesize(Normalizer\NormalizerInterface::class);

$normalizer
->normalize(Argument::is($original))
->shouldBeCalled()
->willReturn($normalized);

$command = new NormalizeCommand($normalizer->reveal());

$command->setIO($io->reveal());
$command->setComposer($composer->reveal());
$command->setApplication($application->reveal());

$tester = new Console\Tester\CommandTester($command);

$tester->execute([
'--no-update-lock' => null,
]);

$this->assertSame(0, $tester->getStatusCode());
$this->assertFileExists($composerFile);
$this->assertStringEqualsFile($composerFile, $normalized);
}

private function composerFileContent(): string
{
static $content;
Expand Down

0 comments on commit 4840417

Please sign in to comment.