From a7f202de4e0e7cf2018d3d80ebf79922d74e3a9d Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Tue, 16 Aug 2016 14:57:02 +0800 Subject: [PATCH 1/2] MDL-55379 phpunit: Fixed hint_result for rerun --- lib/phpunit/classes/hint_resultprinter.php | 27 +++++++++++----------- lib/testing/lib.php | 8 +++++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/phpunit/classes/hint_resultprinter.php b/lib/phpunit/classes/hint_resultprinter.php index 51d941621e8..0a92712e9d1 100644 --- a/lib/phpunit/classes/hint_resultprinter.php +++ b/lib/phpunit/classes/hint_resultprinter.php @@ -88,32 +88,33 @@ class Hint_ResultPrinter extends PHPUnit_TextUI_ResultPrinter { $file = substr($file, strlen($cwd)+1); } - $executable = null; + $pathprefix = testing_cli_argument_path('/'); + if ($pathprefix) { + $pathprefix .= DIRECTORY_SEPARATOR; + } + // There is only vendor/bin/phpunit executable. There is no .cmd or .bat files. + $executable = $pathprefix . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'phpunit'; + + if (testing_is_cygwin()) { + $file = str_replace('\\', '/', $file); + $executable = str_replace('\\', '/', $executable); + } + + // Add server arguments to the rerun if passed. if (isset($_SERVER['argv'][0])) { if (preg_match('/phpunit(\.bat|\.cmd)?$/', $_SERVER['argv'][0])) { - $executable = $_SERVER['argv'][0]; for($i=1;$iwrite("\nTo re-run:\n $executable $testName $file\n"); } } diff --git a/lib/testing/lib.php b/lib/testing/lib.php index 999a5008bc9..cef731adf8c 100644 --- a/lib/testing/lib.php +++ b/lib/testing/lib.php @@ -52,13 +52,17 @@ function testing_cli_argument_path($moodlepath) { // This is the real CLI script, work with relative paths. $cwd = getcwd(); } + + // In sub path, we want to remove leading Directory separator. + $removeseparator = 0; if (substr($cwd, -1) !== DIRECTORY_SEPARATOR) { - $cwd .= DIRECTORY_SEPARATOR; + $removeseparator = 1; } + $path = realpath($CFG->dirroot.$moodlepath); if (strpos($path, $cwd) === 0) { - $path = substr($path, strlen($cwd)); + $path = substr($path, strlen($cwd) + $removeseparator); } if (testing_is_cygwin()) { From 1b319d3d3b0f1f76979120449aac001fef152113 Mon Sep 17 00:00:00 2001 From: Rajesh Taneja Date: Wed, 7 Sep 2016 13:16:09 +0800 Subject: [PATCH 2/2] MDL-55379 phpunit: Fix dir separator for cli commands --- admin/tool/behat/tests/manager_util_test.php | 7 +-- lib/behat/classes/behat_config_util.php | 10 ++-- lib/phpunit/classes/hint_resultprinter.php | 7 +-- lib/testing/lib.php | 49 ++++++++++++++++++-- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/admin/tool/behat/tests/manager_util_test.php b/admin/tool/behat/tests/manager_util_test.php index 2f5980653fd..9556cee758c 100644 --- a/admin/tool/behat/tests/manager_util_test.php +++ b/admin/tool/behat/tests/manager_util_test.php @@ -365,12 +365,7 @@ class tool_behat_manager_util_testcase extends advanced_testcase { $behatconfigutil = $this->behatconfigutil; // Fix expected directory path for OS. - $cleanfeaturepath = str_replace('\\', DIRECTORY_SEPARATOR, $cleanfeaturepath); - $cleanfeaturepath = str_replace('/', DIRECTORY_SEPARATOR, $cleanfeaturepath); - - if (testing_is_cygwin()) { - $featurepath = str_replace('\\', '/', $cleanfeaturepath); - } + $cleanfeaturepath = testing_cli_fix_directory_separator($cleanfeaturepath); list($retkey, $retcleanfeaturepath) = $behatconfigutil->get_clean_feature_key_and_path($featurepath); diff --git a/lib/behat/classes/behat_config_util.php b/lib/behat/classes/behat_config_util.php index 1dcc33c9da8..bdb5337d5ef 100644 --- a/lib/behat/classes/behat_config_util.php +++ b/lib/behat/classes/behat_config_util.php @@ -223,17 +223,13 @@ class behat_config_util { global $CFG; // Fix directory path. - $featurepath = str_replace('\\', DIRECTORY_SEPARATOR, $featurepath); - $featurepath = str_replace('/', DIRECTORY_SEPARATOR, $featurepath); - - if (testing_is_cygwin()) { - $featurepath = str_replace('\\', '/', $featurepath); - } + $featurepath = testing_cli_fix_directory_separator($featurepath); + $dirroot = testing_cli_fix_directory_separator($CFG->dirroot . DIRECTORY_SEPARATOR); $key = basename($featurepath, '.feature'); // Get relative path. - $featuredirname = str_replace($CFG->dirroot . DIRECTORY_SEPARATOR , '', $featurepath); + $featuredirname = str_replace($dirroot , '', $featurepath); // Get 5 levels of feature path to ensure we have a unique key. for ($i = 0; $i < 5; $i++) { if (($featuredirname = dirname($featuredirname)) && $featuredirname !== '.') { diff --git a/lib/phpunit/classes/hint_resultprinter.php b/lib/phpunit/classes/hint_resultprinter.php index 0a92712e9d1..53f3bd812a5 100644 --- a/lib/phpunit/classes/hint_resultprinter.php +++ b/lib/phpunit/classes/hint_resultprinter.php @@ -86,6 +86,7 @@ class Hint_ResultPrinter extends PHPUnit_TextUI_ResultPrinter { $cwd = getcwd(); if (strpos($file, $cwd) === 0) { $file = substr($file, strlen($cwd)+1); + $file = testing_cli_fix_directory_separator($file); } $pathprefix = testing_cli_argument_path('/'); @@ -95,11 +96,7 @@ class Hint_ResultPrinter extends PHPUnit_TextUI_ResultPrinter { // There is only vendor/bin/phpunit executable. There is no .cmd or .bat files. $executable = $pathprefix . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'phpunit'; - - if (testing_is_cygwin()) { - $file = str_replace('\\', '/', $file); - $executable = str_replace('\\', '/', $executable); - } + $executable = testing_cli_fix_directory_separator($executable); // Add server arguments to the rerun if passed. if (isset($_SERVER['argv'][0])) { diff --git a/lib/testing/lib.php b/lib/testing/lib.php index cef731adf8c..b2134cddad3 100644 --- a/lib/testing/lib.php +++ b/lib/testing/lib.php @@ -65,9 +65,7 @@ function testing_cli_argument_path($moodlepath) { $path = substr($path, strlen($cwd) + $removeseparator); } - if (testing_is_cygwin()) { - $path = str_replace('\\', '/', $path); - } + $path = testing_cli_fix_directory_separator($path); return $path; } @@ -242,3 +240,48 @@ function testing_update_composer_dependencies() { // Return to our original location. chdir($cwd); } + +/** + * Fix DIRECTORY_SEPARATOR for windows. + * + * In PHP on Windows, DIRECTORY_SEPARATOR is set to the backslash (\) + * character. However, if you're running a Cygwin/Msys/Git shell + * exec() calls will return paths using the forward slash (/) character. + * + * NOTE: Because PHP on Windows will accept either forward or backslashes, + * paths should be built using ONLY forward slashes, regardless of + * OS. MOODLE_DIRECTORY_SEPARATOR should only be used when parsing + * paths returned by the shell. + * + * @param string $path + * @return string. + */ +function testing_cli_fix_directory_separator($path) { + global $CFG; + + static $dirseparator = null; + + if (!$dirseparator) { + // Default directory separator. + $dirseparator = DIRECTORY_SEPARATOR; + + // On windows we need to find what directory separator is used. + if ($CFG->ostype = 'WINDOWS') { + if (!empty($_SERVER['argv'][0])) { + if (false === strstr($_SERVER['argv'][0], '\\')) { + $dirseparator = '/'; + } else { + $dirseparator = '\\'; + } + } else if (testing_is_cygwin()) { + $dirseparator = '/'; + } + } + } + + // Normalize \ and / to directory separator. + $path = str_replace('\\', $dirseparator, $path); + $path = str_replace('/', $dirseparator, $path); + + return $path; +}