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

Fix: Revert removal of file argument #168

Merged
merged 1 commit into from
Feb 15, 2019
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
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ For a full diff see [`1.x...master`](https://github.com/localheinz/composer-norm

* The constructor of `NormalizeCommand` now requires an implementation of `Localheinz\Json\Normalizer\Format\FormatterInterface`, as well as an instance of `Sebastian\Diff\Differ` to be injected ([#118](https://github.com/localheinz/composer-normalize/pull/118)), by [@localheinz](https://github.com/localheinz)

#### Removed

* Removed the `file` argument of the `NormalizeCommand` as the same functionality can be achieved using the `--working-dir` option ([#151](https://github.com/localheinz/composer-normalize/pull/151)), by [@localheinz](https://github.com/localheinz)

## `1.x`

### Unreleased
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ The `NormalizeCommand` provided by the `NormalizePlugin` within this package wil
:bulb: Interested in what `ComposerJsonNormalizer` does? Head over to
[`localheinz/composer-json-normalizer`](https://github.com/localheinz/composer-json-normalizer#normalizers) for a full explanation, or take a look at the [examples](https://github.com/localheinz/composer-normalize#examples)

### Arguments

* `file`: Path to composer.json file (optional, defaults to `composer.json` in working directory)

### Options

* `--dry-run`: Show the results of normalizing, but do not modify any files
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ parameters:
classesAllowedToBeExtended:
- Composer\Command\BaseCommand
ignoreErrors:
- '#Call to deprecated method usingFileArgument\(\) of class Localheinz\\Composer\\Normalize\\Test\\Util\\CommandInvocation.#'
- '#Method Localheinz\\Composer\\Normalize\\Command\\NormalizeCommand::indentFrom\(\) has a nullable return type declaration.#'
paths:
- src
Expand Down
48 changes: 47 additions & 1 deletion src/Command/NormalizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ protected function configure(): void
{
$this->setDescription('Normalizes composer.json according to its JSON schema (https://getcomposer.org/schema.json).');
$this->setDefinition([
new Console\Input\InputArgument(
'file',
Console\Input\InputArgument::OPTIONAL,
'Path to composer.json file (deprecated, use --working-dir instead)'
),
new Console\Input\InputOption(
'dry-run',
null,
Expand Down Expand Up @@ -112,7 +117,13 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
return 1;
}

$composerFile = Factory::getComposerFile();
$composerFile = $input->getArgument('file');

if (null === $composerFile) {
$composerFile = Factory::getComposerFile();
} else {
$io->write('<fg=yellow>Note: The file argument is deprecated and will be removed in 2.0.0. Please use the --working-dir option instead.</fg>');
}

try {
$composer = $this->factory->createComposer(
Expand Down Expand Up @@ -230,6 +241,15 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O

$this->resetComposer();

$file = $input->getArgument('file');

if (\is_string($file)) {
return $this->updateLockerInWorkingDirectory(
$output,
\dirname($file)
);
}

return $this->updateLocker($output);
}

Expand Down Expand Up @@ -369,4 +389,30 @@ private function updateLocker(Console\Output\OutputInterface $output): int
$output
);
}

/**
* @see https://getcomposer.org/doc/03-cli.md#update
*
* @param Console\Output\OutputInterface $output
* @param string $workingDirectory
*
* @throws \Exception
*
* @return int
*/
private function updateLockerInWorkingDirectory(Console\Output\OutputInterface $output, string $workingDirectory): int
{
return $this->getApplication()->run(
new Console\Input\ArrayInput([
'command' => 'update',
'--lock' => true,
'--no-autoloader' => true,
'--no-plugins' => true,
'--no-scripts' => true,
'--no-suggest' => true,
'--working-dir' => $workingDirectory,
]),
$output
);
}
}
128 changes: 118 additions & 10 deletions test/Integration/NormalizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,13 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsNo
);

self::assertExitCodeSame(0, $exitCode);
self::assertContains('./composer.json is already normalized.', $output->fetch());

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

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

Expand Down Expand Up @@ -398,7 +404,13 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsNo
);

self::assertExitCodeSame(0, $exitCode);
self::assertContains('Successfully normalized ./composer.json.', $output->fetch());

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

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

$currentState = $scenario->currentState();

Expand Down Expand Up @@ -445,7 +457,12 @@ public function testFailsWhenComposerJsonIsPresentAndValidAndComposerLockIsNotPr

$renderedOutput = $output->fetch();

self::assertContains('./composer.json is not normalized.', $renderedOutput);
$expected = \sprintf(
'%s is not normalized.',
$scenario->composerJsonFileReference()
);

self::assertContains($expected, $renderedOutput);
self::assertContains('--- original', $renderedOutput);
self::assertContains('+++ normalized', $renderedOutput);
self::assertContains('---------- begin diff ----------', $renderedOutput);
Expand Down Expand Up @@ -495,7 +512,13 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsNo
);

self::assertExitCodeSame(0, $exitCode);
self::assertContains('Successfully normalized ./composer.json.', $output->fetch());

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

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

$currentState = $scenario->currentState();

Expand Down Expand Up @@ -539,7 +562,13 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsNo
);

self::assertExitCodeSame(0, $exitCode);
self::assertContains('Successfully normalized ./composer.json.', $output->fetch());

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

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

$currentState = $scenario->currentState();

Expand Down Expand Up @@ -621,7 +650,13 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsPr
);

self::assertExitCodeSame(0, $exitCode);
self::assertContains('./composer.json is already normalized.', $output->fetch());

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

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

Expand Down Expand Up @@ -660,7 +695,13 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsPr
);

self::assertExitCodeSame(0, $exitCode);
self::assertContains('Successfully normalized ./composer.json.', $output->fetch());

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

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

$currentState = $scenario->currentState();

Expand Down Expand Up @@ -703,7 +744,62 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsPr
);

self::assertExitCodeSame(0, $exitCode);
self::assertContains('Successfully normalized ./composer.json.', $output->fetch());

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

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

$currentState = $scenario->currentState();

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

public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsPresentAndFreshBeforeAndComposerJsonIsNotYetNormalizedAndComposerLockIsNotFreshAfterAndInformsWhenFileArgumentIsUsed(): void
{
$scenario = $this->createScenario(
CommandInvocation::usingFileArgument(),
__DIR__ . '/../Fixture/json/valid/lock/present/lock/fresh-before/json/not-yet-normalized/lock/not-fresh-after'
);

$initialState = $scenario->initialState();

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

$application = $this->createApplication(new NormalizeCommand(
new Factory(),
new ComposerJsonNormalizer(),
new Formatter(),
new Differ()
));

$input = new Console\Input\ArrayInput($scenario->consoleParameters());

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

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

self::assertExitCodeSame(0, $exitCode);

$renderedOutput = $output->fetch();

self::assertContains('Note: The file argument is deprecated and will be removed in 2.0.0. Please use the --working-dir option instead.', $renderedOutput);

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

self::assertContains($expected, $renderedOutput);

$currentState = $scenario->currentState();

Expand Down Expand Up @@ -751,7 +847,12 @@ public function testFailsWhenComposerJsonIsPresentAndValidAndComposerLockIsPrese

$renderedOutput = $output->fetch();

self::assertContains('./composer.json is not normalized.', $renderedOutput);
$expected = \sprintf(
'%s is not normalized.',
$scenario->composerJsonFileReference()
);

self::assertContains($expected, $renderedOutput);
self::assertContains('---------- begin diff ----------', $renderedOutput);
self::assertContains('----------- end diff -----------', $renderedOutput);
self::assertEquals($initialState, $scenario->currentState());
Expand Down Expand Up @@ -794,7 +895,13 @@ public function testSucceedsWhenComposerJsonIsPresentAndValidAndComposerLockIsPr
);

self::assertExitCodeSame(0, $exitCode);
self::assertContains('Successfully normalized ./composer.json.', $output->fetch());

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

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

$currentState = $scenario->currentState();

Expand Down Expand Up @@ -924,6 +1031,7 @@ private function commandInvocations(): array
{
return [
CommandInvocation::inCurrentWorkingDirectory(),
CommandInvocation::usingFileArgument(),
CommandInvocation::usingWorkingDirectoryOption(),
];
}
Expand Down
10 changes: 10 additions & 0 deletions test/Util/CommandInvocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public static function inCurrentWorkingDirectory(): self
return new self('in-current-working-directory');
}

/**
* @deprecated The file argument will be removed in 2.0.0.
*
* @return CommandInvocation
*/
public static function usingFileArgument(): self
{
return new self('using-file-argument');
}

public static function usingWorkingDirectoryOption(): self
{
return new self('using-working-directory-option');
Expand Down
21 changes: 21 additions & 0 deletions test/Util/Scenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ public function consoleParameters(): array
'command' => 'normalize',
];

if ($this->commandInvocation->is(CommandInvocation::usingFileArgument())) {
return \array_merge($parameters, [
'file' => \sprintf(
'%s/composer.json',
$this->initialState->directory()->path()
),
]);
}

if ($this->commandInvocation->is(CommandInvocation::usingWorkingDirectoryOption())) {
return \array_merge($parameters, [
'--working-dir' => $this->initialState->directory()->path(),
Expand All @@ -93,4 +102,16 @@ public function consoleParameters(): array

return $parameters;
}

public function composerJsonFileReference(): string
{
if ($this->commandInvocation->is(CommandInvocation::usingFileArgument())) {
return \sprintf(
'%s/composer.json',
$this->initialState->directory()->path()
);
}

return './composer.json';
}
}