diff --git a/src/Command/NormalizeCommand.php b/src/Command/NormalizeCommand.php index 82de6440..60bcfae3 100644 --- a/src/Command/NormalizeCommand.php +++ b/src/Command/NormalizeCommand.php @@ -21,6 +21,7 @@ use Ergebnis\Composer\Normalize\Exception; use Ergebnis\Composer\Normalize\Version; use Ergebnis\Json\Normalizer; +use Ergebnis\Json\Normalizer\Format\JsonEncodeOptions; use Ergebnis\Json\Printer; use Localheinz\Diff; use Symfony\Component\Console; @@ -189,6 +190,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..f0afefe1 --- /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" + } +}