Skip to content

Commit

Permalink
Finish adding argument to Process creation chain per #325.
Browse files Browse the repository at this point in the history
  • Loading branch information
TravisCarden committed Feb 14, 2024
1 parent 439d50f commit b616b4c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/API/Process/Factory/ProcessFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpTuf\ComposerStager\API\Process\Factory;

use PhpTuf\ComposerStager\API\Path\Value\PathInterface;
use PhpTuf\ComposerStager\API\Process\Service\ProcessInterface;

/**
Expand All @@ -26,6 +27,9 @@ interface ProcessFactoryInterface
* '--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 @@ -42,5 +46,5 @@ interface ProcessFactoryInterface
* @throws \PhpTuf\ComposerStager\API\Exception\LogicException
* If the process cannot be created due to host configuration.
*/
public function create(array $command, array $env = []): ProcessInterface;
public function create(array $command, ?PathInterface $cwd = null, array $env = []): ProcessInterface;
}
5 changes: 3 additions & 2 deletions src/Internal/Process/Factory/ProcessFactory.php
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 PhpTuf\ComposerStager\API\Process\Factory\ProcessFactoryInterface;
use PhpTuf\ComposerStager\API\Process\Service\ProcessInterface;
use PhpTuf\ComposerStager\API\Translation\Factory\TranslatableFactoryInterface;
Expand All @@ -20,8 +21,8 @@ public function __construct(
) {
}

public function create(array $command, array $env = []): ProcessInterface
public function create(array $command, ?PathInterface $cwd = null, array $env = []): ProcessInterface
{
return new Process($this->symfonyProcessFactory, $this->translatableFactory, $command, $env);
return new Process($this->symfonyProcessFactory, $this->translatableFactory, $command, $cwd, $env);
}
}
2 changes: 1 addition & 1 deletion src/Internal/Process/Service/AbstractProcessRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function run(
int $timeout = ProcessInterface::DEFAULT_TIMEOUT,
): void {
array_unshift($command, $this->findExecutable());
$process = $this->processFactory->create($command, $env);
$process = $this->processFactory->create($command, $cwd, $env);
$process->setTimeout($timeout);
$process->mustRun($callback);
}
Expand Down
7 changes: 6 additions & 1 deletion src/Internal/Process/Service/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpTuf\ComposerStager\API\Exception\InvalidArgumentException;
use PhpTuf\ComposerStager\API\Exception\LogicException;
use PhpTuf\ComposerStager\API\Exception\RuntimeException;
use PhpTuf\ComposerStager\API\Path\Value\PathInterface;
use PhpTuf\ComposerStager\API\Process\Service\OutputCallbackInterface;
use PhpTuf\ComposerStager\API\Process\Service\ProcessInterface;
use PhpTuf\ComposerStager\API\Translation\Factory\TranslatableFactoryInterface;
Expand Down Expand Up @@ -36,6 +37,9 @@ final class Process implements ProcessInterface
* '--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 @@ -56,10 +60,11 @@ public function __construct(
private readonly SymfonyProcessFactoryInterface $symfonyProcessFactory,
TranslatableFactoryInterface $translatableFactory,
array $command = [],
?PathInterface $cwd = null,
array $env = [],
) {
$this->setTranslatableFactory($translatableFactory);
$this->symfonyProcess = $this->symfonyProcessFactory->create($command, null, $env);
$this->symfonyProcess = $this->symfonyProcessFactory->create($command, $cwd, $env);
}

public function getEnv(): array
Expand Down
12 changes: 8 additions & 4 deletions tests/Process/Factory/ProcessFactoryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ public function providerFactory(): array
{
return [
'Minimum values' => [
'command' => [],
'optionalArguments' => [[]],
'command' => ['one'],
'optionalArguments' => [],
],
'Default values' => [
'command' => ['one'],
'optionalArguments' => [null, []],
],
'Simple command' => [
'command' => ['one'],
Expand All @@ -45,9 +49,9 @@ public function providerFactory(): array
'command' => ['one', 'two', 'three'],
'optionalArguments' => [],
],
'Command plus env' => [
'Command plus optional arguments' => [
'command' => ['one'],
'optionalArguments' => [['TWO' => 'two']],
'optionalArguments' => [null, ['TWO' => 'two']],
],
];
}
Expand Down
16 changes: 12 additions & 4 deletions tests/Process/Service/AbstractProcessRunnerUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,29 @@ public function providerBasicFunctionality(): array
'Minimum values' => [
'executableName' => 'one',
'givenRunArguments' => [[]],
'expectedFactoryArguments' => [['one'], []],
'expectedFactoryArguments' => [['one'], null, []],
'callback' => null,
'timeout' => ProcessInterface::DEFAULT_TIMEOUT,
],
'Default values' => [
'executableName' => 'one',
'givenRunArguments' => [['two'], null, []],
'expectedFactoryArguments' => [['one', 'two'], null, []],
'callback' => null,
'timeout' => ProcessInterface::DEFAULT_TIMEOUT,
],
'Simple values' => [
'executableName' => 'two',
'executableName' => 'one',
'givenRunArguments' => [
['three', 'four'],
['two', 'three', 'four'],
PathHelper::arbitraryDirPath(),
['ONE' => 'one', 'TWO' => 'two'],
new TestOutputCallback(),
100,
],
'expectedFactoryArguments' => [
['two', 'three', 'four'],
['one', 'two', 'three', 'four'],
PathHelper::arbitraryDirPath(),
['ONE' => 'one', 'TWO' => 'two'],
],
'callback' => new TestOutputCallback(),
Expand Down
3 changes: 2 additions & 1 deletion tests/Process/Service/ProcessUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public function providerEnv(): array
],
'Initial env, no change' => [
'optionalArguments' => [
null,
[
'ONE' => 'one',
'TWO' => 'two',
Expand All @@ -221,7 +222,7 @@ public function providerEnv(): array
'givenNewEnv' => [],
],
'No env argument, changed' => [
'optionalArguments' => [['one' => 'two']],
'optionalArguments' => [null, ['one' => 'two']],
'expectedInitialEnv' => ['one' => 'two'],
'givenNewEnv' => [
'ONE' => 'one',
Expand Down

0 comments on commit b616b4c

Please sign in to comment.