Skip to content

Commit 735ad1f

Browse files
Add ability to set exit code based on libyear limits
1 parent 16689be commit 735ad1f

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

src/App.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class App
2424
public function __construct(Cli $cli, Calculator $calculator, ComposerFile $composer, $output)
2525
{
2626
$this->cli = $cli->description('libyear: a simple measure of dependency freshness -- calculates the total number of years behind their respective newest versions for all dependencies listed in a composer.json file.')
27-
->opt('quiet:q', 'only display outdated dependencies', false, 'boolean')
28-
->opt('update:u', 'update composer.json with newest versions', false, 'boolean')
29-
->opt('verbose:v', 'display network debug information', false, 'boolean')
27+
->opt('quiet:q', 'only display outdated dependencies')
28+
->opt('update:u', 'update composer.json with newest versions')
29+
->opt('verbose:v', 'display network debug information')
30+
->opt('limit:l', 'fails if total libyears behind is greater than this value')
31+
->opt('limit-any:a', 'fails if any dependency is more libyears behind than this value')
3032
->arg('path', 'the directory containing composer.json and composer.lock files');
3133
$this->calculator = $calculator;
3234
$this->composer = $composer;
@@ -53,6 +55,8 @@ public function run(array $args): bool
5355
$quiet_mode = $arguments->getOpt('quiet') !== null;
5456
$update_mode = $arguments->getOpt('update') !== null;
5557
$verbose_mode = $arguments->getOpt('verbose') !== null;
58+
$limit_total = $arguments->getOpt('limit');
59+
$limit_any = $arguments->getOpt('limit-any');
5660
$dir = $arguments->getArg('path') ?? '.';
5761

5862
$real_dir = realpath($dir);
@@ -68,6 +72,23 @@ public function run(array $args): bool
6872

6973
fwrite($this->output, "Total: $total_display libyears behind\n");
7074

75+
if ($limit_any != null) {
76+
$beyond_limit = array_filter($dependencies, fn($d) => $d->getLibyearsBehind() > $limit_any);
77+
78+
/** @var Dependency $dependency */
79+
foreach ($beyond_limit as $dependency) {
80+
$behind = number_format($dependency->getLibyearsBehind(), 2);
81+
fwrite($this->output, "{$dependency->name} is {$behind} libyears behind, which is greater than the set limit of {$limit_any}\n");
82+
}
83+
84+
return sizeof($beyond_limit) == 0;
85+
}
86+
87+
if ($limit_total != null && $total > $limit_total) {
88+
fwrite($this->output, "Total libyears behind is greater than the set limit of {$limit_total}\n");
89+
return false;
90+
}
91+
7192
if ($update_mode) {
7293
$this->composer->update($dir, $dependencies);
7394
fwrite($this->output, "composer.json updated\n");

tests/AppTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,62 @@ public function testQuietModeOnlyShowsOutdated()
105105
$this->assertStringNotContainsString('Test 1', $console);
106106
}
107107

108+
public function testFailsIfTotalLimitIsExceeded()
109+
{
110+
//arrange
111+
$composer = Mockery::mock(ComposerFile::class);
112+
$output = fopen('php://memory', 'a+');
113+
$app = new App(new Cli(), self::calculator(), $composer, $output);
114+
115+
//act
116+
$result = $app->run(['libyear', '-l', '0']);
117+
118+
//assert
119+
$this->assertFalse($result);
120+
}
121+
122+
public function testPassesIfTotalLimitIsNotExceeded()
123+
{
124+
//arrange
125+
$composer = Mockery::mock(ComposerFile::class);
126+
$output = fopen('php://memory', 'a+');
127+
$app = new App(new Cli(), self::calculator(), $composer, $output);
128+
129+
//act
130+
$result = $app->run(['libyear', '-l', '2']);
131+
132+
//assert
133+
$this->assertTrue($result);
134+
}
135+
136+
public function testFailsIfAnyLimitIsExceeded()
137+
{
138+
//arrange
139+
$composer = Mockery::mock(ComposerFile::class);
140+
$output = fopen('php://memory', 'a+');
141+
$app = new App(new Cli(), self::calculator(), $composer, $output);
142+
143+
//act
144+
$result = $app->run(['libyear', '-a', '0']);
145+
146+
//assert
147+
$this->assertFalse($result);
148+
}
149+
150+
public function testPassesIfAnyLimitIsNotExceeded()
151+
{
152+
//arrange
153+
$composer = Mockery::mock(ComposerFile::class);
154+
$output = fopen('php://memory', 'a+');
155+
$app = new App(new Cli(), self::calculator(), $composer, $output);
156+
157+
//act
158+
$result = $app->run(['libyear', '-a', '2']);
159+
160+
//assert
161+
$this->assertTrue($result);
162+
}
163+
108164
public function testUpdatesComposerIfFlagSet()
109165
{
110166
//arrange

0 commit comments

Comments
 (0)