-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmoke-remote-workspace-backend-filter.php
More file actions
97 lines (77 loc) · 2.6 KB
/
Copy pathsmoke-remote-workspace-backend-filter.php
File metadata and controls
97 lines (77 loc) · 2.6 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
<?php
/**
* Smoke test for remote workspace backend override filter.
*
* Run: php tests/smoke-remote-workspace-backend-filter.php
*/
declare( strict_types=1 );
namespace DataMachineCode\Support {
class GitRunner
{
public static bool $available = true;
public static function diagnose(): array
{
return array(
'git_available' => self::$available,
'proc_open_available' => self::$available,
);
}
public static function is_available(): bool
{
return self::$available;
}
}
}
namespace {
if ( ! defined('ABSPATH') ) {
define('ABSPATH', __DIR__ . '/');
}
$GLOBALS['dmc_remote_backend_filters'] = array();
function add_filter( string $tag, callable $callback, int $priority = 10 ): void
{
$GLOBALS['dmc_remote_backend_filters'][ $tag ][ $priority ][] = $callback;
}
function apply_filters( string $tag, $value )
{
if ( empty($GLOBALS['dmc_remote_backend_filters'][ $tag ]) ) {
return $value;
}
ksort($GLOBALS['dmc_remote_backend_filters'][ $tag ]);
foreach ( $GLOBALS['dmc_remote_backend_filters'][ $tag ] as $callbacks ) {
foreach ( $callbacks as $callback ) {
$value = $callback($value);
}
}
return $value;
}
require __DIR__ . '/../inc/Workspace/RemoteWorkspaceBackend.php';
$failures = array();
$passes = 0;
$assert = function ( string $label, bool $condition ) use ( &$failures, &$passes ): void {
if ( $condition ) {
++$passes;
echo " ok {$label}\n";
return;
}
$failures[] = $label;
echo " fail {$label}\n";
};
echo "Remote workspace backend filter - smoke\n";
\DataMachineCode\Support\GitRunner::$available = true;
$assert('defaults to local git when git is available', false === \DataMachineCode\Workspace\RemoteWorkspaceBackend::should_handle());
add_filter('datamachine_code_remote_workspace_backend_should_handle', static fn(): bool => true, 100);
$assert('filter can force remote backend on', true === \DataMachineCode\Workspace\RemoteWorkspaceBackend::should_handle());
$GLOBALS['dmc_remote_backend_filters'] = array();
\DataMachineCode\Support\GitRunner::$available = false;
$assert('defaults to remote backend when git is unavailable', true === \DataMachineCode\Workspace\RemoteWorkspaceBackend::should_handle());
add_filter('datamachine_code_remote_workspace_backend_should_handle', static fn(): bool => false, 100);
$assert('filter can force mounted local backend on', false === \DataMachineCode\Workspace\RemoteWorkspaceBackend::should_handle());
if ( $failures ) {
echo "\nFailures:\n";
foreach ( $failures as $failure ) {
echo " - {$failure}\n";
}
exit(1);
}
echo "\nOK ({$passes} assertions)\n";
}