Skip to content

Commit

Permalink
LastDragon_ru\LaraASP\Documentator\Utils\Process helper replaced by…
Browse files Browse the repository at this point in the history
… `Illuminate\Support\Facades\Process`.
  • Loading branch information
LastDragon-ru committed Jan 2, 2024
1 parent 6e29397 commit 2f11080
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions;

use Exception;
use Illuminate\Support\Facades\Process;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Contracts\ProcessableInstruction;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetExecFailed;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetIsNotFile;
use LastDragon_ru\LaraASP\Documentator\Utils\Path;
use LastDragon_ru\LaraASP\Documentator\Utils\Process;
use Override;

use function dirname;
Expand All @@ -26,9 +26,7 @@ class IncludeExample implements ProcessableInstruction {
public const Limit = 50;
protected const MarkdownRegexp = '/^\<(?P<tag>markdown)\>(?P<markdown>.*?)\<\/(?P=tag)\>$/msu';

public function __construct(
protected readonly Process $process,
) {
public function __construct() {
// empty
}

Expand Down Expand Up @@ -78,7 +76,7 @@ public function process(string $path, string $target): string {
if ($command) {
// Call
try {
$output = $this->process->run($command, dirname($path));
$output = Process::path(dirname($path))->run($command)->throw()->output();
$output = trim($output);
} catch (Exception $exception) {
throw new TargetExecFailed($path, $target, $exception);
Expand Down Expand Up @@ -144,16 +142,13 @@ protected function getLanguage(string $path, string $target): string {
return pathinfo($target, PATHINFO_EXTENSION);
}

/**
* @return list<string>
*/
protected function getCommand(string $path, string $target): ?array {
protected function getCommand(string $path, string $target): ?string {
$info = pathinfo($target);
$file = isset($info['dirname'])
? Path::join($info['dirname'], "{$info['filename']}.run")
: "{$info['filename']}.run";
$command = Path::getPath(dirname($path), $file);
$command = is_file($command) ? [$command] : null;
$command = is_file($command) ? $command : null;

return $command;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions;

use Illuminate\Container\Container;
use Illuminate\Process\PendingProcess;
use Illuminate\Support\Facades\Process;
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase;
use LastDragon_ru\LaraASP\Documentator\Utils\Process;
use Mockery;
use PHPUnit\Framework\Attributes\CoversClass;

use function basename;
Expand All @@ -23,14 +23,10 @@ public function testProcessNoRun(): void {
$path = self::getTestData()->path('~example.md');
$file = basename(self::getTestData()->path('~example.md'));
$expected = trim(self::getTestData()->content('~example.md'));
$process = Mockery::mock(Process::class);
$process
->shouldReceive('run')
->never();
$instance = Container::getInstance()->make(IncludeExample::class);

$instance = Container::getInstance()->make(IncludeExample::class, [
'process' => $process,
]);
Process::preventStrayProcesses();
Process::fake();

self::assertEquals(
<<<EXPECTED
Expand All @@ -40,22 +36,21 @@ public function testProcessNoRun(): void {
EXPECTED,
$instance->process($path, $file),
);

Process::assertNothingRan();
}

public function testProcess(): void {
$path = self::getTestData()->path('~runnable.md');
$file = basename(self::getTestData()->path('~runnable.md'));
$command = self::getTestData()->path('~runnable.run');
$expected = trim(self::getTestData()->content('~runnable.md'));
$output = 'command output';
$process = Mockery::mock(Process::class);
$process
->shouldReceive('run')
->with([self::getTestData()->path('~runnable.run')], dirname($path))
->once()
->andReturn($output);

$instance = Container::getInstance()->make(IncludeExample::class, [
'process' => $process,
$instance = Container::getInstance()->make(IncludeExample::class);

Process::preventStrayProcesses();
Process::fake([
$command => Process::result($output),
]);

self::assertEquals(
Expand All @@ -72,22 +67,24 @@ public function testProcess(): void {
EXPECTED,
$instance->process($path, $file),
);

Process::assertRan(static function (PendingProcess $process) use ($path, $command): bool {
return $process->path === dirname($path)
&& $process->command === $command;
});
}

public function testProcessLongOutput(): void {
$path = self::getTestData()->path('~runnable.md');
$file = self::getTestData()->path('~runnable.md');
$command = self::getTestData()->path('~runnable.run');
$expected = trim(self::getTestData()->content('~runnable.md'));
$output = implode("\n", range(0, IncludeExample::Limit + 1));
$process = Mockery::mock(Process::class);
$process
->shouldReceive('run')
->with([self::getTestData()->path('~runnable.run')], dirname($path))
->once()
->andReturn($output);

$instance = Container::getInstance()->make(IncludeExample::class, [
'process' => $process,
$instance = Container::getInstance()->make(IncludeExample::class);

Process::preventStrayProcesses();
Process::fake([
$command => Process::result($output),
]);

self::assertEquals(
Expand All @@ -106,22 +103,24 @@ public function testProcessLongOutput(): void {
EXPECTED,
$instance->process($path, $file),
);

Process::assertRan(static function (PendingProcess $process) use ($path, $command): bool {
return $process->path === dirname($path)
&& $process->command === $command;
});
}

public function testProcessMarkdown(): void {
$path = self::getTestData()->path('~runnable.md');
$file = basename(self::getTestData()->path('~runnable.md'));
$command = self::getTestData()->path('~runnable.run');
$expected = trim(self::getTestData()->content('~runnable.md'));
$output = 'command output';
$process = Mockery::mock(Process::class);
$process
->shouldReceive('run')
->with([self::getTestData()->path('~runnable.run')], dirname($path))
->once()
->andReturn("<markdown>{$output}</markdown>");

$instance = Container::getInstance()->make(IncludeExample::class, [
'process' => $process,
$instance = Container::getInstance()->make(IncludeExample::class);

Process::preventStrayProcesses();
Process::fake([
$command => Process::result("<markdown>{$output}</markdown>"),
]);

self::assertEquals(
Expand All @@ -134,22 +133,24 @@ public function testProcessMarkdown(): void {
EXPECTED,
$instance->process($path, $file),
);

Process::assertRan(static function (PendingProcess $process) use ($path, $command): bool {
return $process->path === dirname($path)
&& $process->command === $command;
});
}

public function testProcessMarkdownLongOutput(): void {
$path = self::getTestData()->path('~runnable.md');
$file = self::getTestData()->path('~runnable.md');
$command = self::getTestData()->path('~runnable.run');
$expected = trim(self::getTestData()->content('~runnable.md'));
$output = implode("\n", range(0, IncludeExample::Limit + 1));
$process = Mockery::mock(Process::class);
$process
->shouldReceive('run')
->with([self::getTestData()->path('~runnable.run')], dirname($path))
->once()
->andReturn("<markdown>{$output}</markdown>");

$instance = Container::getInstance()->make(IncludeExample::class, [
'process' => $process,
$instance = Container::getInstance()->make(IncludeExample::class);

Process::preventStrayProcesses();
Process::fake([
$command => Process::result("<markdown>{$output}</markdown>"),
]);

self::assertEquals(
Expand All @@ -166,5 +167,10 @@ public function testProcessMarkdownLongOutput(): void {
EXPECTED,
$instance->process($path, $file),
);

Process::assertRan(static function (PendingProcess $process) use ($path, $command): bool {
return $process->path === dirname($path)
&& $process->command === $command;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions;

use Exception;
use Illuminate\Support\Facades\Process;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Contracts\ProcessableInstruction;
use LastDragon_ru\LaraASP\Documentator\Preprocessor\Exceptions\TargetExecFailed;
use LastDragon_ru\LaraASP\Documentator\Utils\Process;
use Override;

use function dirname;
use function trim;

class IncludeExec implements ProcessableInstruction {
public function __construct(
protected readonly Process $process,
) {
public function __construct() {
// empty
}

Expand All @@ -35,7 +34,7 @@ public static function getTargetDescription(): ?string {
#[Override]
public function process(string $path, string $target): string {
try {
return $this->process->run($target, dirname($path));
return trim(Process::path(dirname($path))->run($target)->throw()->output());
} catch (Exception $exception) {
throw new TargetExecFailed($path, $target, $exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace LastDragon_ru\LaraASP\Documentator\Preprocessor\Instructions;

use Illuminate\Container\Container;
use Illuminate\Process\PendingProcess;
use Illuminate\Support\Facades\Process;
use LastDragon_ru\LaraASP\Documentator\Testing\Package\TestCase;
use LastDragon_ru\LaraASP\Documentator\Utils\Process;
use Mockery;
use PHPUnit\Framework\Attributes\CoversClass;

use function dirname;
Expand All @@ -19,17 +19,18 @@ public function testProcess(): void {
$path = 'current/working/directory/file.md';
$expected = 'result';
$command = 'command to execute';
$process = Mockery::mock(Process::class);
$process
->shouldReceive('run')
->with('command to execute', dirname($path))
->once()
->andReturn($expected);
$instance = Container::getInstance()->make(IncludeExec::class);

$instance = Container::getInstance()->make(IncludeExec::class, [
'process' => $process,
Process::preventStrayProcesses();
Process::fake([
$command => Process::result($expected),
]);

self::assertEquals($expected, $instance->process($path, $command));

Process::assertRan(static function (PendingProcess $process) use ($path, $command): bool {
return $process->path === dirname($path)
&& $process->command === $command;
});
}
}
26 changes: 19 additions & 7 deletions packages/documentator/src/Utils/Git.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace LastDragon_ru\LaraASP\Documentator\Utils;

use Illuminate\Support\Facades\Process;

use function array_filter;
use function array_values;
use function explode;
use function trim;

class Git {
public function __construct(
protected readonly Process $process,
) {
public function __construct() {
// empty
}

Expand All @@ -19,7 +20,7 @@ public function __construct(
* @return list<string>
*/
public function getTags(callable $filter = null, string $root = null): array {
$tags = $this->process->run(['git', 'tag', '--list'], $root);
$tags = $this->run(['git', 'tag', '--list'], $root);
$tags = explode("\n", $tags);
$tags = $filter ? array_filter($tags, $filter) : $tags;
$tags = array_values($tags);
Expand All @@ -28,14 +29,25 @@ public function getTags(callable $filter = null, string $root = null): array {
}

public function getFile(string $path, string $revision = 'HEAD', string $root = null): string {
return $this->process->run(['git', 'show', "{$revision}:{$path}"], $root);
return $this->run(['git', 'show', "{$revision}:{$path}"], $root);
}

public function getBranch(string $root = null): string {
return $this->process->run(['git', 'rev-parse', '--abbrev-ref=HEAD'], $root);
return $this->run(['git', 'rev-parse', '--abbrev-ref=HEAD'], $root);
}

public function getRoot(string $root = null): string {
return $this->process->run(['git', 'rev-parse', '--show-toplevel'], $root);
return $this->run(['git', 'rev-parse', '--show-toplevel'], $root);
}

/**
* @param array<array-key, string>|string $command
*/
private function run(array|string $command, string $root = null): string {
$process = $root !== null ? Process::path($root) : Process::command('');
$output = $process->run($command)->throw()->output();
$output = trim($output);

return $output;
}
}
32 changes: 0 additions & 32 deletions packages/documentator/src/Utils/Process.php

This file was deleted.

0 comments on commit 2f11080

Please sign in to comment.