Skip to content

Commit

Permalink
Improve migration failure messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jan 18, 2024
1 parent 1ca4c54 commit d8ab07c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
38 changes: 24 additions & 14 deletions src/TextUI/Command/Commands/MigrateConfigurationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function copy;
use function file_put_contents;
use function sprintf;
use PHPUnit\TextUI\XmlConfiguration\Migrator;
use Throwable;

Expand All @@ -28,24 +29,33 @@ public function __construct(string $filename)

public function execute(): Result
{
copy($this->filename, $this->filename . '.bak');
try {
$migrated = (new Migrator)->migrate($this->filename);

$buffer = 'Created backup: ' . $this->filename . '.bak' . PHP_EOL;
$shellExitCode = Result::SUCCESS;
copy($this->filename, $this->filename . '.bak');

try {
file_put_contents(
$this->filename,
(new Migrator)->migrate($this->filename),
);
file_put_contents($this->filename, $migrated);

$buffer .= 'Migrated configuration: ' . $this->filename . PHP_EOL;
return Result::from(
sprintf(
'Created backup: %s.bak%sMigrated configuration: %s%s',
$this->filename,
PHP_EOL,
$this->filename,
PHP_EOL,
),
);
} catch (Throwable $t) {
$buffer .= 'Migration failed: ' . $t->getMessage() . PHP_EOL;

$shellExitCode = Result::FAILURE;
return Result::from(
sprintf(
'Migration of %s failed:%s%s%s',
$this->filename,
PHP_EOL,
$t->getMessage(),
PHP_EOL,
),
Result::FAILURE,
);
}

return Result::from($buffer, $shellExitCode);
}
}
15 changes: 2 additions & 13 deletions src/TextUI/Configuration/Xml/Migration/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
namespace PHPUnit\TextUI\XmlConfiguration;

use function sprintf;
use PHPUnit\Runner\Version;
use PHPUnit\Util\Xml\Loader as XmlLoader;
use PHPUnit\Util\Xml\XmlException;
Expand All @@ -30,21 +29,11 @@ public function migrate(string $filename): string
$origin = (new SchemaDetector)->detect($filename);

if (!$origin->detected()) {
throw new Exception(
sprintf(
'%s does not validate against any know schema',
$filename,
),
);
throw new Exception('The file does not validate against any know schema');
}

if ($origin->version() === Version::series()) {
throw new Exception(
sprintf(
'%s does not need to be migrated',
$filename,
),
);
throw new Exception('The file does not need to be migrated');
}

$configurationDocument = (new XmlLoader)->loadFile($filename);
Expand Down
4 changes: 2 additions & 2 deletions tests/end-to-end/migration/unsupported-schema.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ require_once __DIR__ . '/../../bootstrap.php';
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Created backup: %sphpunit.xml.bak
Migration failed: %s does not validate against any know schema
Migration of %s failed:
The file does not validate against any know schema
--CLEAN--
<?php declare(strict_types=1);
unlink(sys_get_temp_dir() . '/phpunit.xml');
Expand Down

0 comments on commit d8ab07c

Please sign in to comment.