-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathcleanup.php
More file actions
117 lines (96 loc) · 4.56 KB
/
cleanup.php
File metadata and controls
117 lines (96 loc) · 4.56 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
<?php
/**
* This script is responsible for cleaning up the test environment after a run of the WordPress PHPUnit Test Runner.
* It ensures that temporary directories and files created during the test process are properly deleted.
*
* @link https://github.com/wordpress/phpunit-test-runner/ Original source repository
* @package WordPress
*/
require __DIR__ . '/functions.php';
/**
* Check for the presence of required environment variables.
* This function should be defined in functions.php and should throw an
* exception or exit if any required variables are missing.
*/
check_required_env();
/**
* Retrieves environment variables and sets defaults for test preparation.
* These variables are used to configure SSH connections, file paths, and
* executable commands needed for setting up the test environment.
*/
$WPT_PREPARE_DIR = trim( getenv( 'WPT_PREPARE_DIR' ) );
$WPT_SSH_CONNECT = trim( getenv( 'WPT_SSH_CONNECT' ) );
$WPT_SSH_OPTIONS = trim( getenv( 'WPT_SSH_OPTIONS' ) ) ? : '-o StrictHostKeyChecking=no';
$WPT_TEST_DIR = trim( getenv( 'WPT_TEST_DIR' ) );
$WPT_PHP_EXECUTABLE_MULTI = trim( getenv( 'WPT_PHP_EXECUTABLE_MULTI' ) ) ? : '';
/**
*/
$WPT_PHP_EXECUTABLE_MULTI_ARRAY = array();
if ( '' !== $WPT_PHP_EXECUTABLE_MULTI ) {
$php_multi_versions = explode( ',', $WPT_PHP_EXECUTABLE_MULTI );
foreach( $php_multi_versions as $php_multi_version ) {
$php_multi_v = explode( '+', $php_multi_version );
if( isset( $php_multi_v[0] ) && $php_multi_v[0] && isset( $php_multi_v[1] ) && $php_multi_v[1] ) {
$WPT_PHP_EXECUTABLE_MULTI_ARRAY[] = array( 'version' => trim( $php_multi_v[0] ), 'bin' => trim( $php_multi_v[1] ) );
}
unset( $php_multi_version );
}
unset( $php_multi_versions );
}
if( count( $WPT_PHP_EXECUTABLE_MULTI_ARRAY ) ) {
foreach( $WPT_PHP_EXECUTABLE_MULTI_ARRAY as $php_multi ) {
$WPT_PREPARE_DIR_MULTI = $WPT_PREPARE_DIR . '-' . crc32( $php_multi['version'] );
$WPT_TEST_DIR_MULTI = $WPT_TEST_DIR . '-' . crc32( $php_multi['version'] );
$WPT_RM_TEST_DIR_CMD = trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ? : 'rm -r ' . $WPT_TEST_DIR_MULTI;
/**
* The directory path of the test preparation directory is assumed to be previously defined.
* For example: $WPT_PREPARE_DIR = '/path/to/your/preparation/dir';
* Clean up the preparation directory.
* Forcefully deletes only the .git directory and the node_modules cache.
* Afterward, the entire preparation directory is removed to ensure a clean state for the next test run.
*/
perform_operations( array(
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR_MULTI . '/.git' ),
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR_MULTI . '/node_modules/.cache' ),
'rm -r ' . escapeshellarg( $WPT_PREPARE_DIR_MULTI ),
) );
/**
* Cleans up the test directory on a remote server.
* This conditional block checks if an SSH connection string is provided and is not empty.
* If a connection string is present, it triggers a cleanup operation on the remote environment.
* The cleanup operation is executed by the `perform_operations` function which takes an array
* of shell commands as its input.
*/
if ( ! empty( $WPT_SSH_CONNECT ) ) {
perform_operations( array(
'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $WPT_RM_TEST_DIR_CMD ),
) );
}
}
} else {
$WPT_RM_TEST_DIR_CMD = trim( getenv( 'WPT_RM_TEST_DIR_CMD' ) ) ? : 'rm -r ' . $WPT_TEST_DIR;
/**
* The directory path of the test preparation directory is assumed to be previously defined.
* For example: $WPT_PREPARE_DIR = '/path/to/your/preparation/dir';
* Clean up the preparation directory.
* Forcefully deletes only the .git directory and the node_modules cache.
* Afterward, the entire preparation directory is removed to ensure a clean state for the next test run.
*/
perform_operations( array(
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/.git' ),
'rm -rf ' . escapeshellarg( $WPT_PREPARE_DIR . '/node_modules/.cache' ),
'rm -r ' . escapeshellarg( $WPT_PREPARE_DIR ),
) );
/**
* Cleans up the test directory on a remote server.
* This conditional block checks if an SSH connection string is provided and is not empty.
* If a connection string is present, it triggers a cleanup operation on the remote environment.
* The cleanup operation is executed by the `perform_operations` function which takes an array
* of shell commands as its input.
*/
if ( ! empty( $WPT_SSH_CONNECT ) ) {
perform_operations( array(
'ssh ' . $WPT_SSH_OPTIONS . ' ' . escapeshellarg( $WPT_SSH_CONNECT ) . ' ' . escapeshellarg( $WPT_RM_TEST_DIR_CMD ),
) );
}
}