Skip to content

Commit

Permalink
Merge pull request #139 from localheinz/fix/update
Browse files Browse the repository at this point in the history
Fix: Update locker when locker is not fresh after normalization
  • Loading branch information
localheinz authored Dec 24, 2018
2 parents 0ffb6c9 + d653003 commit fb907fb
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ For a full diff see [`0.9.0...1.0.0`](https://github.com/localheinz/composer-nor

* Added this changelog ([#94](https://github.com/localheinz/composer-normalize/pull/94)), by [@localheinz](https://github.com/localheinz)

#### Fixed

* Force reading `composer.json` and `composer.lock` after normalization to ensure `composer.lock` is updated when not fresh after normalization ([#139](https://github.com/localheinz/composer-normalize/pull/139)), by [@localheinz](https://github.com/localheinz)

#### Removed

* Removed normalizers after extracting package [`localheinz/composer-json-normalizer`](https://github.com/localheinz/composer-json-normalizer) ([#106](https://github.com/localheinz/composer-normalize/pull/106)), by [@localheinz](https://github.com/localheinz)
Expand Down
37 changes: 37 additions & 0 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
if (false === $noUpdateLock && true === $locker->isLocked()) {
$io->write('<info>Updating lock file.</info>');

$this->resetComposer();

$file = $input->getArgument('file');

if (\is_string($file)) {
return $this->updateLockerInWorkingDirectory(
$output,
\dirname($file)
);
}

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

Expand Down Expand Up @@ -380,4 +391,30 @@ private function updateLocker(Console\Output\OutputInterface $output): int
$output
);
}

/**
* @see https://getcomposer.org/doc/03-cli.md#update
*
* @param Console\Output\OutputInterface $output
* @param string $workingDirectory
*
* @throws \Exception
*
* @return int
*/
private function updateLockerInWorkingDirectory(Console\Output\OutputInterface $output, string $workingDirectory): int
{
return $this->getApplication()->run(
new Console\Input\ArrayInput([
'command' => 'update',
'--lock' => true,
'--no-autoloader' => true,
'--no-plugins' => true,
'--no-scripts' => true,
'--no-suggest' => true,
'--working-dir' => $workingDirectory,
]),
$output
);
}
}
61 changes: 61 additions & 0 deletions test/Integration/NormalizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,56 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsPr
self::assertComposerLockFileNotModified($initialState, $currentState);
}

/**
* @dataProvider providerCommandInvocation
*
* @param CommandInvocation $commandInvocation
*/
public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsPresentAndFreshBeforeAndComposerJsonIsNotYetNormalizedAndComposerLockIsNotFreshAfter(CommandInvocation $commandInvocation): void
{
$scenario = $this->createScenario(
$commandInvocation,
__DIR__ . '/../Fixture/json/valid/lock/present/lock/fresh-before/json/not-yet-normalized/lock/not-fresh-after'
);

$initialState = $scenario->initialState();

self::assertComposerJsonFileExists($initialState);
self::assertComposerLockFileExists($initialState);
self::assertComposerLockFileFresh($initialState);

$application = $this->createApplication(new NormalizeCommand(
new Factory(),
new ComposerJsonNormalizer(),
new Formatter(),
new Differ()
));

$input = new Console\Input\ArrayInput($scenario->consoleParameters());

$output = new Console\Output\BufferedOutput();

$exitCode = $application->run(
$input,
$output
);

self::assertExitCodeSame(0, $exitCode);

$expected = \sprintf(
'Successfully normalized %s.',
$scenario->composerJsonFileReference()
);

self::assertContains($expected, $output->fetch());

$currentState = $scenario->currentState();

self::assertComposerJsonFileModified($initialState, $currentState);
self::assertComposerLockFileModified($initialState, $currentState);
self::assertComposerLockFileFresh($currentState);
}

/**
* @dataProvider providerCommandInvocation
*
Expand Down Expand Up @@ -989,6 +1039,17 @@ private static function assertComposerLockFileNotFresh(State $state): void
));
}

private static function assertComposerLockFileModified(State $expected, State $actual): void
{
self::assertComposerLockFileExists($actual);

self::assertJsonStringNotEqualsJsonString(
$expected->composerLockFile()->contents(),
$actual->composerLockFile()->contents(),
'Failed asserting that initial composer.lock has been modified.'
);
}

private static function assertComposerLockFileNotModified(State $expected, State $actual): void
{
self::assertComposerLockFileExists($actual);
Expand Down

0 comments on commit fb907fb

Please sign in to comment.