Skip to content

Commit 4e88ab2

Browse files
committed
Add tests for Master and update JoyTest integration
1 parent 99a6008 commit 4e88ab2

File tree

18 files changed

+289
-34
lines changed

18 files changed

+289
-34
lines changed

.github/workflows/test.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ jobs:
5151

5252
- name: Run test suite
5353
run: composer test
54+
55+
- name: Run tests with coverage
56+
if: matrix.php-versions == '8.3'
57+
run: vendor/bin/phpunit --coverage-text --coverage-clover=build/clover.xml

src/Command/ConfigCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ protected function execute(Input $input, Output $output): int
4242
$hosts = $this->selectHosts($input, $output);
4343
$data = [];
4444
$keys = $this->deployer->config->keys();
45-
if (!defined('DEPLOYER_NO_ASK')) {
46-
define('DEPLOYER_NO_ASK', true);
47-
}
45+
WillAskUser::$noAsk = true;
4846
foreach ($hosts as $host) {
4947
Context::push(new Context($host));
5048
$values = [];

src/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function load(): void
180180
return;
181181
}
182182

183-
$values = Httpie::get(MASTER_ENDPOINT . '/load')
183+
$values = Httpie::post(MASTER_ENDPOINT . '/load')
184184
->setopt(CURLOPT_CONNECTTIMEOUT, 0)
185185
->setopt(CURLOPT_TIMEOUT, 0)
186186
->header('Authorization', 'Bearer ' . MASTER_TOKEN)
@@ -197,7 +197,7 @@ public function save(): void
197197
return;
198198
}
199199

200-
Httpie::get(MASTER_ENDPOINT . '/save')
200+
Httpie::post(MASTER_ENDPOINT . '/save')
201201
->setopt(CURLOPT_CONNECTTIMEOUT, 0)
202202
->setopt(CURLOPT_TIMEOUT, 0)
203203
->header('Authorization', 'Bearer ' . MASTER_TOKEN)

src/Deployer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public static function masterCall(Host $host, string $func, mixed ...$arguments)
314314
// in order for ticker gather worker outputs and print it to user.
315315
usleep(100_000); // Sleep 100ms.
316316

317-
return Httpie::get(MASTER_ENDPOINT . '/proxy')
317+
return Httpie::post(MASTER_ENDPOINT . '/proxy')
318318
->setopt(CURLOPT_CONNECTTIMEOUT, 0) // no timeout
319319
->setopt(CURLOPT_TIMEOUT, 0) // no timeout
320320
->header('Authorization', 'Bearer ' . MASTER_TOKEN)

src/Exception/WillAskUser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
class WillAskUser extends Exception
1414
{
15+
public static bool $noAsk = false;
16+
1517
public function __construct(string $message)
1618
{
1719
parent::__construct($message);

src/Executor/Master.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ private function runTask(Task $task, array $hosts): int
219219
case '/proxy':
220220
['host' => $host, 'func' => $func, 'arguments' => $arguments] = $payload;
221221

222+
$allowedFunctions = [
223+
'Deployer\ask',
224+
'Deployer\askChoice',
225+
'Deployer\askConfirmation',
226+
'Deployer\askHiddenResponse',
227+
];
228+
if (!in_array($func, $allowedFunctions, true)) {
229+
return new Response(403, ['error' => "Function not allowed: $func"]);
230+
}
231+
222232
Context::push(new Context($this->hosts->get($host)));
223233
$answer = call_user_func($func, ...$arguments);
224234
Context::pop();

src/functions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ function has(string $name): bool
763763

764764
function ask(string $message, ?string $default = null, ?array $autocomplete = null): ?string
765765
{
766-
if (defined('DEPLOYER_NO_ASK')) {
766+
if (WillAskUser::$noAsk) {
767767
throw new WillAskUser($message);
768768
}
769769
Context::required(__FUNCTION__);
@@ -798,7 +798,7 @@ function ask(string $message, ?string $default = null, ?array $autocomplete = nu
798798
*/
799799
function askChoice(string $message, array $availableChoices, $default = null, bool $multiselect = false)
800800
{
801-
if (defined('DEPLOYER_NO_ASK')) {
801+
if (WillAskUser::$noAsk) {
802802
throw new WillAskUser($message);
803803
}
804804
Context::required(__FUNCTION__);
@@ -837,7 +837,7 @@ function askChoice(string $message, array $availableChoices, $default = null, bo
837837

838838
function askConfirmation(string $message, bool $default = false): bool
839839
{
840-
if (defined('DEPLOYER_NO_ASK')) {
840+
if (WillAskUser::$noAsk) {
841841
throw new WillAskUser($message);
842842
}
843843
Context::required(__FUNCTION__);
@@ -865,7 +865,7 @@ function askConfirmation(string $message, bool $default = false): bool
865865

866866
function askHiddenResponse(string $message): string
867867
{
868-
if (defined('DEPLOYER_NO_ASK')) {
868+
if (WillAskUser::$noAsk) {
869869
throw new WillAskUser($message);
870870
}
871871
Context::required(__FUNCTION__);

tests/joy/JoyTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,22 @@ protected function dep(string $task, array $args = []): int
7373
]);
7474
}
7575

76-
abstract protected function recipe(): string;
76+
protected function depFile(string $recipe, string $task, array $args = []): int
77+
{
78+
$this->init($recipe);
79+
return $this->tester->run(array_merge([
80+
$task,
81+
'selector' => 'all',
82+
'--file' => $recipe,
83+
'--limit' => 1,
84+
], $args), [
85+
'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
86+
'interactive' => false,
87+
]);
88+
}
89+
90+
protected function recipe(): string
91+
{
92+
return '';
93+
}
7794
}

tests/legacy/CurrentPathTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
namespace Deployer;
99

10+
use joy\JoyTest;
1011
use Symfony\Component\Console\Output\Output;
1112

12-
class CurrentPathTest extends AbstractTest
13+
class CurrentPathTest extends JoyTest
1314
{
1415
public const RECIPE = __DIR__ . '/recipe/deploy.php';
1516

tests/legacy/DeployTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77

88
namespace Deployer;
99

10+
use joy\JoyTest;
1011
use PHPUnit\Framework\Attributes\Depends;
1112
use Symfony\Component\Console\Output\Output;
1213

13-
class DeployTest extends AbstractTest
14+
class DeployTest extends JoyTest
1415
{
1516
public const RECIPE = __DIR__ . '/recipe/deploy.php';
1617

1718
public function testDeploy()
1819
{
19-
$display = $this->dep(self::RECIPE, 'deploy');
20+
$this->depFile(self::RECIPE, 'deploy');
2021

2122
$display = $this->tester->getDisplay();
2223
self::assertEquals(0, $this->tester->getStatusCode(), $display);
@@ -56,17 +57,17 @@ public function testDeploySelectHosts()
5657
public function testKeepReleases()
5758
{
5859
for ($i = 0; $i < 3; $i++) {
59-
$this->dep(self::RECIPE, 'deploy');
60+
$this->depFile(self::RECIPE, 'deploy');
6061
self::assertEquals(0, $this->tester->getStatusCode(), $this->tester->getDisplay());
6162
}
6263

6364
for ($i = 0; $i < 6; $i++) {
64-
$this->dep(self::RECIPE, 'deploy:fail');
65+
$this->depFile(self::RECIPE, 'deploy:fail');
6566
self::assertEquals(1, $this->tester->getStatusCode(), $this->tester->getDisplay());
6667
}
6768

6869
for ($i = 0; $i < 3; $i++) {
69-
$this->dep(self::RECIPE, 'deploy');
70+
$this->depFile(self::RECIPE, 'deploy');
7071
self::assertEquals(0, $this->tester->getStatusCode(), $this->tester->getDisplay());
7172
}
7273

@@ -80,7 +81,7 @@ public function testKeepReleases()
8081
#[Depends('testKeepReleases')]
8182
public function testRollback()
8283
{
83-
$this->dep(self::RECIPE, 'rollback');
84+
$this->depFile(self::RECIPE, 'rollback');
8485

8586
self::assertEquals(0, $this->tester->getStatusCode(), $this->tester->getDisplay());
8687

@@ -93,7 +94,7 @@ public function testRollback()
9394

9495
public function testFail()
9596
{
96-
$this->dep(self::RECIPE, 'deploy:fail');
97+
$this->depFile(self::RECIPE, 'deploy:fail');
9798

9899
$display = $this->tester->getDisplay();
99100
self::assertEquals(1, $this->tester->getStatusCode(), $display);
@@ -108,7 +109,7 @@ public function testFail()
108109
#[Depends('testFail')]
109110
public function testCleanup()
110111
{
111-
$this->dep(self::RECIPE, 'deploy:cleanup');
112+
$this->depFile(self::RECIPE, 'deploy:cleanup');
112113

113114
self::assertEquals(0, $this->tester->getStatusCode(), $this->tester->getDisplay());
114115

@@ -121,8 +122,8 @@ public function testCleanup()
121122

122123
public function testIsUnlockedExitsWithOneWhenDeployIsLocked()
123124
{
124-
$this->dep(self::RECIPE, 'deploy:lock');
125-
$this->dep(self::RECIPE, 'deploy:is_locked');
125+
$this->depFile(self::RECIPE, 'deploy:lock');
126+
$this->depFile(self::RECIPE, 'deploy:is_locked');
126127
$display = $this->tester->getDisplay();
127128

128129
self::assertStringContainsString('Deploy is locked by ', $display);
@@ -131,8 +132,8 @@ public function testIsUnlockedExitsWithOneWhenDeployIsLocked()
131132

132133
public function testIsUnlockedExitsWithZeroWhenDeployIsNotLocked()
133134
{
134-
$this->dep(self::RECIPE, 'deploy:unlock');
135-
$this->dep(self::RECIPE, 'deploy:is_locked');
135+
$this->depFile(self::RECIPE, 'deploy:unlock');
136+
$this->depFile(self::RECIPE, 'deploy:is_locked');
136137
$display = $this->tester->getDisplay();
137138

138139
self::assertStringContainsString('Deploy is unlocked.', $display);

0 commit comments

Comments
 (0)