Skip to content

Commit 3443ece

Browse files
author
Keyur Govande
committed
Split test for Windows and Unix.
Also fix test for Windows 8: It no longer verifies the title using PowerShell. It only executes the API calls. On Windows 7 and older though, we still continue to verify the title.
1 parent ea88bda commit 3443ece

File tree

3 files changed

+136
-63
lines changed

3 files changed

+136
-63
lines changed

sapi/cli/tests/cli_process_title.phpt

-63
This file was deleted.
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
Check cli_process_title support on Unix
3+
--SKIPIF--
4+
<?php
5+
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
6+
die("skip");
7+
?>
8+
--FILE--
9+
<?php
10+
echo "*** Testing setting the process title ***\n";
11+
12+
$set_title = $original_title = uniqid("title", true);
13+
$pid = getmypid();
14+
15+
if (cli_set_process_title($original_title) === true)
16+
echo "Successfully set title\n";
17+
18+
$ps_output = shell_exec("ps -p $pid -o command | tail -n 1");
19+
20+
if ($ps_output === null)
21+
{
22+
echo "ps failed\n";
23+
die();
24+
}
25+
26+
$loaded_title = trim($ps_output);
27+
if (strpos(strtoupper(substr(PHP_OS, 0, 13)), "BSD") !== false)
28+
{
29+
// Fix up title for BSD
30+
$set_title = "php: $original_title (php)";
31+
}
32+
33+
if ($loaded_title == $set_title)
34+
echo "Successfully verified title using ps\n";
35+
else
36+
echo "Actually loaded from ps: $loaded_title\n";
37+
38+
$read_title = cli_get_process_title();
39+
if ($read_title == $original_title)
40+
echo "Successfully verified title using get\n";
41+
else
42+
echo "Actually loaded from get: $read_title\n";
43+
44+
?>
45+
--EXPECTF--
46+
*** Testing setting the process title ***
47+
Successfully set title
48+
Successfully verified title using ps
49+
Successfully verified title using get
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
--TEST--
2+
Check cli_process_title support in Windows
3+
--SKIPIF--
4+
<?php
5+
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
6+
die("skip");
7+
?>
8+
--FILE--
9+
<?php
10+
11+
// On Windows 8, this test does not work the same way. When the PowerShell command
12+
// "get-process" is executed using shell_exec, it overwrites the ConsoleTitle with
13+
// "Windows PowerShell" and this title ONLY clears away when the php.exe
14+
// process exits i.e. the test finishes.
15+
// On older versions like Windows 7 though, running the command appends
16+
// "Windows PowerShell" to the ConsoleTitle temporarily and the title reverts
17+
// back to the original once shell_exec is done.
18+
// Hence on Windows 8, we don't verify that the title is actually set by
19+
// cli_set_process_title(). We're only making the API calls to ensure there are
20+
// no warnings/errors.
21+
22+
$is_windows8 = false;
23+
$ps_output = shell_exec("PowerShell \"(Get-Host).UI.RawUI.WindowTitle\"");
24+
if ($ps_output === null)
25+
{
26+
echo "Get-Host failed\n";
27+
die();
28+
}
29+
30+
$ps_output = trim($ps_output);
31+
if ($ps_output == "Windows PowerShell")
32+
$is_windows8 = true;
33+
34+
echo "*** Testing setting the process title ***\n";
35+
36+
$original_title = uniqid("title", true);
37+
$pid = getmypid();
38+
39+
if (cli_set_process_title($original_title) === true)
40+
echo "Successfully set title\n";
41+
42+
if ($is_windows8)
43+
{
44+
$loaded_title = $original_title;
45+
}
46+
else
47+
{
48+
$loaded_title = shell_exec("PowerShell \"get-process cmd*,powershell* | Select-Object mainWindowTitle | ft -hide\"");
49+
50+
if ($loaded_title === null)
51+
{
52+
echo "Reading title using get-process failed\n";
53+
die();
54+
}
55+
56+
// Kind of convoluted. So when the test is run on Windows 7 or older, the console where
57+
// the run-tests.php is executed forks a php.exe, which forks a cmd.exe, which then forks
58+
// a final php.exe to run the actual test. But the console title is set for the original console.
59+
// I couldn't figure out a good way to navigate this, so we're "grep'ing" all possible
60+
// console windows for our very unique title. It should occur exactly once in the grep
61+
// output.
62+
$pos = strpos($loaded_title, $original_title, 0);
63+
if ($pos !== false)
64+
{
65+
$pos = strpos($loaded_title, $original_title, $pos + strlen($original_title));
66+
if ($pos === false)
67+
$loaded_title = $original_title;
68+
}
69+
}
70+
71+
if ($loaded_title == $original_title)
72+
echo "Successfully verified title using get-process\n";
73+
else
74+
echo "Actually loaded from get-process: $loaded_title\n";
75+
76+
$read_title = cli_get_process_title();
77+
if ($read_title == $original_title)
78+
echo "Successfully verified title using get\n";
79+
else
80+
echo "Actually loaded from get: $read_title\n";
81+
82+
?>
83+
--EXPECTF--
84+
*** Testing setting the process title ***
85+
Successfully set title
86+
Successfully verified title using get-process
87+
Successfully verified title using get

0 commit comments

Comments
 (0)