-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathreport.php
More file actions
141 lines (122 loc) · 5.52 KB
/
Copy pathreport.php
File metadata and controls
141 lines (122 loc) · 5.52 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
<?php
/**
* WordPress PHPUnity Test Runner: Report script
*
* This script is responsible for collecting details related to the testing
* environment and the outcome of the test suite before reporting the data
* through the configured Reporter API.
*
* The information gathered includes:
* - SVN revision,
* - Environment details.
* - Test outcomes.
*
* @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( false );
/**
* Ensure that optional environment variables are present with default values.
*/
$runner_vars = setup_runner_env_vars();
/*
* Retrieve the SVN revision number from the repository's Git log.
*
* The SVN revision number is extracted from the retrieves the latest commit
* message using a combination of grep and cut commands.
*/
log_message( 'Getting SVN Revision' );
$rev = exec( 'git --git-dir=' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '/.git log -1 --pretty=%B | grep "git-svn-id:" | cut -d " " -f 2 | cut -d "@" -f 2' );
/*
* Retrieve the SVN commit message from the repository's Git log.
*
* The `git log` command is used to fetch the commit message being tested.
*/
log_message( 'Getting SVN message' );
$message = trim( exec( 'git --git-dir=' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] ) . '/.git log -1 --pretty=%B | head -1' ) );
/**
* Prepares the file path for copying the junit.xml results.
* Logs a message indicating the start of the operation to copy junit.xml results.
* Constructs the file path to the junit.xml file(s) located in the test directory,
* making use of the WPT_TEST_DIR environment variable. The path is sanitized to be
* safely used in shell commands.
*/
log_message( 'Copying junit.xml results' );
$junit_location = escapeshellarg( $runner_vars['WPT_TEST_DIR'] ) . '/tests/phpunit/build/logs/*';
/**
* Modifies the junit.xml results file path for a remote location if an SSH connection is available.
* If the WPT_SSH_CONNECT environment variable is not empty, indicating that an SSH connection
* is configured, this snippet adapts the junit_location variable to include the necessary SSH
* command and options for accessing the remote file system. It concatenates SSH options with the
* remote path to ensure that the junit.xml results can be accessed or copied over SSH.
*/
if ( ! empty( $runner_vars['WPT_SSH_CONNECT'] ) ) {
$junit_location = '-e "ssh ' . $runner_vars['WPT_SSH_OPTIONS'] . '" ' . escapeshellarg( $runner_vars['WPT_SSH_CONNECT'] . ':' . $junit_location );
}
// Construct the rsync command for synchronizing the junit.xml file.
$rsync_options = '-r';
if ( $runner_vars['WPT_DEBUG'] ) {
$rsync_options = $rsync_options . 'v';
}
// Copy the junit.xml file from the test directory to the prepare directory.
$junit_exec = 'rsync ' . $rsync_options . ' ' . $junit_location . ' ' . escapeshellarg( $runner_vars['WPT_PREPARE_DIR'] );
perform_operations(
array(
$junit_exec,
)
);
// Process the junit.xml file.
log_message( 'Processing and uploading junit.xml' );
$xml = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/junit.xml' );
$results = process_junit_xml( $xml );
/*
* Retrieves environment details from a JSON file or generates them if not available.
* Initializes the environment details string. If an 'env.json' file exists in the prepared
* directory, its contents are read into the environment details string. If the file doesn't
* exist but the prepared directory is the same as the test directory, the environment details
* are generated by calling a function that retrieves these details, then encoded into JSON format.
*/
$env = '';
if ( file_exists( $runner_vars['WPT_PREPARE_DIR'] . '/env.json' ) ) {
$env = file_get_contents( $runner_vars['WPT_PREPARE_DIR'] . '/env.json' );
} elseif ( $runner_vars['WPT_PREPARE_DIR'] === $runner_vars['WPT_TEST_DIR'] ) {
$env = json_encode( get_env_details(), JSON_PRETTY_PRINT );
}
/*
* Submit the test results to the configured Test Reporter API.
*
* When an API key is provided, the results will be submitted to the configured
* Test Reporter API.
*
* Otherwise, the test results will be logged locally.
*/
if ( ! empty( $runner_vars['WPT_REPORT_API_KEY'] ) ) {
// Upload the results and capture the HTTP status and response body
list( $http_status, $response_body ) = upload_results( $results, $rev, $message, $env, $runner_vars['WPT_REPORT_API_KEY'] );
// Decode the JSON response body
$response = json_decode( $response_body, true );
if ( 20 == substr( $http_status, 0, 2 ) ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
// Construct and log a success message with a link if provided in the response
$message = 'Results successfully uploaded';
$message .= isset( $response['link'] ) ? ': ' . $response['link'] : '';
log_message( $message );
} else {
// Construct and log an error message with additional details if provided in the response
$message = 'Error uploading results';
$message .= isset( $response['message'] ) ? ': ' . $response['message'] : '';
$message .= ' (HTTP status ' . (int) $http_status . ')';
error_message( $message );
}
} else {
// Log the test results and environment details locally if no API key is provided
log_message( '[+] TEST RESULTS' . "\n\n" . $results . "\n\n" );
log_message( '[+] ENVIRONMENT' . "\n\n" . $env . "\n\n" );
}