From bd670df7f782a7539458aa98a554eae1d37c989a Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 8 Nov 2022 19:36:04 +0000 Subject: [PATCH 1/2] Ensure slashes and UTF-8 characters stay unescaped --- composer.json | 1 + composer.lock | 3 +- src/Command/NormalizeCommand.php | 5 ++ .../ExtraSlashes/ShouldBeRemoved/Test.php | 78 +++++++++++++++++++ .../ShouldBeRemoved/fixture/composer.json | 10 +++ 5 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php create mode 100644 test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/fixture/composer.json diff --git a/composer.json b/composer.json index 9540057e..d36be0a0 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ }, "require": { "php": "^7.4 || ^8.0", + "ext-json": "*", "composer-plugin-api": "^2.0.0", "ergebnis/json-normalizer": "~2.1.0", "ergebnis/json-printer": "^3.2.0", diff --git a/composer.lock b/composer.lock index 9cdd31f8..f71c6175 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0b06b826f3544a99b3925e725363b69b", + "content-hash": "11f5cabccd23b8b167889b0f1bc53506", "packages": [ { "name": "ergebnis/json-normalizer", @@ -5745,6 +5745,7 @@ "prefer-lowest": false, "platform": { "php": "^7.4 || ^8.0", + "ext-json": "*", "composer-plugin-api": "^2.0.0" }, "platform-dev": [], diff --git a/src/Command/NormalizeCommand.php b/src/Command/NormalizeCommand.php index f216e6b6..918fa309 100644 --- a/src/Command/NormalizeCommand.php +++ b/src/Command/NormalizeCommand.php @@ -22,6 +22,7 @@ use Ergebnis\Composer\Normalize\Exception; use Ergebnis\Composer\Normalize\Version; use Ergebnis\Json\Normalizer; +use Ergebnis\Json\Normalizer\Format\JsonEncodeOptions; use Localheinz\Diff; use Symfony\Component\Console; @@ -229,6 +230,10 @@ protected function execute( $format = Normalizer\Format\Format::fromJson($json); + // Apply opinionated formatting rules to composer.json files. + $format = $format->withHasFinalNewLine(true); + $format = $format->withJsonEncodeOptions(JsonEncodeOptions::fromInt(\JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE)); + if (null !== $indent) { $format = $format->withIndent($indent); } diff --git a/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php b/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php new file mode 100644 index 00000000..7e855303 --- /dev/null +++ b/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php @@ -0,0 +1,78 @@ +initialState(); + + self::assertComposerJsonFileExists($initialState); + self::assertComposerLockFileNotExists($initialState); + + $application = self::createApplicationWithNormalizeCommandAsProvidedByNormalizePlugin(); + + $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::assertStringContainsString($expected, $output->fetch()); + + $currentState = $scenario->currentState(); + + self::assertComposerJsonFileModified($initialState, $currentState); + self::assertComposerLockFileNotExists($currentState); + + $newContent = $currentState->composerJsonFile()->contents(); + + self::assertStringNotContainsString('"vendor\/package-one":', $newContent); + self::assertStringNotContainsString('"vendor\/package-two":', $newContent); + + self::assertStringContainsString('"vendor/package-one":', $newContent); + self::assertStringContainsString('"vendor/package-two":', $newContent); + } +} diff --git a/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/fixture/composer.json b/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/fixture/composer.json new file mode 100644 index 00000000..4d9e84f7 --- /dev/null +++ b/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/fixture/composer.json @@ -0,0 +1,10 @@ +{ + "license": "MIT", + "type": "library", + "_comment": "This composer.json is valid according to a lax validation, a composer.lock is not present, and composer.json is not yet normalized.", + "require": { + "php": "^8.1", + "vendor\/package-one": "^1.0", + "vendor/package-two": "^1.0" + } +} From ebf183bba73ff94504b0bfc7fe043c51891e67d9 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Tue, 17 Jan 2023 22:18:03 +1300 Subject: [PATCH 2/2] Apply changes from php-cs-fixer --- .../Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php b/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php index 7e855303..f0afefe1 100644 --- a/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php +++ b/test/Integration/Command/NormalizeCommand/Json/Valid/ExtraSlashes/ShouldBeRemoved/Test.php @@ -3,7 +3,7 @@ declare(strict_types=1); /** - * Copyright (c) 2018-2022 Andreas Möller + * Copyright (c) 2018-2023 Andreas Möller * * For the full copyright and license information, please view * the LICENSE.md file that was distributed with this source code.