Skip to content

Commit

Permalink
fix errors output in stdout making test fails with cryptic errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Feb 1, 2024
1 parent bdcbb0f commit a929aa5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
31 changes: 20 additions & 11 deletions src/Psalm/Internal/Cli/Psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ public static function run(array $argv): void

if (isset($options['set-baseline'])) {
if (is_array($options['set-baseline'])) {
die('Only one baseline file can be created at a time' . PHP_EOL);
fwrite(STDERR, 'Only one baseline file can be created at a time' . PHP_EOL);
exit(1);
}
}

Expand Down Expand Up @@ -486,7 +487,8 @@ static function (string $arg): void {
private static function generateConfig(string $current_dir, array &$args): void
{
if (file_exists($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml')) {
die('A config file already exists in the current directory' . PHP_EOL);
fwrite(STDERR, 'A config file already exists in the current directory' . PHP_EOL);
exit(1);
}

$args = array_values(array_filter(
Expand All @@ -507,12 +509,14 @@ private static function generateConfig(string $current_dir, array &$args): void
$init_source_dir = null;
if (count($args)) {
if (count($args) > 2) {
die('Too many arguments provided for psalm --init' . PHP_EOL);
fwrite(STDERR, 'Too many arguments provided for psalm --init' . PHP_EOL);
exit(1);
}

if (isset($args[1])) {
if (!preg_match('/^[1-8]$/', $args[1])) {
die('Config strictness must be a number between 1 and 8 inclusive' . PHP_EOL);
fwrite(STDERR, 'Config strictness must be a number between 1 and 8 inclusive' . PHP_EOL);
exit(1);
}

$init_level = (int)$args[1];
Expand All @@ -532,11 +536,13 @@ private static function generateConfig(string $current_dir, array &$args): void
$vendor_dir,
);
} catch (ConfigCreationException $e) {
die($e->getMessage() . PHP_EOL);
fwrite(STDERR, $e->getMessage() . PHP_EOL);
exit(1);
}

if (!file_put_contents($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml', $template_contents)) {
die('Could not write to psalm.xml' . PHP_EOL);
if (file_put_contents($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml', $template_contents) === false) {
fwrite(STDERR, 'Could not write to psalm.xml' . PHP_EOL);
exit(1);
}

exit('Config file created successfully. Please re-run psalm.' . PHP_EOL);
Expand Down Expand Up @@ -681,7 +687,8 @@ private static function updateBaseline(array $options, Config $config): array
$baselineFile = $config->error_baseline;

if (empty($baselineFile)) {
die('Cannot update baseline, because no baseline file is configured.' . PHP_EOL);
fwrite(STDERR, 'Cannot update baseline, because no baseline file is configured.' . PHP_EOL);
exit(1);
}

try {
Expand Down Expand Up @@ -776,11 +783,13 @@ private static function autoGenerateConfig(
$vendor_dir,
);
} catch (ConfigCreationException $e) {
die($e->getMessage() . PHP_EOL);
fwrite(STDERR, $e->getMessage() . PHP_EOL);
exit(1);
}

if (!file_put_contents($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml', $template_contents)) {
die('Could not write to psalm.xml' . PHP_EOL);
if (file_put_contents($current_dir . DIRECTORY_SEPARATOR . 'psalm.xml', $template_contents) === false) {
fwrite(STDERR, 'Could not write to psalm.xml' . PHP_EOL);
exit(1);
}

exit('Config file created successfully. Please re-run psalm.' . PHP_EOL);
Expand Down
30 changes: 20 additions & 10 deletions src/Psalm/Internal/Cli/Psalter.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public static function run(array $argv): void
self::syncShortOptions($options);

if (isset($options['c']) && is_array($options['c'])) {
die('Too many config files provided' . PHP_EOL);
fwrite(STDERR, 'Too many config files provided' . PHP_EOL);
exit(1);
}

if (array_key_exists('h', $options)) {
Expand Down Expand Up @@ -200,7 +201,8 @@ public static function run(array $argv): void
$root_path = realpath($options['r']);

if ($root_path === false) {
die('Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL);
fwrite(STDERR, 'Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL);
exit(1);
}

$current_dir = $root_path;
Expand Down Expand Up @@ -304,7 +306,8 @@ public static function run(array $argv): void

if (array_key_exists('issues', $options)) {
if (!is_string($options['issues']) || !$options['issues']) {
die('Expecting a comma-separated list of issues' . PHP_EOL);
fwrite(STDERR, 'Expecting a comma-separated list of issues' . PHP_EOL);
exit(1);
}

$issues = explode(',', $options['issues']);
Expand Down Expand Up @@ -339,7 +342,8 @@ public static function run(array $argv): void
);

if ($allow_backwards_incompatible_changes === null) {
die('--allow-backwards-incompatible-changes expects a boolean value [true|false|1|0]' . PHP_EOL);
fwrite(STDERR, '--allow-backwards-incompatible-changes expects a boolean value [true|false|1|0]' . PHP_EOL);
exit(1);
}

$project_analyzer->getCodebase()->allow_backwards_incompatible_changes
Expand All @@ -354,7 +358,8 @@ public static function run(array $argv): void
);

if ($doc_block_add_new_line_before_return === null) {
die('--add-newline-between-docblock-annotations expects a boolean value [true|false|1|0]' . PHP_EOL);
fwrite(STDERR, '--add-newline-between-docblock-annotations expects a boolean value [true|false|1|0]' . PHP_EOL);
exit(1);
}

ParsedDocblock::addNewLineBetweenAnnotations($doc_block_add_new_line_before_return);
Expand Down Expand Up @@ -505,7 +510,8 @@ private static function loadCodeowners(Providers $providers): array
} elseif (file_exists('docs/CODEOWNERS')) {
$codeowners_file_path = realpath('docs/CODEOWNERS');
} else {
die('Cannot use --codeowner without a CODEOWNERS file' . PHP_EOL);
fwrite(STDERR, 'Cannot use --codeowner without a CODEOWNERS file' . PHP_EOL);
exit(1);
}

$codeowners_file = file_get_contents($codeowners_file_path);
Expand Down Expand Up @@ -555,7 +561,8 @@ static function (string $line): bool {
}

if (!$codeowner_files) {
die('Could not find any available entries in CODEOWNERS' . PHP_EOL);
fwrite(STDERR, 'Could not find any available entries in CODEOWNERS' . PHP_EOL);
exit(1);
}

return $codeowner_files;
Expand All @@ -571,11 +578,13 @@ private static function loadCodeownersFiles(array $desired_codeowners, array $co
/** @psalm-suppress MixedAssignment */
foreach ($desired_codeowners as $desired_codeowner) {
if (!is_string($desired_codeowner)) {
die('Invalid --codeowner ' . (string)$desired_codeowner . PHP_EOL);
fwrite(STDERR, 'Invalid --codeowner ' . (string) $desired_codeowner . PHP_EOL);
exit(1);
}

if ($desired_codeowner[0] !== '@') {
die('--codeowner option must start with @' . PHP_EOL);
fwrite(STDERR, '--codeowner option must start with @' . PHP_EOL);
exit(1);
}

$matched_file = false;
Expand All @@ -588,7 +597,8 @@ private static function loadCodeownersFiles(array $desired_codeowners, array $co
}

if (!$matched_file) {
die('User/group ' . $desired_codeowner . ' does not own any PHP files' . PHP_EOL);
fwrite(STDERR, 'User/group ' . $desired_codeowner . ' does not own any PHP files' . PHP_EOL);
exit(1);
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/Psalm/Internal/Cli/Refactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ static function (string $arg) use ($valid_long_options): void {
}

if (isset($options['c']) && is_array($options['c'])) {
die('Too many config files provided' . PHP_EOL);
fwrite(STDERR, 'Too many config files provided' . PHP_EOL);
exit(1);
}

if (array_key_exists('h', $options)) {
Expand Down Expand Up @@ -171,7 +172,8 @@ static function (string $arg) use ($valid_long_options): void {
$root_path = realpath($options['r']);

if ($root_path === false) {
die('Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL);
fwrite(STDERR, 'Could not locate root directory ' . $current_dir . DIRECTORY_SEPARATOR . $options['r'] . PHP_EOL);
exit(1);
}

$current_dir = $root_path;
Expand Down Expand Up @@ -210,7 +212,8 @@ static function (string $arg) use ($valid_long_options): void {

if ($arg === '--into') {
if ($operation !== 'move' || !$last_arg) {
die('--into is not expected here' . PHP_EOL);
fwrite(STDERR, '--into is not expected here' . PHP_EOL);
exit(1);
}

$operation = 'move_into';
Expand All @@ -224,7 +227,8 @@ static function (string $arg) use ($valid_long_options): void {

if ($arg === '--to') {
if ($operation !== 'rename' || !$last_arg) {
die('--to is not expected here' . PHP_EOL);
fwrite(STDERR, '--to is not expected here' . PHP_EOL);
exit(1);
}

$operation = 'rename_to';
Expand All @@ -239,7 +243,8 @@ static function (string $arg) use ($valid_long_options): void {

if ($operation === 'move_into' || $operation === 'rename_to') {
if (!$last_arg) {
die('Expecting a previous argument' . PHP_EOL);
fwrite(STDERR, 'Expecting a previous argument' . PHP_EOL);
exit(1);
}

if ($operation === 'move_into') {
Expand Down Expand Up @@ -273,11 +278,13 @@ static function (string $arg) use ($valid_long_options): void {
continue;
}

die('Unexpected argument "' . $arg . '"' . PHP_EOL);
fwrite(STDERR, 'Unexpected argument "' . $arg . '"' . PHP_EOL);
exit(1);
}

if (!$to_refactor) {
die('No --move or --rename arguments supplied' . PHP_EOL);
fwrite(STDERR, 'No --move or --rename arguments supplied' . PHP_EOL);
exit(1);
}

$config = CliUtils::initializeConfig(
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/CliUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ public static function initPhpVersion(array $options, Config $config, ProjectAna

if (isset($options['php-version'])) {
if (!is_string($options['php-version'])) {
die('Expecting a version number in the format x.y' . PHP_EOL);
fwrite(STDERR, 'Expecting a version number in the format x.y' . PHP_EOL);
exit(1);
}
$version = $options['php-version'];
$source = 'cli';
Expand Down

0 comments on commit a929aa5

Please sign in to comment.