Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $cwd argument to Process creation chain #325

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/API/Process/Service/ComposerProcessRunnerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpTuf\ComposerStager\API\Process\Service;

use PhpTuf\ComposerStager\API\Path\Value\PathInterface;

/**
* Runs Composer commands.
*
Expand All @@ -24,6 +26,9 @@ interface ComposerProcessRunnerInterface
* '--with-all-dependencies',
* ];
* ```
* @param \PhpTuf\ComposerStager\API\Path\Value\PathInterface|null $cwd
* The current working directory (CWD) for the process. If set to null,
* the CWD of the current PHP process will be used.
* @param array<string|\Stringable> $env
* An array of environment variables, keyed by variable name with corresponding
* string or stringable values. In addition to those explicitly specified,
Expand Down Expand Up @@ -51,6 +56,7 @@ interface ComposerProcessRunnerInterface
*/
public function run(
array $command,
?PathInterface $cwd = null,
array $env = [],
?OutputCallbackInterface $callback = null,
int $timeout = ProcessInterface::DEFAULT_TIMEOUT,
Expand Down
6 changes: 6 additions & 0 deletions src/API/Process/Service/RsyncProcessRunnerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpTuf\ComposerStager\API\Process\Service;

use PhpTuf\ComposerStager\API\Path\Value\PathInterface;

/**
* Runs rsync commands.
*
Expand All @@ -24,6 +26,9 @@ interface RsyncProcessRunnerInterface
* 'path/to/destination',
* ];
* ```
* @param \PhpTuf\ComposerStager\API\Path\Value\PathInterface|null $cwd
* The current working directory (CWD) for the process. If set to null,
* the CWD of the current PHP process will be used.
* @param array<string|\Stringable> $env
* An array of environment variables, keyed by variable name with corresponding
* string or stringable values. In addition to those explicitly specified,
Expand Down Expand Up @@ -51,6 +56,7 @@ interface RsyncProcessRunnerInterface
*/
public function run(
array $command,
?PathInterface $cwd = null,
array $env = [],
?OutputCallbackInterface $callback = null,
int $timeout = ProcessInterface::DEFAULT_TIMEOUT,
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Core/Stager.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private function runCommand(
$command = ['--working-dir=' . $stagingDir->absolute(), ...$composerCommand];

try {
$this->composerRunner->run($command, [], $callback, $timeout);
$this->composerRunner->run($command, null, [], $callback, $timeout);
} catch (ExceptionInterface $e) {
throw new RuntimeException($e->getTranslatableMessage(), 0, $e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/FileSyncer/Service/RsyncFileSyncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function runCommand(
$command = $this->buildCommand($exclusions, $sourceAbsolute, $destinationAbsolute);

try {
$this->rsync->run($command, [], $callback);
$this->rsync->run($command, null, [], $callback);
} catch (ExceptionInterface $e) {
throw new IOException($e->getTranslatableMessage(), 0, $e);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Internal/Process/Factory/SymfonyProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpTuf\ComposerStager\Internal\Process\Factory;

use PhpTuf\ComposerStager\API\Exception\LogicException;
use PhpTuf\ComposerStager\API\Path\Value\PathInterface;
use PhpTuf\ComposerStager\API\Translation\Factory\TranslatableFactoryInterface;
use PhpTuf\ComposerStager\Internal\Translation\Factory\TranslatableAwareTrait;
use Symfony\Component\Process\Exception\ExceptionInterface as SymfonyExceptionInterface;
Expand All @@ -22,10 +23,15 @@ public function __construct(TranslatableFactoryInterface $translatableFactory)
$this->setTranslatableFactory($translatableFactory);
}

public function create(array $command, array $env = []): SymfonyProcess
public function create(array $command, ?PathInterface $cwd = null, array $env = []): SymfonyProcess
{
if ($cwd instanceof PathInterface) {
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$cwd = $cwd->absolute();
}

try {
return new SymfonyProcess($command, null, $env);
return new SymfonyProcess($command, $cwd, $env);
} catch (SymfonyExceptionInterface $e) {
throw new LogicException($this->t(
'Failed to create process: %details',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpTuf\ComposerStager\Internal\Process\Factory;

use PhpTuf\ComposerStager\API\Path\Value\PathInterface;
use Symfony\Component\Process\Process as SymfonyProcess;

/**
Expand All @@ -26,6 +27,9 @@ interface SymfonyProcessFactoryInterface
* '--with-all-dependencies',
* ];
* ```
* @param \PhpTuf\ComposerStager\API\Path\Value\PathInterface|null $cwd
* The current working directory (CWD) for the process. If set to null,
* the CWD of the current PHP process will be used.
* @param array<string|\Stringable> $env
* An array of environment variables, keyed by variable name with corresponding
* string or stringable values. In addition to those explicitly specified,
Expand All @@ -44,5 +48,5 @@ interface SymfonyProcessFactoryInterface
*
* @see \Symfony\Component\Process\Process::__construct
*/
public function create(array $command, array $env = []): SymfonyProcess;
public function create(array $command, ?PathInterface $cwd = null, array $env = []): SymfonyProcess;
}
5 changes: 5 additions & 0 deletions src/Internal/Process/Service/AbstractProcessRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpTuf\ComposerStager\Internal\Process\Service;

use PhpTuf\ComposerStager\API\Finder\Service\ExecutableFinderInterface;
use PhpTuf\ComposerStager\API\Path\Value\PathInterface;
use PhpTuf\ComposerStager\API\Process\Factory\ProcessFactoryInterface;
use PhpTuf\ComposerStager\API\Process\Service\OutputCallbackInterface;
use PhpTuf\ComposerStager\API\Process\Service\ProcessInterface;
Expand Down Expand Up @@ -37,6 +38,9 @@ public function __construct(
* The command arguments as separate string values, e.g.,
* ['require', 'example/package'] or ['source', 'destination']. The return
* value of ::executableName() will be automatically prepended.
* @param \PhpTuf\ComposerStager\API\Path\Value\PathInterface|null $cwd
* The current working directory (CWD) for the process. If set to null,
* the CWD of the current PHP process will be used.
* @param \PhpTuf\ComposerStager\API\Process\Service\OutputCallbackInterface|null $callback
* An optional PHP callback to run whenever there is process output.
* @param array<string|\Stringable> $env
Expand Down Expand Up @@ -66,6 +70,7 @@ public function __construct(
*/
public function run(
array $command,
?PathInterface $cwd = null,
array $env = [],
?OutputCallbackInterface $callback = null,
int $timeout = ProcessInterface::DEFAULT_TIMEOUT,
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Process/Service/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function __construct(
array $env = [],
) {
$this->setTranslatableFactory($translatableFactory);
$this->symfonyProcess = $this->symfonyProcessFactory->create($command, $env);
$this->symfonyProcess = $this->symfonyProcessFactory->create($command, null, $env);
}

public function getEnv(): array
Expand Down
4 changes: 2 additions & 2 deletions tests/Core/StagerUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testStageWithMinimumParams(): void
self::INERT_COMMAND,
];
$this->composerRunner
->run($expectedCommand, [], null, $timeout)
->run($expectedCommand, null, [], null, $timeout)
->shouldBeCalledOnce();
$sut = $this->createSut();

Expand All @@ -86,7 +86,7 @@ public function testStageWithOptionalParams(
->assertIsFulfilled($activeDirPath, $stagingDirPath)
->shouldBeCalledOnce();
$this->composerRunner
->run($expectedCommand, [], $callback, $timeout)
->run($expectedCommand, null, [], $callback, $timeout)
->shouldBeCalledOnce();
$sut = $this->createSut();

Expand Down
93 changes: 55 additions & 38 deletions tests/FileSyncer/Service/RsyncFileSyncerUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpTuf\ComposerStager\API\Exception\LogicException;
use PhpTuf\ComposerStager\API\Exception\RuntimeException;
use PhpTuf\ComposerStager\API\Filesystem\Service\FilesystemInterface;
use PhpTuf\ComposerStager\API\Path\Value\PathListInterface;
use PhpTuf\ComposerStager\API\Process\Service\OutputCallbackInterface;
use PhpTuf\ComposerStager\API\Process\Service\RsyncProcessRunnerInterface;
use PhpTuf\ComposerStager\Internal\FileSyncer\Service\RsyncFileSyncer;
Expand Down Expand Up @@ -76,9 +75,9 @@ private function createSut(): RsyncFileSyncer
public function testSync(
string $source,
string $destination,
?PathListInterface $exclusions,
array $command,
?OutputCallbackInterface $callback,
array $optionalArguments,
array $expectedCommand,
?OutputCallbackInterface $expectedCallback,
): void {
$sourcePath = PathHelper::createPath($source);
$destinationPath = PathHelper::createPath($destination);
Expand All @@ -87,34 +86,48 @@ public function testSync(
->mkdir($destinationPath)
->shouldBeCalledOnce();
$this->rsync
->run($command, [], $callback)
->run($expectedCommand, null, [], $expectedCallback)
->shouldBeCalledOnce();
$sut = $this->createSut();

$sut->sync($sourcePath, $destinationPath, $exclusions, $callback);
$sut->sync($sourcePath, $destinationPath, ...$optionalArguments);
}

public function providerSync(): array
{
return [
'Minimum arguments' => [
'source' => '/var/www/source',
'destination' => '/var/www/destination',
'optionalArguments' => [],
'expectedCommand' => [
'--archive',
'--delete-after',
'--verbose',
'/var/www/source/',
'/var/www/destination',
],
'expectedCallback' => null,
],
'Siblings: no exclusions given' => [
'source' => '/var/www/source/one',
'destination' => '/var/www/destination/two',
'exclusions' => null,
'command' => [
'optionalArguments' => [],
'expectedCommand' => [
'--archive',
'--delete-after',
'--verbose',
'/var/www/source/one/',
'/var/www/destination/two',
],
'callback' => null,
'expectedCallback' => null,
],
//'Siblings: simple exclusions given' => [
'Siblings: simple exclusions given' => [
'source' => '/var/www/source/two',
'destination' => '/var/www/destination/two',
'exclusions' => new PathList('three.txt', 'four.txt'),
'command' => [
'optionalArguments' => [new PathList('three.txt', 'four.txt'), new TestOutputCallback()],
'expectedCommand' => [
'--archive',
'--delete-after',
'--verbose',
Expand All @@ -123,18 +136,20 @@ public function providerSync(): array
'/var/www/source/two/',
'/var/www/destination/two',
],
'callback' => new TestOutputCallback(),
'expectedCallback' => new TestOutputCallback(),
],
'Siblings: duplicate exclusions given' => [
'source' => '/var/www/source/three',
'destination' => '/var/www/destination/three',
'exclusions' => new PathList(...[
'four/five',
'six/seven',
'six/seven',
'six/seven',
]),
'command' => [
'optionalArguments' => [
new PathList(...[
'four/five',
'six/seven',
'six/seven',
'six/seven',
]),
],
'expectedCommand' => [
'--archive',
'--delete-after',
'--verbose',
Expand All @@ -143,19 +158,21 @@ public function providerSync(): array
'/var/www/source/three/',
'/var/www/destination/three',
],
'callback' => null,
'expectedCallback' => null,
],
'Siblings: Windows directory separators' => [
'source' => '/var/www/source/one\\two',
'destination' => '/var/www/destination\\one/two',
'exclusions' => new PathList(...[
'three\\four',
'five/six/seven/eight',
'five/six/seven/eight',
'five\\six/seven\\eight',
'five/six\\seven/eight',
]),
'command' => [
'optionalArguments' => [
new PathList(...[
'three\\four',
'five/six/seven/eight',
'five/six/seven/eight',
'five\\six/seven\\eight',
'five/six\\seven/eight',
]),
],
'expectedCommand' => [
'--archive',
'--delete-after',
'--verbose',
Expand All @@ -164,26 +181,26 @@ public function providerSync(): array
'/var/www/source/one/two/',
'/var/www/destination/one/two',
],
'callback' => null,
'expectedCallback' => null,
],
'Nested: destination inside source (neither is excluded)' => [
'source' => '/var/www/source',
'destination' => '/var/www/source/destination',
'exclusions' => null,
'command' => [
'optionalArguments' => [],
'expectedCommand' => [
'--archive',
'--delete-after',
'--verbose',
'/var/www/source/',
'/var/www/source/destination',
],
'callback' => null,
'expectedCallback' => null,
],
'Nested: source inside destination (source is excluded)' => [
'source' => '/var/www/destination/source',
'destination' => '/var/www/destination',
'exclusions' => null,
'command' => [
'optionalArguments' => [],
'expectedCommand' => [
'--archive',
'--delete-after',
'--verbose',
Expand All @@ -192,13 +209,13 @@ public function providerSync(): array
'/var/www/destination/source/',
'/var/www/destination',
],
'callback' => null,
'expectedCallback' => null,
],
'Nested: with Windows directory separators' => [
'source' => '/var/www/destination\\source',
'destination' => '/var/www/destination',
'exclusions' => null,
'command' => [
'optionalArguments' => [],
'expectedCommand' => [
'--archive',
'--delete-after',
'--verbose',
Expand All @@ -207,7 +224,7 @@ public function providerSync(): array
'/var/www/destination/source/',
'/var/www/destination',
],
'callback' => null,
'expectedCallback' => null,
],
];
}
Expand Down
Loading
Loading