Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add --diff option #303

Merged
merged 1 commit into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

For a full diff see [`2.1.2...master`][2.1.2...master].
For a full diff see [`2.2.0...master`][2.2.0...master].

## [`2.1.2`][2.1.1]
## [`2.2.0`][2.2.0]

For a full diff see [`2.1.2...2.2.0`][2.1.2...2.2.0].

### Added

* Added `--diff` option ([#303]), by [@localheinz]

## [`2.1.2`][2.1.2]

For a full diff see [`2.1.1...2.1.2`][2.1.1...2.1.2].

Expand Down Expand Up @@ -284,6 +292,7 @@ For a full diff see [`81bc3a8...0.1.0`][81bc3a8...0.1.0].
[2.1.0]: https://github.com/ergebnis/composer-normalize/releases/tag/2.1.0
[2.1.1]: https://github.com/ergebnis/composer-normalize/releases/tag/2.1.1
[2.1.2]: https://github.com/ergebnis/composer-normalize/releases/tag/2.1.2
[2.2.0]: https://github.com/ergebnis/composer-normalize/releases/tag/2.2.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 All @@ -309,7 +318,8 @@ For a full diff see [`81bc3a8...0.1.0`][81bc3a8...0.1.0].
[2.0.2...2.1.0]: https://github.com/ergebnis/composer-normalize/compare/2.0.2...2.1.0
[2.1.0...2.1.1]: https://github.com/ergebnis/composer-normalize/compare/2.1.0...2.1.1
[2.1.1...2.1.2]: https://github.com/ergebnis/composer-normalize/compare/2.1.1...2.1.2
[2.1.2...master]: https://github.com/ergebnis/composer-normalize/compare/2.1.2...master
[2.1.2...2.2.0]: https://github.com/ergebnis/composer-normalize/compare/2.1.2...2.2.0
[2.2.0...master]: https://github.com/ergebnis/composer-normalize/compare/2.2.0...master

[#1]: https://github.com/ergebnis/composer-normalize/pull/1
[#2]: https://github.com/ergebnis/composer-normalize/pull/2
Expand Down Expand Up @@ -349,6 +359,7 @@ For a full diff see [`81bc3a8...0.1.0`][81bc3a8...0.1.0].
[#292]: https://github.com/ergebnis/composer-normalize/pull/292
[#297]: https://github.com/ergebnis/composer-normalize/pull/297
[#301]: https://github.com/ergebnis/composer-normalize/pull/301
[#303]: https://github.com/ergebnis/composer-normalize/pull/303

[@ergebnis]: https://github.com/ergebnis
[@localheinz]: https://github.com/localheinz
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ The `NormalizeCommand` provided by the `NormalizePlugin` within this package wil

### Options

* `--diff`: Show the results of normalizing
* `--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
Expand Down
10 changes: 9 additions & 1 deletion src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ protected function configure(): void
Console\Input\InputArgument::OPTIONAL,
'Path to composer.json file'
),
new Console\Input\InputOption(
'diff',
null,
Console\Input\InputOption::VALUE_NONE,
'Show the results of normalizing'
),
new Console\Input\InputOption(
'dry-run',
null,
Expand Down Expand Up @@ -192,7 +198,7 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
return 0;
}

if (true === $input->getOption('dry-run')) {
if (true === $input->getOption('diff') || true === $input->getOption('dry-run')) {
$io->writeError(\sprintf(
'<error>%s is not normalized.</error>',
$composerFile
Expand All @@ -212,7 +218,9 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
'<fg=yellow>----------- end diff -----------</>',
'',
]);
}

if (true === $input->getOption('dry-run')) {
return 1;
}

Expand Down
51 changes: 51 additions & 0 deletions test/Integration/Command/NormalizeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,57 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsNo
self::assertComposerLockFileNotExists($currentState);
}

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

$initialState = $scenario->initialState();

self::assertComposerJsonFileExists($initialState);
self::assertComposerLockFileNotExists($initialState);

$application = self::createApplicationWithNormalizeCommandAsProvidedByNormalizePlugin();

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

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

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

self::assertExitCodeSame(0, $exitCode);

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

$renderedOutput = $output->fetch();

self::assertStringContainsString($expected, $renderedOutput);
self::assertStringContainsString('--- original', $renderedOutput);
self::assertStringContainsString('+++ normalized', $renderedOutput);
self::assertStringContainsString('---------- begin diff ----------', $renderedOutput);
self::assertStringContainsString('----------- end diff -----------', $renderedOutput);

$currentState = $scenario->currentState();

self::assertComposerJsonFileModified($initialState, $currentState);
self::assertComposerLockFileNotExists($currentState);
}

/**
* @dataProvider providerCommandInvocation
*
Expand Down