Skip to content

Commit dc0b005

Browse files
committed
use modified process class
1 parent 4e1559b commit dc0b005

File tree

2 files changed

+175
-7
lines changed

2 files changed

+175
-7
lines changed

src/Context/FeatureContext.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use RuntimeException;
2424
use WP_CLI;
2525
use DirectoryIterator;
26-
use WP_CLI\Process;
2726
use WP_CLI\ProcessRun;
2827
use WP_CLI\Utils;
2928
use WP_CLI\WpOrgApi;
@@ -675,11 +674,7 @@ public static function prepare( BeforeSuiteScope $scope ): void {
675674
self::$mysql_binary = Utils\get_mysql_binary_path();
676675
}
677676

678-
$command = 'wp cli info';
679-
if ( Utils\is_windows() ) {
680-
$command .= ' > NUL 2>&1';
681-
}
682-
$result = Process::create( $command, null, self::get_process_env_variables() )->run_check();
677+
$result = Process::create( 'wp cli info', null, self::get_process_env_variables() )->run_check();
683678
echo "{$result->stdout}\n";
684679

685680
// Remove install cache if any (not setting the static var).
@@ -1884,7 +1879,6 @@ function wpcli_bootstrap_behat_feature_context(): void {
18841879

18851880
// Load helper functionality that is needed for the tests.
18861881
require_once "{$framework_folder}/php/utils.php";
1887-
require_once "{$framework_folder}/php/WP_CLI/Process.php";
18881882
require_once "{$framework_folder}/php/WP_CLI/ProcessRun.php";
18891883

18901884
// Manually load Composer file includes by generating a config with require:

src/Context/Process.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace WP_CLI\Tests\Context;
4+
5+
use RuntimeException;
6+
7+
/**
8+
* Run a system process, and learn what happened.
9+
*/
10+
class Process {
11+
/**
12+
* @var string The full command to execute by the system.
13+
*/
14+
private $command;
15+
16+
/**
17+
* @var string|null The path of the working directory for the process or NULL if not specified (defaults to current working directory).
18+
*/
19+
private $cwd;
20+
21+
/**
22+
* @var array Environment variables to set when running the command.
23+
*/
24+
private $env;
25+
26+
/**
27+
* @var array Descriptor spec for `proc_open()`.
28+
*/
29+
private static $descriptors = [
30+
0 => STDIN,
31+
1 => [ 'pipe', 'w' ],
32+
2 => [ 'pipe', 'w' ],
33+
];
34+
35+
/**
36+
* @var bool Whether to log run time info or not.
37+
*/
38+
public static $log_run_times = false;
39+
40+
/**
41+
* @var array Array of process run time info, keyed by process command, each a 2-element array containing run time and run count.
42+
*/
43+
public static $run_times = [];
44+
45+
/**
46+
* @param string $command Command to execute.
47+
* @param string|null $cwd Directory to execute the command in.
48+
* @param array|null $env Environment variables to set when running the command.
49+
*
50+
* @return Process
51+
*/
52+
public static function create( $command, $cwd = null, $env = [] ) {
53+
$proc = new self();
54+
55+
$proc->command = $command;
56+
$proc->cwd = $cwd;
57+
$proc->env = $env;
58+
59+
return $proc;
60+
}
61+
62+
private function __construct() {}
63+
64+
/**
65+
* Run the command.
66+
*
67+
* @return \WP_CLI\ProcessRun
68+
*/
69+
public function run() {
70+
\WP_CLI\Utils\check_proc_available( 'Process::run' );
71+
72+
$start_time = microtime( true );
73+
74+
$pipes = [];
75+
if ( \WP_CLI\Utils\is_windows() ) {
76+
// On Windows, leaving pipes open can cause hangs.
77+
// Redirect output to files and close stdin.
78+
$stdout_file = tempnam( sys_get_temp_dir(), 'behat-stdout-' );
79+
$stderr_file = tempnam( sys_get_temp_dir(), 'behat-stderr-' );
80+
$descriptors = [
81+
0 => [ 'pipe', 'r' ],
82+
1 => [ 'file', $stdout_file, 'a' ],
83+
2 => [ 'file', $stderr_file, 'a' ],
84+
];
85+
$proc = \WP_CLI\Utils\proc_open_compat( $this->command, $descriptors, $pipes, $this->cwd, $this->env );
86+
fclose( $pipes[0] );
87+
$stdout = file_get_contents( $stdout_file );
88+
$stderr = file_get_contents( $stderr_file );
89+
unlink( $stdout_file );
90+
unlink( $stderr_file );
91+
} else {
92+
$proc = \WP_CLI\Utils\proc_open_compat( $this->command, self::$descriptors, $pipes, $this->cwd, $this->env );
93+
$stdout = stream_get_contents( $pipes[1] );
94+
fclose( $pipes[1] );
95+
$stderr = stream_get_contents( $pipes[2] );
96+
fclose( $pipes[2] );
97+
}
98+
99+
$return_code = proc_close( $proc );
100+
101+
$run_time = microtime( true ) - $start_time;
102+
103+
if ( self::$log_run_times ) {
104+
if ( ! isset( self::$run_times[ $this->command ] ) ) {
105+
self::$run_times[ $this->command ] = [ 0, 0 ];
106+
}
107+
self::$run_times[ $this->command ][0] += $run_time;
108+
++self::$run_times[ $this->command ][1];
109+
}
110+
111+
return new \WP_CLI\ProcessRun(
112+
[
113+
'stdout' => $stdout,
114+
'stderr' => $stderr,
115+
'return_code' => $return_code,
116+
'command' => $this->command,
117+
'cwd' => $this->cwd,
118+
'env' => $this->env,
119+
'run_time' => $run_time,
120+
]
121+
);
122+
}
123+
124+
/**
125+
* Run the command, but throw an Exception on error.
126+
*
127+
* @return \WP_CLI\ProcessRun
128+
*/
129+
public function run_check() {
130+
$r = $this->run();
131+
132+
if ( $r->return_code ) {
133+
throw new RuntimeException( $r );
134+
}
135+
136+
return $r;
137+
}
138+
139+
/**
140+
* Run the command, but throw an Exception on error.
141+
* Same as `run_check()` above, but checks the correct stderr.
142+
*
143+
* @return \WP_CLI\ProcessRun
144+
*/
145+
public function run_check_stderr() {
146+
$r = $this->run();
147+
148+
if ( $r->return_code ) {
149+
throw new RuntimeException( $r );
150+
}
151+
152+
if ( ! empty( $r->stderr ) ) {
153+
// If the only thing that STDERR caught was the Requests deprecated message, ignore it.
154+
// This is a temporary fix until we have a better solution for dealing with Requests
155+
// as a dependency shared between WP Core and WP-CLI.
156+
$stderr_lines = array_filter( explode( "\n", $r->stderr ) );
157+
if ( 1 === count( $stderr_lines ) ) {
158+
$stderr_line = $stderr_lines[0];
159+
if (
160+
false !== strpos(
161+
$stderr_line,
162+
'The PSR-0 `Requests_...` class names in the Request library are deprecated.'
163+
)
164+
) {
165+
return $r;
166+
}
167+
}
168+
169+
throw new RuntimeException( $r );
170+
}
171+
172+
return $r;
173+
}
174+
}

0 commit comments

Comments
 (0)