diff --git a/infection.json b/infection.json index e0d1158ff..b10e0e7b4 100644 --- a/infection.json +++ b/infection.json @@ -1,4 +1,5 @@ { + "$schema": "./resources/schema.json", "timeout": 25, "source": { "directories": [ diff --git a/resources/schema.json b/resources/schema.json index 1082128a9..a274eebe3 100644 --- a/resources/schema.json +++ b/resources/schema.json @@ -6,6 +6,9 @@ "source" ], "properties": { + "$schema": { + "type": "string" + }, "timeout": { "type": "number", "description": "The allowed timeout in seconds.", diff --git a/src/Command/ConfigureCommand.php b/src/Command/ConfigureCommand.php index ddc09d050..88a6f3026 100644 --- a/src/Command/ConfigureCommand.php +++ b/src/Command/ConfigureCommand.php @@ -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; @@ -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; /** @@ -178,6 +184,8 @@ private function saveConfig( ): void { $configObject = new stdClass(); + $configObject->{'$schema'} = $this->getJsonSchemaPathOrUrl(); + $configObject->source = new stdClass(); if ($sourceDirs) { @@ -217,7 +225,7 @@ 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) ); } @@ -225,4 +233,25 @@ 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); + } } diff --git a/src/Console/Application.php b/src/Console/Application.php index 63fdcce9e..5f8a41022 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -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 = ' ____ ____ __ _ diff --git a/tests/e2e/Configure/infection.json.test b/tests/e2e/Configure/infection.json.test index c3bfc8d7d..1ad7b2491 100644 --- a/tests/e2e/Configure/infection.json.test +++ b/tests/e2e/Configure/infection.json.test @@ -1,4 +1,5 @@ { + "$schema": "https://raw.githubusercontent.com/infection/infection/master/resources/schema.json", "source": { "directories": [ "src"