Skip to content

Commit 16689be

Browse files
Return proper exit code on failure
1 parent 4086f6e commit 16689be

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

libyear

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ const vendor_indicator = 'vendor' . DIRECTORY_SEPARATOR . 'ecoapm' . DIRECTORY_S
88
$is_vendored = strpos(__DIR__, vendor_indicator) === strlen(__DIR__) - strlen(vendor_indicator);
99
require_once __DIR__ . ($is_vendored ? '/../../..' : '') . '/vendor/autoload.php';
1010

11-
Factory::app()->run($argv);
11+
$result = Factory::app()->run($argv);
12+
exit($result ? 0 : 1);

src/App.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use cli\Table;
66
use Garden\Cli\Cli;
77
use Exception;
8-
use InvalidArgumentException;
98

109
class App
1110
{
@@ -37,17 +36,18 @@ public function __construct(Cli $cli, Calculator $calculator, ComposerFile $comp
3736
/**
3837
* @param string[] $args
3938
*/
40-
public function run(array $args): void
39+
public function run(array $args): bool
4140
{
4241
try {
4342
$arguments = $this->cli->parse($args, false);
4443
} catch (Exception $e) {
45-
$error = $e->getMessage();
46-
fwrite($this->output, "{$error}\n");
47-
if (!str_starts_with($error, "usage: ")) {
44+
$msg = $e->getMessage();
45+
fwrite($this->output, "{$msg}\n");
46+
if (!str_starts_with($msg, "usage: ")) {
4847
$this->showHelp();
48+
return false;
4949
}
50-
return;
50+
return true;
5151
}
5252

5353
$quiet_mode = $arguments->getOpt('quiet') !== null;
@@ -73,6 +73,8 @@ public function run(array $args): void
7373
fwrite($this->output, "composer.json updated\n");
7474
fwrite($this->output, "A manual run of \"composer update\" is required to actually update dependencies\n");
7575
}
76+
77+
return true;
7678
}
7779

7880
/**

tests/AppTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,32 @@ public function testCanDisplayHelpText()
4242
$app = new App(new Cli(), self::calculator(), $composer, $output);
4343

4444
//act
45-
$app->run(['libyear', '--help']);
45+
$result = $app->run(['libyear', '--help']);
4646

4747
//assert
4848
fseek($output, 0);
4949
$console = stream_get_contents($output);
5050
$this->assertStringContainsString('OPTIONS', $console);
5151
$this->assertStringContainsString('ARGUMENTS', $console);
52+
$this->assertTrue($result);
53+
}
54+
55+
public function testInvalidOptionDisplaysHelpText()
56+
{
57+
//arrange
58+
$composer = Mockery::mock(ComposerFile::class);
59+
$output = fopen('php://memory', 'a+');
60+
$app = new App(new Cli(), self::calculator(), $composer, $output);
61+
62+
//act
63+
$result = $app->run(['libyear', '-x']);
64+
65+
//assert
66+
fseek($output, 0);
67+
$console = stream_get_contents($output);
68+
$this->assertStringContainsString('OPTIONS', $console);
69+
$this->assertStringContainsString('ARGUMENTS', $console);
70+
$this->assertFalse($result);
5271
}
5372

5473
public function testShowsAllDependenciesByDefault()
@@ -59,13 +78,14 @@ public function testShowsAllDependenciesByDefault()
5978
$app = new App(new Cli(), self::calculator(), $composer, $output);
6079

6180
//act
62-
$app->run(['libyear', '.']);
81+
$result = $app->run(['libyear', '.']);
6382

6483
//assert
6584
fseek($output, 0);
6685
$console = stream_get_contents($output);
6786
$this->assertStringContainsString('Test 1', $console);
6887
$this->assertStringContainsString('Test 2', $console);
88+
$this->assertTrue($result);
6989
}
7090

7191
public function testQuietModeOnlyShowsOutdated()

0 commit comments

Comments
 (0)