Skip to content

Commit

Permalink
Allow specifying a relative file via phar
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Mar 8, 2024
1 parent 1a61e0d commit a8363bc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
use function assert;
use function count;
use function dirname;
use function file_exists;
use function gettype;
use function in_array;
use function is_string;
Expand All @@ -62,7 +61,6 @@ protected function configure(): void
null,
InputOption::VALUE_REQUIRED,
'the config file to configure the checking options',
self::DEFAULT_CONFIG_PATH,
)
->addArgument(
'composer-json',
Expand Down Expand Up @@ -234,23 +232,23 @@ static function (string $unknownSymbol) use ($guesser): array {
*/
private function getCheckOptions(InputInterface $input): Options
{
$fileName = $input->getOption('config-file');

if (is_string($fileName) === false) {
return new Options();
}

if (file_exists($fileName) === false) {
if ($fileName === self::DEFAULT_CONFIG_PATH) {
$configFileArgument = $input->getOption('config-file');

Check failure on line 235 in src/ComposerRequireChecker/Cli/CheckCommand.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.2, ubuntu-latest)

MixedAssignment

src/ComposerRequireChecker/Cli/CheckCommand.php:235:9: MixedAssignment: Unable to determine the type that $configFileArgument is being assigned to (see https://psalm.dev/032)

if (is_string($configFileArgument)) {
$fileName = realpath($configFileArgument);
if ($fileName === false) {
throw new InvalidArgumentException(
sprintf(
'Configuration file [%s] does not exist.',
$configFileArgument,
),
);
}
} else {
$fileName = realpath(self::DEFAULT_CONFIG_PATH);
if ($fileName === false) {
return new Options();
}

throw new InvalidArgumentException(
sprintf(
'Configuration file [%s] does not exist.',
$fileName,
),
);
}

$config = JsonLoader::getData($fileName);
Expand Down
42 changes: 42 additions & 0 deletions test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public function testNoUnknownComposerSymbolFound(): void
);
}

/** This is the same as the next test, but with the default configuration file assumed */
public function testDefaultConfigPath(): void
{
$baseDirectory = dirname(__DIR__, 2) . '/fixtures/defaultConfigPath/';
Expand All @@ -302,6 +303,33 @@ public function testDefaultConfigPath(): void
);
}

/** This is the same as the previous test, but with the configuration file specified */
public function testDefaultConfigPathSpecified(): void
{
$baseDirectory = dirname(__DIR__, 2) . '/fixtures/defaultConfigPath/';

chdir($baseDirectory);
$exitCode = $this->commandTester->execute([
'composer-json' => 'composer.json',
'--config-file' => 'composer-require-checker.json',
]);
$output = $this->commandTester->getDisplay();

$this->assertNotEquals(0, $exitCode);
$this->assertMatchesRegularExpression(
'/The following 2 unknown symbols were found/s',
$output,
);
$this->assertMatchesRegularExpression(
'/Composer\\\\InstalledVersions/s',
$output,
);
$this->assertMatchesRegularExpression(
'/json_decode/s',
$output,
);
}

public function testOverrideDefaultConfigPath(): void
{
$baseDirectory = dirname(__DIR__, 2) . '/fixtures/defaultConfigPath/';
Expand Down Expand Up @@ -349,6 +377,20 @@ public function testNotExistentConfigPath(): void
]);
}

public function testNotExistentDefaultConfigPath(): void
{
$baseDirectory = dirname(__DIR__, 2) . '/fixtures/noUnknownSymbols/';

chdir($baseDirectory);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Configuration file [composer-require-checker.json] does not exist.');
$this->commandTester->execute([
'composer-json' => 'composer.json',
'--config-file' => 'composer-require-checker.json',
]);
}

/** @requires PHP >= 8.1.0 */
public function testNoUnknownEnumSymbolsFound(): void
{
Expand Down
9 changes: 9 additions & 0 deletions test/ComposerRequireCheckerTest/PharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ public function testMissingCustomConfiguration(): void
$this->assertStringContainsString('Configuration file [no-such-file] does not exist.', implode("\n", $output));
$this->assertNotEquals(0, $return);
}

public function testMissingDefaultConfiguration(): void
{
chdir(__DIR__ . '/../fixtures/noUnknownComposerSymbol');
exec(sprintf('%s check --config-file=%s 2>&1', $this->bin, 'composer-require-checker.json'), $output, $return);

$this->assertStringContainsString('Configuration file [composer-require-checker.json] does not exist.', implode("\n", $output));
$this->assertNotEquals(0, $return);
}
}

0 comments on commit a8363bc

Please sign in to comment.