Skip to content

Commit 99e7fb2

Browse files
authored
fix: route workspace clone CLI through ability (#621)
1 parent 1415cbe commit 99e7fb2

2 files changed

Lines changed: 145 additions & 8 deletions

File tree

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,18 @@ public function clone_repo( array $args, array $assoc_args ): void {
223223
return;
224224
}
225225

226-
$workspace = new Workspace();
227-
$result = $workspace->clone_repo(
228-
$args[0],
229-
$assoc_args['name'] ?? null,
226+
$ability = wp_get_ability('datamachine-code/workspace-clone');
227+
if ( ! $ability ) {
228+
WP_CLI::error('Workspace clone ability not available.');
229+
return;
230+
}
231+
232+
$result = $ability->execute(
230233
array(
234+
'url' => $args[0],
235+
'name' => $assoc_args['name'] ?? null,
231236
'full' => isset($assoc_args['full']),
232237
'allow_duplicate_remote' => isset($assoc_args['allow-duplicate-remote']),
233-
'progress_callback' => static function ( array $event ): void {
234-
$elapsed = number_format( (float) ( $event['elapsed'] ?? 0 ), 1);
235-
WP_CLI::log(sprintf('[clone %ss] %s', $elapsed, (string) ( $event['message'] ?? '' )));
236-
},
237238
)
238239
);
239240

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Pure-PHP smoke for workspace clone CLI routing.
4+
*
5+
* Run: php tests/smoke-workspace-clone-cli.php
6+
*/
7+
8+
declare( strict_types=1 );
9+
10+
namespace {
11+
if ( ! defined('ABSPATH') ) {
12+
define('ABSPATH', __DIR__);
13+
}
14+
15+
class WP_CLI
16+
{
17+
public static array $logs = array();
18+
public static array $successes = array();
19+
20+
public static function error( string $message ): void
21+
{
22+
throw new RuntimeException($message);
23+
}
24+
25+
public static function success( string $message ): void
26+
{
27+
self::$successes[] = $message;
28+
}
29+
30+
public static function log( string $message ): void
31+
{
32+
self::$logs[] = $message;
33+
}
34+
}
35+
36+
class WP_Error
37+
{
38+
public function __construct( private string $code, private string $message, private array $data = array() )
39+
{
40+
}
41+
42+
public function get_error_code(): string
43+
{
44+
return $this->code;
45+
}
46+
47+
public function get_error_message(): string
48+
{
49+
return $this->message;
50+
}
51+
52+
public function get_error_data(): array
53+
{
54+
return $this->data;
55+
}
56+
}
57+
58+
function is_wp_error( $value ): bool
59+
{
60+
return $value instanceof WP_Error;
61+
}
62+
63+
function wp_get_ability( string $name )
64+
{
65+
return $GLOBALS['__abilities'][ $name ] ?? null;
66+
}
67+
}
68+
69+
namespace DataMachine\Cli {
70+
class BaseCommand
71+
{
72+
}
73+
}
74+
75+
namespace {
76+
include_once dirname(__DIR__) . '/inc/Cli/Commands/WorkspaceCommand.php';
77+
78+
final class WorkspaceCloneTestAbility
79+
{
80+
public array $input = array();
81+
82+
public function execute( array $input ): array
83+
{
84+
$this->input = $input;
85+
return array(
86+
'success' => true,
87+
'backend' => 'github_api',
88+
'name' => (string) $input['name'],
89+
'path' => 'github://Extra-Chill/data-machine-code',
90+
'message' => 'Registered remote workspace.',
91+
);
92+
}
93+
}
94+
95+
$failures = array();
96+
$assert = function ( string $label, bool $condition ) use ( &$failures ): void {
97+
if ( $condition ) {
98+
echo " ok {$label}\n";
99+
return;
100+
}
101+
102+
$failures[] = $label;
103+
echo " fail {$label}\n";
104+
};
105+
106+
echo "Workspace clone CLI - smoke\n";
107+
108+
$ability = new WorkspaceCloneTestAbility();
109+
$GLOBALS['__abilities']['datamachine-code/workspace-clone'] = $ability;
110+
111+
$command = new \DataMachineCode\Cli\Commands\WorkspaceCommand();
112+
$command->clone_repo(
113+
array( 'https://github.com/Extra-Chill/data-machine-code.git' ),
114+
array(
115+
'name' => 'data-machine-code',
116+
'allow-duplicate-remote' => true,
117+
)
118+
);
119+
120+
$assert('uses workspace clone ability', 'https://github.com/Extra-Chill/data-machine-code.git' === ( $ability->input['url'] ?? '' ));
121+
$assert('passes explicit clone name', 'data-machine-code' === ( $ability->input['name'] ?? '' ));
122+
$assert('passes duplicate remote opt-in', true === ( $ability->input['allow_duplicate_remote'] ?? false ));
123+
$assert('does not force full clone by default', false === ( $ability->input['full'] ?? true ));
124+
$assert('prints ability success message', in_array('Registered remote workspace.', WP_CLI::$successes, true));
125+
$assert('prints returned workspace path', in_array('Path: github://Extra-Chill/data-machine-code', WP_CLI::$logs, true));
126+
127+
if ( $failures ) {
128+
echo "\nFailures:\n";
129+
foreach ( $failures as $failure ) {
130+
echo " - {$failure}\n";
131+
}
132+
exit(1);
133+
}
134+
135+
echo "\nOK\n";
136+
}

0 commit comments

Comments
 (0)