Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  clear the terminal on an iteration basis
  • Loading branch information
Baptouuuu committed Apr 4, 2020
2 parents 752e28a + 3eef302 commit 2af7d05
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 42 deletions.
36 changes: 36 additions & 0 deletions src/Iteration.php
@@ -0,0 +1,36 @@
<?php
declare(strict_types = 1);

namespace Innmind\LabStation;

use Innmind\CLI\Environment;
use Innmind\Immutable\Str;

final class Iteration
{
private bool $shouldClearTerminal = true;

public function start(): void
{
$this->shouldClearTerminal = true;
}

public function failing(): void
{
$this->shouldClearTerminal = false;
}

public function end(Environment $env): void
{
if (!$this->shouldClearTerminal) {
return;
}

if ($env->arguments()->contains('--keep-output')) {
return;
}

// clear terminal
$env->output()->write(Str::of("\033[2J\033[H"));
}
}
5 changes: 5 additions & 0 deletions src/Monitor.php
Expand Up @@ -18,6 +18,7 @@ final class Monitor
private Manager $manager;
private IPC $ipc;
private Name $name;
private Iteration $iteration;
private Trigger $trigger;
/** @var list<Agent> */
private array $agents;
Expand All @@ -27,13 +28,15 @@ public function __construct(
Manager $manager,
IPC $ipc,
Name $name,
Iteration $iteration,
Trigger $trigger,
Agent ...$agents
) {
$this->protocol = $protocol;
$this->manager = $manager;
$this->ipc = $ipc;
$this->name = $name;
$this->iteration = $iteration;
$this->trigger = $trigger;
$this->agents = $agents;
}
Expand All @@ -57,7 +60,9 @@ public function __invoke(Environment $env): void

$this->ipc->listen($this->name)(function(Message $message) use ($env): void {
$activity = $this->protocol->decode($message);
$this->iteration->start();
($this->trigger)($activity, $env);
$this->iteration->end($env);
});

$agents->kill();
Expand Down
21 changes: 13 additions & 8 deletions src/Trigger/Psalm.php
Expand Up @@ -7,6 +7,7 @@
Trigger,
Activity,
Activity\Type,
Iteration,
};
use Innmind\CLI\Environment;
use Innmind\Server\Control\Server\{
Expand All @@ -22,11 +23,16 @@ final class Psalm implements Trigger
{
private Processes $processes;
private Filesystem $filesystem;
private Iteration $iteration;

public function __construct(Processes $processes, Filesystem $filesystem)
{
public function __construct(
Processes $processes,
Filesystem $filesystem,
Iteration $iteration
) {
$this->processes = $processes;
$this->filesystem = $filesystem;
$this->iteration = $iteration;
}

public function __invoke(Activity $activity, Environment $env): void
Expand Down Expand Up @@ -63,12 +69,16 @@ public function __invoke(Activity $activity, Environment $env): void
}
});
$process->wait();
$successful = $process->exitCode()->isSuccessful();

if (!$successful) {
$this->iteration->failing();
}

if ($env->arguments()->contains('--silent')) {
return;
}

$successful = $process->exitCode()->isSuccessful();
$text = 'Psalm : ';
$text .= $successful ? 'ok' : 'failing';

Expand All @@ -79,10 +89,5 @@ public function __invoke(Activity $activity, Environment $env): void
->withArgument($text)
)
->wait();

// clear terminal
if ($successful && !$env->arguments()->contains('--keep-output')) {
$output->write(Str::of("\033[2J\033[H"));
}
}
}
16 changes: 9 additions & 7 deletions src/Trigger/Tests.php
Expand Up @@ -7,6 +7,7 @@
Trigger,
Activity,
Activity\Type,
Iteration,
};
use Innmind\CLI\Environment;
use Innmind\Server\Control\Server\{
Expand All @@ -19,10 +20,12 @@
final class Tests implements Trigger
{
private Processes $processes;
private Iteration $iteration;

public function __construct(Processes $processes)
public function __construct(Processes $processes, Iteration $iteration)
{
$this->processes = $processes;
$this->iteration = $iteration;
}

public function __invoke(Activity $activity, Environment $env): void
Expand Down Expand Up @@ -55,12 +58,16 @@ public function __invoke(Activity $activity, Environment $env): void
}
});
$process->wait();
$successful = $process->exitCode()->isSuccessful();

if (!$successful) {
$this->iteration->failing();
}

if ($env->arguments()->contains('--silent')) {
return;
}

$successful = $process->exitCode()->isSuccessful();
$text = 'PHPUnit : ';
$text .= $successful ? 'ok' : 'failing';

Expand All @@ -71,10 +78,5 @@ public function __invoke(Activity $activity, Environment $env): void
->withArgument($text),
)
->wait();

// clear terminal
if ($successful && !$env->arguments()->contains('--keep-output')) {
$output->write(Str::of("\033[2J\033[H"));
}
}
}
6 changes: 4 additions & 2 deletions src/bootstrap.php
Expand Up @@ -27,6 +27,7 @@ function bootstrap(OperatingSystem $os): Commands
),
$ipc,
$monitor,
$iteration = new Iteration,
new Trigger\All(
new Trigger\Graphs(
$os->filesystem(),
Expand All @@ -42,10 +43,11 @@ function bootstrap(OperatingSystem $os): Commands
$os->filesystem(),
$os->control()->processes(),
),
new Trigger\Tests($os->control()->processes()),
new Trigger\Tests($os->control()->processes(), $iteration),
new Trigger\Psalm(
$os->control()->processes(),
$os->filesystem()
$os->filesystem(),
$iteration,
),
new Trigger\ComposerUpdate(
$os->control()->processes(),
Expand Down
4 changes: 4 additions & 0 deletions tests/Command/WorkTest.php
Expand Up @@ -8,6 +8,7 @@
Monitor,
Protocol,
Trigger,
Iteration,
};
use Innmind\CLI\{
Command,
Expand Down Expand Up @@ -35,6 +36,7 @@ public function testInterface()
$this->createMock(Manager::class),
$this->createMock(IPC::class),
new Name('foo'),
new Iteration,
$this->createMock(Trigger::class)
)
)
Expand All @@ -51,6 +53,7 @@ public function testUsage()
$this->createMock(Manager::class),
$this->createMock(IPC::class),
new Name('foo'),
new Iteration,
$this->createMock(Trigger::class)
)
))->toString()
Expand All @@ -65,6 +68,7 @@ public function testInvokation()
$this->createMock(Manager::class),
$ipc = $this->createMock(IPC::class),
new Name('foo'),
new Iteration,
$this->createMock(Trigger::class)
)
);
Expand Down
131 changes: 131 additions & 0 deletions tests/IterationTest.php
@@ -0,0 +1,131 @@
<?php
declare(strict_types = 1);

namespace Tests\Innmind\LabStation;

use Innmind\LabStation\Iteration;
use Innmind\CLI\Environment;
use Innmind\Stream\Writable;
use Innmind\Immutable\{
Sequence,
Str,
};
use PHPUnit\Framework\TestCase;

class IterationTest extends TestCase
{
public function testEndingAnIterationWithoutAStartWillClearTheTerminal()
{
$iteration = new Iteration;
$env = $this->createMock(Environment::class);
$env
->expects($this->any())
->method('arguments')
->willReturn(Sequence::strings());
$env
->expects($this->any())
->method('output')
->willReturn($output = $this->createMock(Writable::class));
$output
->expects($this->once())
->method('write')
->with(Str::of("\033[2J\033[H"));

$this->assertNull($iteration->end($env));
}

public function testNormalIterationWithoutAFailureWillClearTheTerminal()
{
$iteration = new Iteration;
$env = $this->createMock(Environment::class);
$env
->expects($this->any())
->method('arguments')
->willReturn(Sequence::strings());
$env
->expects($this->any())
->method('output')
->willReturn($output = $this->createMock(Writable::class));
$output
->expects($this->once())
->method('write')
->with(Str::of("\033[2J\033[H"));

$this->assertNull($iteration->start());
$this->assertNull($iteration->end($env));
}

public function testNormalIterationWithAFailureWillNotClearTheTerminal()
{
$iteration = new Iteration;
$env = $this->createMock(Environment::class);
$env
->expects($this->any())
->method('arguments')
->willReturn(Sequence::strings());
$env
->expects($this->never())
->method('output');

$this->assertNull($iteration->start());
$this->assertNull($iteration->failing());
$this->assertNull($iteration->end($env));
}

public function testNormalIterationWithoutAFailureWillNotClearTheTerminalWhenExplicitlyAskToKeepOutput()
{
$iteration = new Iteration;
$env = $this->createMock(Environment::class);
$env
->expects($this->any())
->method('arguments')
->willReturn(Sequence::strings('--keep-output'));
$env
->expects($this->never())
->method('output');

$this->assertNull($iteration->start());
$this->assertNull($iteration->end($env));
}

public function testNormalIterationWithAFailureWillNotClearTheTerminalWhenExplicitlyAskToKeepOutput()
{
$iteration = new Iteration;
$env = $this->createMock(Environment::class);
$env
->expects($this->any())
->method('arguments')
->willReturn(Sequence::strings('--keep-output'));
$env
->expects($this->never())
->method('output');

$this->assertNull($iteration->start());
$this->assertNull($iteration->failing());
$this->assertNull($iteration->end($env));
}

public function testClearTheTerminalEvenWhenPreviousIterationFailed()
{
$iteration = new Iteration;
$env = $this->createMock(Environment::class);
$env
->expects($this->any())
->method('arguments')
->willReturn(Sequence::strings());
$env
->expects($this->any())
->method('output')
->willReturn($output = $this->createMock(Writable::class));
$output
->expects($this->once())
->method('write')
->with(Str::of("\033[2J\033[H"));

$iteration->start();
$iteration->failing();
$iteration->end($env);
$this->assertNull($iteration->start());
$this->assertNull($iteration->end($env));
}
}
7 changes: 7 additions & 0 deletions tests/MonitorTest.php
Expand Up @@ -10,6 +10,7 @@
Agent,
Activity,
Activity\Type,
Iteration,
};
use Innmind\CLI\Environment;
use Innmind\ProcessManager\Manager;
Expand All @@ -20,6 +21,7 @@
Message,
};
use Innmind\Url\Path;
use Innmind\Immutable\Sequence;
use PHPUnit\Framework\TestCase;

class MonitorTest extends TestCase
Expand All @@ -31,12 +33,17 @@ public function testInvokation()
$manager = $this->createMock(Manager::class),
$ipc = $this->createMock(IPC::class),
$name = new Name('monitor'),
new Iteration,
$trigger = $this->createMock(Trigger::class),
$agent1 = $this->createMock(Agent::class),
$agent2 = $this->createMock(Agent::class)
);
$message = $this->createMock(Message::class);
$env = $this->createMock(Environment::class);
$env
->expects($this->any())
->method('arguments')
->willReturn(Sequence::strings());
$env
->expects($this->once())
->method('workingDirectory')
Expand Down

0 comments on commit 2af7d05

Please sign in to comment.