Skip to content

Commit

Permalink
Enhancement: Add --no-check-lock option to allow skipping validation …
Browse files Browse the repository at this point in the history
…of composer.lock
  • Loading branch information
localheinz committed Aug 16, 2020
1 parent 05354a3 commit 156c801
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ For a full diff see [`2.7.0...main`][2.7.0...main].

For a full diff see [`2.6.1...2.7.0`][2.6.1...2.7.0].

### Added

* Added `--no-check-lock` option which allows skipping validation of `composer.lock` ([#515]), by [@localheinz]

### Changed

* Updated `schema.json` ([#512]), by [@ergebnis-bot]
Expand Down Expand Up @@ -521,6 +525,7 @@ For a full diff see [`81bc3a8...0.1.0`][81bc3a8...0.1.0].
[#485]: https://github.com/ergebnis/composer-normalize/pull/485
[#487]: https://github.com/ergebnis/composer-normalize/pull/487
[#512]: https://github.com/ergebnis/composer-normalize/pull/512
[#515]: https://github.com/ergebnis/composer-normalize/pull/515

[@core23]: https://github.com/core23
[@dependabot]: https://github.com/dependabot
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ to normalize `composer.json` in the working directory.
The `NormalizeCommand` provided by the `NormalizePlugin` within this package will

* determine whether a `composer.json` exists
* determine whether a `composer.lock` exists, and if so, whether it is up to date
* determine whether a `composer.lock` exists, and if so, whether it is up to date (unless the `--no-check-lock` option is used)
* use the `ComposerJsonNormalizer` from [`ergebnis/composer-json-normalizer`](https://github.com/ergebnis/composer-json-normalizer) to normalize the content of `composer.json`
* format the normalized content (either as sniffed, or as specified using the `--indent-size` and `--indent-style` options)
* write the normalized and formatted content of `composer.json` back to the file
Expand All @@ -128,6 +128,7 @@ The `NormalizeCommand` provided by the `NormalizePlugin` within this package wil
* `--dry-run`: Show the results of normalizing, but do not modify any files
* `--indent-size`: Indent size (an integer greater than 0); should be used with the `--indent-style` option
* `--indent-style`: Indent style (one of "space", "tab"); should be used with the `--indent-size` option
* `--no-check-lock`: Do not check if lock file is up to date
* `--no-update-lock`: Do not update lock file if it exists

### Continuous Integration
Expand Down
8 changes: 7 additions & 1 deletion src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ protected function configure(): void
\implode('", "', \array_keys(self::$indentStyles))
)
),
new Console\Input\InputOption(
'no-check-lock',
null,
Console\Input\InputOption::VALUE_NONE,
'Do not check if lock file is up to date'
),
new Console\Input\InputOption(
'no-update-lock',
null,
Expand Down Expand Up @@ -139,7 +145,7 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O

$locker = $composer->getLocker();

if ($locker->isLocked() && !$locker->isFresh()) {
if (false === $input->getOption('no-check-lock') && $locker->isLocked() && !$locker->isFresh()) {
$io->writeError('<error>The lock file is not up to date with the latest changes in composer.json, it is recommended that you run `composer update --lock`.</error>');

return 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "library",
"keywords": [
"foo",
"bar"
],
"license": "MIT",
"authors": [
{
"name": "Andreas Möller",
"email": "[email protected]"
}
],
"require": {
"php": "^5.6",
"ext-json": "*"
},
"_comment": "This composer.json is valid according to a lax validation, a composer.lock is present, but not fresh before invoking the command with the --no-check-lock option, and already normalized."
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "library",
"_comment": "This composer.json is valid according to a lax validation, a composer.lock is present, but not fresh before invoking the command with the --no-check-lock option, and not yet normalized.",
"keywords": [
"foo",
"bar"
],
"license": "MIT",
"authors": [
{
"name": "Andreas Möller",
"email": "[email protected]"
}
],
"require": {
"ext-json": "*",
"php": "^5.6"
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 90 additions & 1 deletion test/Integration/Command/NormalizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public function testFailsWhenComposerJsonIsPresentAndValidAccordingToLaxValidati
{
$scenario = self::createScenario(
$commandInvocation,
__DIR__ . '/../../Fixture/json/valid/lock/present/lock/not-fresh-before'
__DIR__ . '/../../Fixture/json/valid/lock/present/lock/not-fresh-before/no-check-lock/false'
);

$initialState = $scenario->initialState();
Expand All @@ -615,6 +615,95 @@ public function testFailsWhenComposerJsonIsPresentAndValidAccordingToLaxValidati
self::assertEquals($initialState, $scenario->currentState());
}

/**
* @dataProvider providerCommandInvocation
*
* @param CommandInvocation $commandInvocation
*/
public function testSucceedsWhenComposerJsonIsPresentAndValidAccordingToLaxValidationAndComposerLockIsPresentButNotFreshBeforeNoCheckLockOptionIsUsedAndComposerJsonIsAlreadyNormalized(CommandInvocation $commandInvocation): void
{
$scenario = self::createScenario(
$commandInvocation,
__DIR__ . '/../../Fixture/json/valid/lock/present/lock/not-fresh-before/no-check-lock/true/json/already-normalized'
);

$initialState = $scenario->initialState();

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

$application = self::createApplicationWithNormalizeCommandAsProvidedByNormalizePlugin();

$input = new Console\Input\ArrayInput($scenario->consoleParametersWith([
'--no-check-lock' => true,
]));

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

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

self::assertExitCodeSame(0, $exitCode);

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

self::assertStringContainsString($expected, $output->fetch());
self::assertEquals($initialState, $scenario->currentState());
}

/**
* @dataProvider providerCommandInvocation
*
* @param CommandInvocation $commandInvocation
*/
public function testSucceedsWhenComposerJsonIsPresentAndValidAccordingToLaxValidationAndComposerLockIsPresentButNotFreshBeforeNoCheckLockOptionIsUsedAndComposerJsonIsNotYetNormalized(CommandInvocation $commandInvocation): void
{
$scenario = self::createScenario(
$commandInvocation,
__DIR__ . '/../../Fixture/json/valid/lock/present/lock/not-fresh-before/no-check-lock/true/json/not-yet-normalized'
);

$initialState = $scenario->initialState();

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

$application = self::createApplicationWithNormalizeCommandAsProvidedByNormalizePlugin();

$input = new Console\Input\ArrayInput($scenario->consoleParametersWith([
'--no-check-lock' => true,
]));

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

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

self::assertExitCodeSame(0, $exitCode);

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

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

$currentState = $scenario->currentState();

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

/**
* @dataProvider providerCommandInvocation
*
Expand Down

0 comments on commit 156c801

Please sign in to comment.