From cd3826875558cf9cf6faf0a797f9a468e43b44d8 Mon Sep 17 00:00:00 2001 From: David Rothstein Date: Tue, 4 Nov 2014 23:39:42 -0500 Subject: Issue #2310415 by cilefen, ednawig, TravisCarden: Fixed run-tests.sh does not handle the error when invalid test groups/classes are specified. --- scripts/run-tests.sh | 60 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 6 deletions(-) (limited to 'scripts/run-tests.sh') diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh index 189d7f2e8..9078168a1 100755 --- a/scripts/run-tests.sh +++ b/scripts/run-tests.sh @@ -419,9 +419,20 @@ function simpletest_script_get_test_list() { else { if ($args['class']) { // Check for valid class names. - foreach ($args['test_names'] as $class_name) { - if (in_array($class_name, $all_tests)) { - $test_list[] = $class_name; + $test_list = array(); + foreach ($args['test_names'] as $test_class) { + if (class_exists($test_class)) { + $test_list[] = $test_class; + } + else { + $groups = simpletest_test_get_all(); + $all_classes = array(); + foreach ($groups as $group) { + $all_classes = array_merge($all_classes, array_keys($group)); + } + simpletest_script_print_error('Test class not found: ' . $test_class); + simpletest_script_print_alternatives($test_class, $all_classes, 6); + exit(1); } } } @@ -444,9 +455,12 @@ function simpletest_script_get_test_list() { // Check for valid group names and get all valid classes in group. foreach ($args['test_names'] as $group_name) { if (isset($groups[$group_name])) { - foreach ($groups[$group_name] as $class_name => $info) { - $test_list[] = $class_name; - } + $test_list = array_merge($test_list, array_keys($groups[$group_name])); + } + else { + simpletest_script_print_error('Test group not found: ' . $group_name); + simpletest_script_print_alternatives($group_name, array_keys($groups)); + exit(1); } } } @@ -674,3 +688,37 @@ function simpletest_script_color_code($status) { } return 0; // Default formatting. } + +/** + * Prints alternative test names. + * + * Searches the provided array of string values for close matches based on the + * Levenshtein algorithm. + * + * @see http://php.net/manual/en/function.levenshtein.php + * + * @param string $string + * A string to test. + * @param array $array + * A list of strings to search. + * @param int $degree + * The matching strictness. Higher values return fewer matches. A value of + * 4 means that the function will return strings from $array if the candidate + * string in $array would be identical to $string by changing 1/4 or fewer of + * its characters. + */ +function simpletest_script_print_alternatives($string, $array, $degree = 4) { + $alternatives = array(); + foreach ($array as $item) { + $lev = levenshtein($string, $item); + if ($lev <= strlen($item) / $degree || FALSE !== strpos($string, $item)) { + $alternatives[] = $item; + } + } + if (!empty($alternatives)) { + simpletest_script_print(" Did you mean?\n", SIMPLETEST_SCRIPT_COLOR_FAIL); + foreach ($alternatives as $alternative) { + simpletest_script_print(" - $alternative\n", SIMPLETEST_SCRIPT_COLOR_FAIL); + } + } +} -- cgit v1.2.3