-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmoke-workspace-local-fallback.php
More file actions
481 lines (422 loc) · 17.5 KB
/
Copy pathsmoke-workspace-local-fallback.php
File metadata and controls
481 lines (422 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
/**
* Pure-PHP smoke for local workspace fallback when remote state exists.
*
* Run: php tests/smoke-workspace-local-fallback.php
*/
declare( strict_types=1 );
namespace DataMachineCode\Abilities {
class AbilityRegistry
{
}
class GitHubAbilities
{
}
}
namespace DataMachineCode\Workspace {
class RemoteWorkspaceBackend
{
public static array $show_input = array();
public static array $read_input = array();
public static array $list_input = array();
public static array $grep_input = array();
public static array $git_status_input = array();
public static array $worktree_input = array();
public static bool $show_returns_remote_success = false;
public static bool $read_returns_remote_success = false;
public static bool $list_returns_remote_success = false;
public static bool $grep_returns_remote_success = false;
public static bool $git_status_returns_remote_success = false;
public static bool $worktree_returns_remote_success = false;
public static function should_handle(): bool
{
return true;
}
public function show( string $handle ): array|\WP_Error
{
self::$show_input = compact('handle');
if ( self::$show_returns_remote_success ) {
return array(
'success' => true,
'name' => $handle,
'repo' => $handle,
'is_worktree' => str_contains($handle, '@'),
'path' => 'github://Automattic/' . str_replace('@', '#', $handle),
);
}
return new \WP_Error(
'remote_workspace_repo_not_found',
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
);
}
public function worktree_add( string $repo_name, string $branch, ?string $from = null ): array|\WP_Error
{
self::$worktree_input = compact('repo_name', 'branch', 'from');
if ( self::$worktree_returns_remote_success ) {
return array(
'success' => true,
'handle' => $repo_name . '@' . str_replace('/', '-', $branch),
'path' => 'github://Automattic/' . $repo_name . '#' . $branch,
);
}
return new \WP_Error(
'remote_workspace_repo_not_found',
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
);
}
public function read_file( string $handle, string $path, int $max_size, ?int $offset = null, ?int $limit = null ): array|\WP_Error
{
self::$read_input = compact('handle', 'path', 'max_size', 'offset', 'limit');
if ( self::$read_returns_remote_success ) {
return array(
'success' => true,
'backend' => 'github_api',
'content' => 'remote file',
'path' => $path,
'size' => 11,
);
}
return new \WP_Error(
'remote_workspace_repo_not_found',
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
);
}
public function list_directory( string $handle, ?string $path = null ): array|\WP_Error
{
self::$list_input = compact('handle', 'path');
if ( self::$list_returns_remote_success ) {
return array(
'success' => true,
'backend' => 'github_api',
'repo' => $handle,
'path' => $path ?? '/',
'entries' => array(),
);
}
return new \WP_Error(
'remote_workspace_repo_not_found',
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
);
}
public function grep( string $handle, string $pattern, ?string $path = null, ?string $include_pattern = null, int $max_results = 100, int $context_lines = 0 ): array|\WP_Error
{
self::$grep_input = compact('handle', 'pattern', 'path', 'include_pattern', 'max_results', 'context_lines');
if ( self::$grep_returns_remote_success ) {
return array(
'success' => true,
'backend' => 'github_api',
'repo' => $handle,
'path' => $path ?? '/',
'pattern' => $pattern,
'matches' => array(),
'count' => 0,
);
}
return new \WP_Error(
'remote_workspace_repo_not_found',
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
);
}
public function git_status( string $handle ): array|\WP_Error
{
self::$git_status_input = compact('handle');
if ( self::$git_status_returns_remote_success ) {
return array(
'success' => true,
'backend' => 'github_api',
'name' => $handle,
'path' => 'github://Automattic/' . str_replace('@', '#', $handle),
'dirty' => 0,
'files' => array(),
);
}
return new \WP_Error(
'remote_workspace_repo_not_found',
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
);
}
}
class Workspace
{
public static array $show_input = array();
public static array $git_status_input = array();
public static array $worktree_input = array();
public static bool $show_returns_local_success = true;
public function show_repo( string $handle ): array|\WP_Error
{
self::$show_input = compact('handle');
if ( ! self::$show_returns_local_success ) {
return new \WP_Error('workspace_repo_not_found', 'Workspace repository not found.');
}
$parsed = explode('@', $handle, 2);
$repo = $parsed[0];
$slug = $parsed[1] ?? '';
$path = '/Users/chubes/Developer/' . $handle;
return array(
'success' => true,
'name' => $handle,
'repo' => $repo,
'is_worktree' => '' !== $slug,
'path' => $path,
'branch' => '' !== $slug ? str_replace('-', '/', $slug) : 'main',
'remote' => 'git@github.a8c.com:Automattic/wpcom-codebox.git',
'commit' => 'abc123 listed primary',
'dirty' => 0,
);
}
public function worktree_add(
string $repo,
string $branch,
?string $from = null,
bool $inject_context = true,
bool $bootstrap = true,
bool $allow_stale = false,
bool $rebase_base = false,
bool $force = false,
array $task = array()
): array {
self::$worktree_input = compact('repo', 'branch', 'from', 'inject_context', 'bootstrap', 'allow_stale', 'rebase_base', 'force', 'task');
return array(
'success' => true,
'handle' => $repo . '@' . str_replace('/', '-', $branch),
'path' => '/Users/chubes/Developer/' . $repo . '@' . str_replace('/', '-', $branch),
);
}
public function git_status( string $handle ): array
{
self::$git_status_input = compact('handle');
return array(
'success' => true,
'name' => $handle,
'path' => '/Users/chubes/Developer/' . $handle,
'dirty' => 1,
'files' => array( ' M README.md' ),
);
}
}
class WorkspaceReader
{
public static array $read_input = array();
public static array $list_input = array();
public static array $grep_input = array();
public function __construct( private Workspace $workspace )
{
}
public function read_file( string $name, string $path, int $max_size, ?int $offset = null, ?int $limit = null ): array
{
self::$read_input = compact('name', 'path', 'max_size', 'offset', 'limit');
return array(
'success' => true,
'content' => 'local file',
'path' => $path,
'size' => 10,
);
}
public function list_directory( string $name, ?string $path = null ): array
{
self::$list_input = compact('name', 'path');
return array(
'success' => true,
'repo' => $name,
'path' => $path ?? '/',
'entries' => array(
array(
'name' => 'README.md',
'type' => 'file',
'size' => 10,
),
),
);
}
public function grep( string $name, string $pattern, ?string $path = null, ?string $include_pattern = null, int $max_results = 100, int $context_lines = 0 ): array
{
self::$grep_input = compact('name', 'pattern', 'path', 'include_pattern', 'max_results', 'context_lines');
return array(
'success' => true,
'repo' => $name,
'path' => $path ?? '/',
'pattern' => $pattern,
'matches' => array(
array(
'file' => 'README.md',
'line' => 1,
'text' => 'local file',
),
),
'count' => 1,
);
}
}
}
namespace DataMachineCode\Support {
class RuntimeCapabilities
{
}
class GitRunner
{
}
}
namespace {
if ( ! defined('ABSPATH') ) {
define('ABSPATH', __DIR__);
}
class WP_Error
{
public function __construct( private string $code, private string $message, private array $data = array() )
{
}
public function get_error_code(): string
{
return $this->code;
}
public function get_error_message(): string
{
return $this->message;
}
public function get_error_data(): array
{
return $this->data;
}
}
function is_wp_error( $value ): bool
{
return $value instanceof WP_Error;
}
require __DIR__ . '/../inc/Abilities/WorkspaceAbilities.php';
$failures = array();
$assert = function ( string $label, bool $condition ) use ( &$failures ): void {
if ( $condition ) {
echo " ok {$label}\n";
return;
}
$failures[] = $label;
echo " fail {$label}\n";
};
echo "Workspace local fallback - smoke\n";
$show = \DataMachineCode\Abilities\WorkspaceAbilities::showRepo(
array(
'name' => 'wpcom-codebox',
)
);
$assert('remote show is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_input);
$assert('local show attempted first', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$show_input['handle'] ?? '' ));
$assert('show returns local listed primary', is_array($show) && '/Users/chubes/Developer/wpcom-codebox' === ( $show['path'] ?? '' ));
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_returns_remote_success = true;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_input = array();
\DataMachineCode\Workspace\Workspace::$show_input = array();
$duplicate_show = \DataMachineCode\Abilities\WorkspaceAbilities::showRepo(
array(
'name' => 'wpcom-codebox',
)
);
$assert('duplicate remote show is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_input);
$assert('duplicate local primary wins show path', is_array($duplicate_show) && '/Users/chubes/Developer/wpcom-codebox' === ( $duplicate_show['path'] ?? '' ));
$duplicate_path = \DataMachineCode\Abilities\WorkspaceAbilities::getPath(
array(
'name' => 'wpcom-codebox@fix-listed-primary-resolution',
)
);
$assert('workspace path returns local worktree when duplicate remote exists', is_array($duplicate_path) && '/Users/chubes/Developer/wpcom-codebox@fix-listed-primary-resolution' === ( $duplicate_path['path'] ?? '' ));
$assert('workspace path marks local worktree handle as existing', is_array($duplicate_path) && true === ( $duplicate_path['exists'] ?? null ));
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$read_returns_remote_success = true;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$read_input = array();
\DataMachineCode\Workspace\WorkspaceReader::$read_input = array();
$read = \DataMachineCode\Abilities\WorkspaceAbilities::readFile(
array(
'repo' => 'wpcom-codebox',
'path' => 'README.md',
'max_size' => 500,
'offset' => 2,
'limit' => 3,
)
);
$assert('remote read is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$read_input);
$assert('local read attempted for local primary', 'wpcom-codebox' === ( \DataMachineCode\Workspace\WorkspaceReader::$read_input['name'] ?? '' ));
$assert('read preserves options locally', 500 === ( \DataMachineCode\Workspace\WorkspaceReader::$read_input['max_size'] ?? null ) && 2 === ( \DataMachineCode\Workspace\WorkspaceReader::$read_input['offset'] ?? null ) && 3 === ( \DataMachineCode\Workspace\WorkspaceReader::$read_input['limit'] ?? null ));
$assert('read returns local file result', is_array($read) && 'local file' === ( $read['content'] ?? '' ));
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$list_returns_remote_success = true;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$list_input = array();
\DataMachineCode\Workspace\WorkspaceReader::$list_input = array();
$list = \DataMachineCode\Abilities\WorkspaceAbilities::listDirectory(
array(
'repo' => 'wpcom-codebox',
'path' => 'inc',
)
);
$assert('remote list is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$list_input);
$assert('local list attempted for local primary', 'wpcom-codebox' === ( \DataMachineCode\Workspace\WorkspaceReader::$list_input['name'] ?? '' ));
$assert('list preserves path locally', 'inc' === ( \DataMachineCode\Workspace\WorkspaceReader::$list_input['path'] ?? '' ));
$assert('list returns local entries', is_array($list) && 'README.md' === ( $list['entries'][0]['name'] ?? '' ));
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$grep_returns_remote_success = true;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$grep_input = array();
\DataMachineCode\Workspace\WorkspaceReader::$grep_input = array();
$grep = \DataMachineCode\Abilities\WorkspaceAbilities::grepFiles(
array(
'repo' => 'wpcom-codebox',
'pattern' => 'local',
'path' => 'inc',
'include' => '*.php',
'max_results' => 7,
'context_lines' => 2,
)
);
$assert('remote grep is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$grep_input);
$assert('local grep attempted for local primary', 'wpcom-codebox' === ( \DataMachineCode\Workspace\WorkspaceReader::$grep_input['name'] ?? '' ));
$assert('grep preserves options locally', '*.php' === ( \DataMachineCode\Workspace\WorkspaceReader::$grep_input['include_pattern'] ?? '' ) && 7 === ( \DataMachineCode\Workspace\WorkspaceReader::$grep_input['max_results'] ?? null ) && 2 === ( \DataMachineCode\Workspace\WorkspaceReader::$grep_input['context_lines'] ?? null ));
$assert('grep returns local matches', is_array($grep) && 1 === ( $grep['count'] ?? null ));
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_returns_remote_success = true;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_input = array();
\DataMachineCode\Workspace\Workspace::$git_status_input = array();
$git_status = \DataMachineCode\Abilities\WorkspaceAbilities::gitStatus(
array(
'name' => 'wpcom-codebox',
)
);
$assert('remote git status is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_input);
$assert('local git status attempted for local primary', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$git_status_input['handle'] ?? '' ));
$assert('git status returns local dirty state', is_array($git_status) && 1 === ( $git_status['dirty'] ?? null ));
\DataMachineCode\Workspace\Workspace::$show_returns_local_success = false;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_returns_remote_success = false;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_input = array();
\DataMachineCode\Workspace\Workspace::$git_status_input = array();
$miss_status = \DataMachineCode\Abilities\WorkspaceAbilities::gitStatus(
array(
'name' => 'wpcom-codebox',
)
);
$assert('remote git status miss is consulted before fallback', 'wpcom-codebox' === ( \DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_input['handle'] ?? '' ));
$assert('remote git status miss falls back locally', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$git_status_input['handle'] ?? '' ));
$assert('git status fallback returns local result', is_array($miss_status) && '/Users/chubes/Developer/wpcom-codebox' === ( $miss_status['path'] ?? '' ));
\DataMachineCode\Workspace\Workspace::$show_returns_local_success = true;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_returns_remote_success = true;
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_input = array();
$worktree = \DataMachineCode\Abilities\WorkspaceAbilities::worktreeAdd(
array(
'repo' => 'wpcom-codebox',
'branch' => 'fix/listed-primary-resolution',
'from' => 'origin/main',
'inject_context' => false,
'bootstrap' => false,
'allow_stale' => true,
'rebase_base' => true,
'force' => true,
'task_url' => 'https://github.com/Extra-Chill/data-machine-code/issues/635',
'task_ref' => 'Extra-Chill/data-machine-code#635',
)
);
$assert('remote github worktree row is ignored when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_input);
$assert('local worktree add fallback attempted', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$worktree_input['repo'] ?? '' ));
$assert('worktree add preserves base ref', 'origin/main' === ( \DataMachineCode\Workspace\Workspace::$worktree_input['from'] ?? '' ));
$assert('worktree add preserves local options', false === ( \DataMachineCode\Workspace\Workspace::$worktree_input['inject_context'] ?? null ) && true === ( \DataMachineCode\Workspace\Workspace::$worktree_input['allow_stale'] ?? null ));
$assert('worktree add preserves task metadata', 'Extra-Chill/data-machine-code#635' === ( \DataMachineCode\Workspace\Workspace::$worktree_input['task']['task_ref'] ?? '' ));
$assert('worktree add returns local worktree result', is_array($worktree) && 'wpcom-codebox@fix-listed-primary-resolution' === ( $worktree['handle'] ?? '' ));
$assert('worktree add returns editable local path', is_array($worktree) && '/Users/chubes/Developer/wpcom-codebox@fix-listed-primary-resolution' === ( $worktree['path'] ?? '' ));
if ( $failures ) {
echo "\nFailures:\n";
foreach ( $failures as $failure ) {
echo " - {$failure}\n";
}
exit(1);
}
echo "\nOK\n";
}