-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmoke-mounted-sandbox-bootstrap.php
More file actions
127 lines (107 loc) · 3.91 KB
/
Copy pathsmoke-mounted-sandbox-bootstrap.php
File metadata and controls
127 lines (107 loc) · 3.91 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
<?php
/**
* Smoke test for mounted sandbox self-configuration.
*
* Run: php tests/smoke-mounted-sandbox-bootstrap.php
*/
declare( strict_types=1 );
namespace DataMachineCode\Abilities {
class WorkspaceAbilities {
public static array $adopted = array();
public static function adoptRepo( array $input ): array {
self::$adopted[] = $input;
return array( 'success' => true ) + $input;
}
}
}
namespace {
if ( ! defined('ABSPATH') ) {
define('ABSPATH', __DIR__ . '/');
}
$GLOBALS['dmc_mounted_sandbox_actions'] = array();
$GLOBALS['dmc_mounted_sandbox_filters'] = array();
function add_action( string $tag, callable $callback, int $priority = 10 ): void {
$GLOBALS['dmc_mounted_sandbox_actions'][ $tag ][ $priority ][] = $callback;
}
function do_action( string $tag, ...$args ): void {
if ( empty($GLOBALS['dmc_mounted_sandbox_actions'][ $tag ]) ) {
return;
}
ksort($GLOBALS['dmc_mounted_sandbox_actions'][ $tag ]);
foreach ( $GLOBALS['dmc_mounted_sandbox_actions'][ $tag ] as $callbacks ) {
foreach ( $callbacks as $callback ) {
$callback(...$args);
}
}
}
function add_filter( string $tag, callable|string $callback, int $priority = 10 ): void {
$GLOBALS['dmc_mounted_sandbox_filters'][ $tag ][ $priority ][] = $callback;
}
function apply_filters( string $tag, $value ) {
if ( empty($GLOBALS['dmc_mounted_sandbox_filters'][ $tag ]) ) {
return $value;
}
ksort($GLOBALS['dmc_mounted_sandbox_filters'][ $tag ]);
foreach ( $GLOBALS['dmc_mounted_sandbox_filters'][ $tag ] as $callbacks ) {
foreach ( $callbacks as $callback ) {
$value = is_string($callback) && '__return_false' === $callback ? false : $callback($value);
}
}
return $value;
}
function wp_mkdir_p( string $path ): bool {
return is_dir($path) || mkdir($path, 0775, true);
}
require __DIR__ . '/../inc/Runtime/MountedSandboxBootstrap.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 "Mounted sandbox bootstrap - smoke\n";
$root = sys_get_temp_dir() . '/dmc-mounted-sandbox-' . bin2hex(random_bytes(4));
$repo_backed = $root . '/repo-backed';
$local_repo = $root . '/local-repo';
mkdir($repo_backed . '/.git', 0775, true);
mkdir($local_repo . '/.git', 0775, true);
register_shutdown_function(
static function () use ( $root ): void {
if ( is_dir($root) ) {
exec('rm -rf ' . escapeshellarg($root));
}
}
);
$GLOBALS['mounted_runtime_context'] = array(
'workspace_root' => $root,
'sandbox_workspace' => array(
'root' => $root,
'mounts' => array(
array( 'target' => $repo_backed, 'sourceMode' => 'repo-backed' ),
array( 'target' => $local_repo, 'sourceMode' => 'local' ),
),
),
);
\DataMachineCode\Runtime\MountedSandboxBootstrap::register();
$assert('defines workspace path from generic sandbox context', defined('DATAMACHINE_WORKSPACE_PATH') && DATAMACHINE_WORKSPACE_PATH === $root);
$assert('forces mounted local workspace backend', false === apply_filters('datamachine_code_remote_workspace_backend_should_handle', true));
$registered_actions = array_keys($GLOBALS['dmc_mounted_sandbox_actions']);
$assert('registers only neutral mounted-runtime actions', $registered_actions === array( 'mounted_runtime_bootstrap', 'wordpress_runtime_bootstrap', 'wp_abilities_api_init' ));
do_action('wp_abilities_api_init');
$adopted = \DataMachineCode\Abilities\WorkspaceAbilities::$adopted;
$assert('adopts exactly one non repo-backed workspace mount', 1 === count($adopted));
$assert('adopts the local workspace mount', isset($adopted[0]['path']) && $adopted[0]['path'] === $local_repo);
if ( $failures ) {
echo "\nFailures:\n";
foreach ( $failures as $failure ) {
echo " - {$failure}\n";
}
exit(1);
}
echo "\nOK ({$passes} assertions)\n";
}