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

Add $schema to generated infection.json config file for autocomple… #1553

Merged
merged 3 commits into from Aug 12, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions infection.json
@@ -1,4 +1,5 @@
{
"$schema": "./resources/schema.json",
"timeout": 25,
"source": {
"directories": [
Expand Down
3 changes: 3 additions & 0 deletions resources/schema.json
Expand Up @@ -6,6 +6,9 @@
"source"
],
"properties": {
"$schema": {
"type": "string"
},
"timeout": {
"type": "number",
"description": "The allowed timeout in seconds.",
Expand Down
31 changes: 30 additions & 1 deletion src/Command/ConfigureCommand.php
Expand Up @@ -47,11 +47,15 @@
use Infection\Config\ValueProvider\TestFrameworkConfigPathProvider;
use Infection\Config\ValueProvider\TextLogFileProvider;
use Infection\Configuration\Schema\SchemaConfigurationLoader;
use Infection\Console\Application;
use Infection\Console\IO;
use Infection\FileSystem\Finder\TestFrameworkFinder;
use Infection\TestFramework\Config\TestFrameworkConfigLocator;
use Infection\TestFramework\TestFrameworkTypes;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use OutOfBoundsException;
use PackageVersions\Versions;
use RuntimeException;
use function Safe\file_get_contents;
use function Safe\file_put_contents;
Expand All @@ -60,6 +64,8 @@
use function Safe\json_encode;
use function Safe\sprintf;
use stdClass;
use function strpos;
use function strstr;
use Symfony\Component\Console\Input\InputOption;

/**
Expand Down Expand Up @@ -178,6 +184,8 @@ private function saveConfig(
): void {
$configObject = new stdClass();

$configObject->{'$schema'} = $this->getJsonSchemaPathOrUrl();

$configObject->source = new stdClass();

if ($sourceDirs) {
Expand Down Expand Up @@ -217,12 +225,33 @@ private function saveConfig(

file_put_contents(
SchemaConfigurationLoader::DEFAULT_CONFIG_FILE,
json_encode($configObject, JSON_PRETTY_PRINT)
json_encode($configObject, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
);
}

private function abort(): void
{
throw new RuntimeException('Configuration generation aborted');
}

private function getJsonSchemaPathOrUrl(): string
{
$fileName = 'vendor/infection/infection/resources/schema.json';

if (file_exists($fileName)) {
return $fileName;
}

try {
$version = strstr(Versions::getVersion(Application::PACKAGE_NAME), '@', true);

if ($version === false || strpos($version, 'dev-') === 0) {
$version = 'master';
}
} catch (OutOfBoundsException $e) {
$version = 'master';
}

return sprintf('https://raw.githubusercontent.com/infection/infection/%s/resources/schema.json', $version);
}
}
4 changes: 2 additions & 2 deletions src/Console/Application.php
Expand Up @@ -55,9 +55,9 @@
*/
final class Application extends BaseApplication
{
private const NAME = 'Infection - PHP Mutation Testing Framework';
public const PACKAGE_NAME = 'infection/infection';

private const PACKAGE_NAME = 'infection/infection';
private const NAME = 'Infection - PHP Mutation Testing Framework';

private const LOGO = '
____ ____ __ _
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/Configure/infection.json.test
@@ -1,4 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/infection/infection/master/resources/schema.json",
"source": {
"directories": [
"src"
Expand Down