Skip to content

Commit

Permalink
Merge pull request #416 from ergebnis/feature/lax
Browse files Browse the repository at this point in the history
Enhancement: Apply lax validation to composer.json
  • Loading branch information
ergebnis-bot authored Apr 30, 2020
2 parents 9f73879 + da4eafa commit ae07699
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 70 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`2.4.0...master`][2.4.0...master].
For a full diff see [`2.5.0...master`][2.5.0...master].

## [`2.5.0`][2.5.0]

For a full diff see [`2.4.0...2.5.0`][2.4.0...2.5.0].

### Changed

* Apply lax validation to `composer.json` ([#416]), by [@localheinz]

## [`2.4.0`][2.4.0]

Expand Down Expand Up @@ -365,6 +373,7 @@ For a full diff see [`81bc3a8...0.1.0`][81bc3a8...0.1.0].
[2.3.1]: https://github.com/ergebnis/composer-normalize/releases/tag/2.3.1
[2.3.2]: https://github.com/ergebnis/composer-normalize/releases/tag/2.3.2
[2.4.0]: https://github.com/ergebnis/composer-normalize/releases/tag/2.4.0
[2.5.0]: https://github.com/ergebnis/composer-normalize/releases/tag/2.5.0

[81bc3a8...0.1.0]: https://github.com/ergebnis/composer-normalize/compare/81bc3a8...0.1.0
[0.1.0...0.2.0]: https://github.com/ergebnis/composer-normalize/compare/0.1.0...0.2.0
Expand Down Expand Up @@ -399,7 +408,8 @@ For a full diff see [`81bc3a8...0.1.0`][81bc3a8...0.1.0].
[2.3.0...2.3.1]: https://github.com/ergebnis/composer-normalize/compare/2.3.0...2.3.1
[2.3.1...2.3.2]: https://github.com/ergebnis/composer-normalize/compare/2.3.1...2.3.2
[2.3.2...2.4.0]: https://github.com/ergebnis/composer-normalize/compare/2.4.0...master
[2.4.0...master]: https://github.com/ergebnis/composer-normalize/compare/2.4.0...master
[2.4.0...2.5.0]: https://github.com/ergebnis/composer-normalize/compare/2.4.0...2.5.0
[2.5.0...master]: https://github.com/ergebnis/composer-normalize/compare/2.5.0...master

[#1]: https://github.com/ergebnis/composer-normalize/pull/1
[#2]: https://github.com/ergebnis/composer-normalize/pull/2
Expand Down Expand Up @@ -449,6 +459,7 @@ For a full diff see [`81bc3a8...0.1.0`][81bc3a8...0.1.0].
[#380]: https://github.com/ergebnis/composer-normalize/pull/380
[#406]: https://github.com/ergebnis/composer-normalize/pull/406
[#412]: https://github.com/ergebnis/composer-normalize/pull/412
[#416]: https://github.com/ergebnis/composer-normalize/pull/416

[@ergebnis]: https://github.com/ergebnis
[@ergebnis-bot]: https://github.com/ergebnis-bot
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ help: ## Displays this list of targets with descriptions
.PHONY: phar
phar: vendor ## Builds a phar with humbug/box
phar/box.phar validate box.json
composer require composer/composer:${COMPOSER_VERSION} --no-interaction --no-progress --no-suggest
composer require composer/composer:${COMPOSER_VERSION} --no-interaction --no-progress --no-suggest --update-with-dependencies
phar/box.phar compile --config=box.json
git checkout HEAD -- composer.json composer.lock
phar/box.phar info .build/phar/composer-normalize.phar
Expand Down
3 changes: 2 additions & 1 deletion box.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
],
"compression": "GZ",
"files": [
"resource/schema.json"
"resource/schema.json",
"resource/schema-lax.json"
],
"git": "git",
"main": "phar/composer-normalize.php",
Expand Down
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"Composer\\Factory",
"Composer\\IO\\IOInterface",
"Composer\\Json\\JsonFile",
"Composer\\Json\\JsonValidationException",
"Composer\\Plugin\\Capability\\CommandProvider",
"Composer\\Plugin\\Capable",
"Composer\\Plugin\\PluginInterface",
Expand Down
2 changes: 1 addition & 1 deletion phar/composer-normalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

$command = new Normalize\Command\NormalizeCommand(
new Factory(),
new Normalizer\Vendor\Composer\ComposerJsonNormalizer(__DIR__ . '/../resource/schema.json'),
new Normalizer\Vendor\Composer\ComposerJsonNormalizer(__DIR__ . '/../resource/schema-lax.json'),
new Normalizer\Format\Formatter(new Printer\Printer()),
new Diff\Differ(new Diff\Output\StrictUnifiedDiffOutputBuilder([
'fromFile' => 'original',
Expand Down
6 changes: 6 additions & 0 deletions resource/schema-lax.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "schema.json",
"additionalProperties": true,
"required": []
}
53 changes: 30 additions & 23 deletions src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Composer\Command;
use Composer\Factory;
use Composer\IO;
use Ergebnis\Composer\Normalize\Exception;
use Ergebnis\Json\Normalizer;
use Localheinz\Diff;
Expand Down Expand Up @@ -121,19 +122,10 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
$composerFile = Factory::getComposerFile();
}

try {
$composer = $this->factory->createComposer(
$io,
$composerFile
);
} catch (\Exception $exception) {
$io->writeError(\sprintf(
'<error>%s</error>',
$exception->getMessage()
));

return 1;
}
$composer = $this->factory->createComposer(
$io,
$composerFile
);

if (false === $input->getOption('dry-run') && !\is_writable($composerFile)) {
$io->writeError(\sprintf(
Expand All @@ -159,19 +151,22 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O

try {
$normalized = $this->normalizer->normalize($json);
} catch (Normalizer\Exception\OriginalInvalidAccordingToSchemaException | Normalizer\Exception\NormalizedInvalidAccordingToSchemaException $exception) {
$io->writeError(\sprintf(
'<error>%s</error>',
$exception->getMessage()
));
} catch (Normalizer\Exception\OriginalInvalidAccordingToSchemaException $exception) {
$io->writeError('<error>Original composer.json does not match the expected JSON schema:</error>');

$errors = $exception->errors();
$this->showValidationErrors(
$io,
...$exception->errors()
);

\array_walk($errors, static function (string $error) use ($io): void {
$io->writeError($error);
});
return 1;
} catch (Normalizer\Exception\NormalizedInvalidAccordingToSchemaException $exception) {
$io->writeError('<error>Normalized composer.json does not match the expected JSON schema:</error>');

$io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
$this->showValidationErrors(
$io,
...$exception->errors()
);

return 1;
} catch (\RuntimeException $exception) {
Expand Down Expand Up @@ -309,6 +304,18 @@ private function indentFrom(Console\Input\InputInterface $input): ?Normalizer\Fo
return $indent;
}

private function showValidationErrors(IO\IOInterface $io, string ...$errors): void
{
foreach ($errors as $error) {
$io->writeError(\sprintf(
'<error>- %s</error>',
$error
));
}

$io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
}

/**
* @param string $before
* @param string $after
Expand Down
2 changes: 1 addition & 1 deletion src/NormalizePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getCommands(): array
new Factory(),
new Normalizer\Vendor\Composer\ComposerJsonNormalizer(\sprintf(
'file://%s',
\realpath(__DIR__ . '/../resource/schema.json')
\realpath(__DIR__ . '/../resource/schema-lax.json')
)),
new Normalizer\Format\Formatter(new Printer\Printer()),
new Diff\Differ(new Diff\Output\StrictUnifiedDiffOutputBuilder([
Expand Down
6 changes: 5 additions & 1 deletion test/Fixture/json/not-valid/composer.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"description": "This composer.json is not valid as it is missing required fields."
"_comment": "This composer.json is not valid according to a lax validation, as existing fields have unexpected types",
"name": [
"foo",
"bar"
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"name": "foo/bar",
"type": "library",
"description": "This composer.json is valid, a composer.lock is not present, and composer.json is already normalized.",
"keywords": [
"foo",
"bar"
Expand All @@ -16,5 +14,6 @@
"require": {
"php": "^7.1",
"ext-json": "*"
}
},
"_comment": "This composer.json is valid according to a lax validation, a composer.lock is not present, and composer.json is already normalized."
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "foo/bar",
"type": "library",
"description": "This composer.json is valid, a composer.lock is not present, and composer.json is not yet normalized.",
"_comment": "This composer.json is valid according to a lax validation, , a composer.lock is not present, and composer.json is not yet normalized.",
"keywords": ["foo", "bar"],
"license": "MIT",
"authors": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"name": "foo/bar",
"type": "library",
"description": "This composer.json is valid, a composer.lock is present and fresh before invoking the command, and composer.json is already normalized.",
"keywords": [
"foo",
"bar"
Expand All @@ -16,5 +14,6 @@
"require": {
"php": "^7.1",
"ext-json": "*"
}
},
"_comment": "This composer.json is valid according to a lax validation, a composer.lock is present and fresh before invoking the command, and composer.json is 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
@@ -1,7 +1,6 @@
{
"name": "foo/bar",
"type": "library",
"description": "This composer.json is valid, a composer.lock is present and fresh before invoking the command, composer.json is not yet normalized, and composer.lock is still fresh after invoking the command.",
"keywords": ["foo", "bar"],
"license": "MIT",
"authors": [
Expand All @@ -10,5 +9,6 @@
"require": {
"php": "^7.1",
"ext-json": "*"
}
},
"_comment": "This composer.json is valid according to a lax validation, a composer.lock is present and fresh before invoking the command, composer.json is not yet normalized, and composer.lock is still fresh after invoking the command."
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "foo/bar",
"type": "library",
"description": "This composer.json is valid, a composer.lock is present and fresh before invoking the command, composer.json is not yet normalized, and composer.lock is not fresh after invoking the command.",
"keywords": ["foo", "bar"],
"license": "MIT",
"authors": [
Expand All @@ -10,5 +9,6 @@
"require": {
"ext-json": "*",
"php": "^7.1"
}
},
"_comment": "This composer.json is valid according to a lax validation, , a composer.lock is present and fresh before invoking the command, composer.json is not yet normalized, and composer.lock is not fresh after invoking the command."
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "foo/bar",
"type": "library",
"description": "This composer.json is valid, a composer.lock is present, but not fresh before invoking the command.",
"_comment": "This composer.json is valid according to a lax validation, a composer.lock is present, but not fresh before invoking the command.",
"keywords": [
"foo",
"bar"
Expand Down
Loading

0 comments on commit ae07699

Please sign in to comment.