Sitelet https://web.archive.org/web/20220718154308/https://github.com/composer/composer/issues/10796
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

[Help Wanted] Increasing test coverage of Command classes #10796

Open
Seldaek opened this issue May 24, 2022 · 2 comments
Open

[Help Wanted] Increasing test coverage of Command classes #10796

Seldaek opened this issue May 24, 2022 · 2 comments

Comments

@Seldaek
Copy link
Member

@Seldaek Seldaek commented May 24, 2022 •

As you can see on this image, Command classes have very poor coverage currently (16% of lines covered in total):

image

Here some examples of how to write sane integration tests for most commands:

public function testCanListScripts(): void
{
$this->initTempComposer([
'scripts' => [
'test' => '@php test',
'fix-cs' => 'php-cs-fixer fix',
],
'scripts-descriptions' => [
'fix-cs' => 'Run the codestyle fixer',
],
]);
$appTester = $this->getApplicationTester();
$appTester->run(['command' => 'run-script', '--list' => true]);
$appTester->assertCommandIsSuccessful();
$output = $appTester->getDisplay();
$this->assertStringContainsString('Runs the test script as defined in composer.json.', $output, 'The default description for the test script should be printed');
$this->assertStringContainsString('Run the codestyle fixer', $output, 'The custom description for the fix-cs script should be printed');
}

/**
* @dataProvider provideConfigUpdates
* @param array<mixed> $before
* @param array<mixed> $command
* @param array<mixed> $expected
*/
public function testConfigUpdates(array $before, array $command, array $expected): void
{
$this->initTempComposer($before);
$appTester = $this->getApplicationTester();
$appTester->run(array_merge(['command' => 'config'], $command));
$appTester->assertCommandIsSuccessful($appTester->getDisplay());
$this->assertSame($expected, json_decode((string) file_get_contents('composer.json'), true));
}
public function provideConfigUpdates(): \Generator
{
yield 'set scripts' => [
[],
['setting-key' => 'scripts.test', 'setting-value' => ['foo bar']],
['scripts' => ['test' => 'foo bar']],
];
yield 'unset scripts' => [
['scripts' => ['test' => 'foo bar', 'lala' => 'baz']],
['setting-key' => 'scripts.lala', '--unset' => true],
['scripts' => ['test' => 'foo bar']],
];

$this->initTempComposer($composerJson);
$packages = [
$this->getPackage('first/pkg', '2.3.4'),
$this->getPackage('second/pkg', '3.4.0'),
];
$devPackages = [
$this->getPackage('dev/pkg', '2.3.4.5'),
];
$this->createInstalledJson($packages, $devPackages);
if ($lock) {
$this->createComposerLock($packages, $devPackages);
}
$appTester = $this->getApplicationTester();
$appTester->run(array_merge(['command' => 'bump'], $command));
$json = new JsonFile('./composer.json');
$this->assertSame($expected, $json->read());

If you feel like helping, PRs are very much welcome, I don't think we absolutely need 100% coverage but having the most common use cases covered would already be very valuable. Please target the main branch if you want to contribute some, and feel free to announce it here if you start some larger chunk of work to avoid duplicating efforts.

@theoboldalex
Copy link

@theoboldalex theoboldalex commented Jul 9, 2022

@Seldaek I'd love to take on some of this work. I have opened a first PR here #10932 and will start to work my way through the commands.

@Seldaek
Copy link
Member Author

@Seldaek Seldaek commented Jul 9, 2022

@theoboldalex great, thanks! Maybe wait until I get a chance to review the first one to make sure you don't run off and do more of them with the wrong assumptions. I'll try to get to it soon.

Also note the listing above is a bit outdated as I tried to work on this here and there when touching commands the last few months. But there is still plenty to be done :)

@Seldaek Seldaek removed this from the 2.4 milestone Jul 17, 2022
@Seldaek Seldaek added this to the 2.5 milestone Jul 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants