Skip to content

Commit 82d5024

Browse files
committed
fix: preserve env vars when running occ as file owner
Ensure environment variables are passed when command execution switches user via runuser, and make PyStringNode cast explicit in assertion messages for static-analysis compatibility. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 4eb8d38 commit 82d5024

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

src/NextcloudApiContext.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,17 @@ public static function runCommand(string $command): array {
531531
if ($owner === false) {
532532
throw new \Exception('Could not retrieve owner information for UID ' . $fileOwnerUid);
533533
}
534-
$fullCommand = 'php ' . $console . ' ' . $command;
535-
if (!empty(self::$environments)) {
536-
$fullCommand = http_build_query(self::$environments, '', ' ') . ' ' . $fullCommand;
537-
}
534+
$baseCommand = 'php ' . $console . ' ' . $command;
535+
$environmentPrefix = !empty(self::$environments)
536+
? http_build_query(self::$environments, '', ' ')
537+
: '';
538+
538539
if (posix_getuid() !== $owner['uid']) {
539-
$fullCommand = 'runuser -u ' . $owner['name'] . ' -- ' . $fullCommand;
540+
$fullCommand = 'runuser -u ' . $owner['name'] . ' -- '
541+
. ($environmentPrefix !== '' ? 'env ' . $environmentPrefix . ' ' : '')
542+
. $baseCommand;
543+
} else {
544+
$fullCommand = ($environmentPrefix !== '' ? $environmentPrefix . ' ' : '') . $baseCommand;
540545
}
541546
$fullCommand .= ' 2>&1';
542547
return self::runBashCommand($fullCommand);
@@ -586,7 +591,7 @@ private static function runBashCommand(string $command): array {
586591

587592
#[Given('the output of the last command should contain the following text:')]
588593
public static function theOutputOfTheLastCommandContains(PyStringNode $text): void {
589-
Assert::assertStringContainsString((string) $text, self::$commandOutput, 'The output of the last command does not contain: ' . $text);
594+
Assert::assertStringContainsString((string) $text, self::$commandOutput, 'The output of the last command does not contain: ' . (string) $text);
590595
}
591596

592597
#[Given('the output of the last command should be empty')]

0 commit comments

Comments
 (0)