Skip to content

Commit

Permalink
feature: set environment variables
Browse files Browse the repository at this point in the history
closes #239
  • Loading branch information
g105b committed May 8, 2024
1 parent 820794a commit bb74f6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Process {
/** @var array<string, mixed> */
protected array $status;
protected bool $isBlocking = false;
/** @var array<string, string> */
protected array $env = [];

public function __construct(string...$command) {
$this->command = $command;
Expand All @@ -31,6 +33,10 @@ public function setExecCwd(string $cwd):void {
$this->cwd = $cwd;
}

public function setEnv(string $key, string $value):void {
$this->env[$key] = $value;
}

/**
* Runs the command in a concurrent thread.
* Sets the input, output and errors streams.
Expand All @@ -54,7 +60,7 @@ public function exec():void {
$this->command,
$descriptor,
$this->pipes,
// env_vars: $this->env,
env_vars: $this->env,
);
if(!$this->process) {
throw new CommandNotFoundException($this->command[0]);
Expand Down
10 changes: 9 additions & 1 deletion test/phpunit/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,12 @@ public function testExecBlocking() {
$sut->exec();
self::assertFalse($sut->isRunning());
}
}

public function testSetEnv() {
$sut = new Process("printenv");
$sut->setEnv("NAME", "PHPUnit");
$sut->exec();
$output = $sut->getOutput();
self::assertStringContainsString("NAME=PHPUnit\n", $output);
}
}

0 comments on commit bb74f6f

Please sign in to comment.