Skip to content

Commit 5f64839

Browse files
committed
feat: add more steps to works with commands
Signed-off-by: Vitor Mattos <[email protected]>
1 parent e08afc9 commit 5f64839

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ When the response should contain the initial state :name with the following valu
6262
When the response should contain the initial state :name json that match with:
6363
When the following :appId app config is set
6464
When /^run the command "(?P<command>(?:[^"]|\\")*)"$/
65+
When the output of the last command should contain the following text:
66+
When the output of the last command should be empty
6567
When /^run the command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/
6668
When /^run the bash command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/
69+
When create an environment :name with value :value to be used by occ command
6770
```
6871

6972
## Tips
@@ -89,7 +92,7 @@ When sending "post" to ocs "/apps/libresign/api/v1/request-signature"
8992

9093
### Step: all steps that run commands
9194

92-
Before the command be executd, will replace the follow placeholders:
95+
Before the command is executed, the following placeholders will be replaced:
9396

9497
| Placeholder | Value |
9598
| -------------------- | ---------------------- |

features/test.feature

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,35 @@ Feature: Test this extension
210210

211211
Scenario: Test list app directory with success
212212
When run the bash command "ls <appRootDir>" with result code 0
213+
Then the output of the last command should contain the following text:
214+
"""
215+
appinfo
216+
"""
213217

214218
Scenario: Test list Nextcloud directory with success
215219
When run the bash command "ls <nextcloudRootDir>" with result code 0
216220

217221
Scenario: Test run bash command with success
218222
When run the bash command "true" with result code 0
223+
Then the output of the last command should be empty
219224

220225
Scenario: Test run bash command with error
221226
When run the bash command "false" with result code 1
222227

223228
Scenario: Run occ command with success
224229
When run the command "status" with result code 0
230+
Then the output of the last command should contain the following text:
231+
"""
232+
version:
233+
"""
225234

226235
Scenario: Run occ command with success
227236
When run the command "invalid-command" with result code 1
237+
238+
Scenario: Create an environment with value to be used by occ command
239+
When create an environment "OC_PASS" with value "" to be used by occ command
240+
And run the command "user:add --password-from-env test" with result code 1
241+
Then the output of the last command should contain the following text:
242+
"""
243+
--password-from-env given, but NC_PASS/OC_PASS is empty!
244+
"""

src/NextcloudApiContext.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ class NextcloudApiContext implements Context {
2626
protected RunServerListener $server;
2727
protected string $currentUser = '';
2828
protected array $fields = [];
29+
protected static array $environments = [];
30+
protected static string $commandOutput = '';
31+
2932
/**
3033
* @var string[]
3134
*/
32-
protected array $createdUsers = [];
35+
protected static array $createdUsers = [];
3336
protected ResponseInterface $response;
3437
/** @var CookieJar[] */
3538
protected $cookieJars;
@@ -61,13 +64,15 @@ public static function beforeSuite(BeforeSuiteScope $scope):void {
6164
"runuser -u %s -- %s\n\n",
6265
get_current_user(), $whoami, get_current_user(), $command));
6366
}
67+
self::runCommand('config:system:set debug --value true --type boolean');
6468
}
6569

6670
/**
6771
* @BeforeScenario
6872
*/
69-
public function setUp(): void {
70-
$this->createdUsers = [];
73+
public function beforeScenario(): void {
74+
self::$createdUsers = [];
75+
self::$environments = [];
7176
}
7277

7378
/**
@@ -116,7 +121,7 @@ protected function createUser(string $user): void {
116121
$this->sendOCSRequest('GET', '/cloud/users' . '/' . $user);
117122
$this->assertStatusCode($this->response, 200, 'Failed to do first login');
118123

119-
$this->createdUsers[] = $user;
124+
self::$createdUsers[] = $user;
120125

121126
$this->setCurrentUser($currentUser);
122127
}
@@ -477,6 +482,9 @@ public static function runCommand(string $command): array {
477482
throw new \Exception('Could not retrieve owner information for UID ' . $fileOwnerUid);
478483
}
479484
$fullCommand = 'php ' . $console . ' ' . $command;
485+
if (!empty(self::$environments)) {
486+
$fullCommand = http_build_query(self::$environments, '', ' ') . ' ' . $fullCommand;
487+
}
480488
if (posix_getuid() !== $owner['uid']) {
481489
$fullCommand = 'runuser -u ' . $owner['name'] . ' -- ' . $fullCommand;
482490
}
@@ -518,13 +526,28 @@ private static function runBashCommand(string $command): array {
518526
}
519527

520528
exec($command, $output, $resultCode);
529+
self::$commandOutput = implode("\n", $output);
521530
return [
522531
'command' => $command,
523532
'output' => $output,
524533
'resultCode' => $resultCode,
525534
];
526535
}
527536

537+
/**
538+
* @When the output of the last command should contain the following text:
539+
*/
540+
public static function theOutputOfTheLastCommandContains(PyStringNode $text): void {
541+
Assert::assertStringContainsString((string) $text, self::$commandOutput, 'The output of the last command does not contain: ' . $text);
542+
}
543+
544+
/**
545+
* @When the output of the last command should be empty
546+
*/
547+
public static function theOutputOfTheLastCommandShouldBeEmpty(): void {
548+
Assert::assertEmpty(self::$commandOutput, 'The output of the last command should be empty, but got: ' . self::$commandOutput);
549+
}
550+
528551
/**
529552
* @When /^run the command "(?P<command>(?:[^"]|\\")*)" with result code (\d+)$/
530553
*/
@@ -541,11 +564,19 @@ public static function runBashCommandWithResultCode(string $command, int $result
541564
Assert::assertEquals($resultCode, $return['resultCode'], print_r($return, true));
542565
}
543566

567+
/**
568+
* @When create an environment :name with value :value to be used by occ command
569+
*/
570+
public static function createAnEnvironmentWithValueToBeUsedByOccCommand(string $name, string $value):void {
571+
self::$environments[$name] = $value;
572+
}
573+
544574
/**
545575
* @AfterScenario
546576
*/
547577
public function tearDown(): void {
548-
foreach ($this->createdUsers as $user) {
578+
self::$environments = [];
579+
foreach (self::$createdUsers as $user) {
549580
$this->deleteUser($user);
550581
}
551582
}
@@ -556,7 +587,7 @@ protected function deleteUser(string $user): ResponseInterface {
556587
$this->sendOCSRequest('DELETE', '/cloud/users/' . $user);
557588
$this->setCurrentUser($currentUser);
558589

559-
unset($this->createdUsers[array_search($user, $this->createdUsers, true)]);
590+
unset(self::$createdUsers[array_search($user, self::$createdUsers, true)]);
560591

561592
return $this->response;
562593
}

0 commit comments

Comments
 (0)