diff --git a/commands/simpletest/simpletest.drush.inc b/commands/simpletest/simpletest.drush.inc index 807533f..3254b93 100644 --- a/commands/simpletest/simpletest.drush.inc +++ b/commands/simpletest/simpletest.drush.inc @@ -44,6 +44,11 @@ function simpletest_drush_command() { 'drupal dependencies' => array('simpletest'), 'core' => array('6','7'), ); + $items['test drush'] = array( + 'callback' => 'drush_test_drush', + 'description' => 'Run drush-specific tests', + 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, + ); return $items; } @@ -61,10 +66,9 @@ function drush_test_mail($recipients) { return drush_set_error('DRUSH_SIMPLETEST_RUNTESTS_SH', dt('You must copy or symlink run-tests.sh into your /scripts directory beneath Drupal root.')); } - $php = drush_find_php(); $extra = drush_get_option('extra'); $url = escapeshellarg(url('', array('absolute' => TRUE))); - $exec = $php . " $run_tests --php '" . $php . '\' --url ' . $url . " $extra"; + $exec = "$run_tests --php '" . DRUSH_COMMAND . '\' --url ' . $url . " $extra"; drush_shell_exec($exec); $output = implode("\n", drush_shell_exec_output()); $subject = 'Simpletest results - ' . drush_simpletest_format_results($output); @@ -87,3 +91,17 @@ function drush_simpletest_format_results($output) { return dt('Unknown.'); } } + +/** + * Simple drush self-test procedure + * + * This only tests self-execution for now. + * + * XXX: this needs to be adapted to a testing framework, see: + * + * http://drupal.org/node/483940 + */ +function drush_test_drush() { + drush_log(dt("Invoking %drush help in a subprocess", array('%drush' => DRUSH_COMMAND))); + drush_backend_invoke('help', array(), 'GET', FALSE); +} \ No newline at end of file diff --git a/drush b/drush index fa5b274..aeef37b 100755 --- a/drush +++ b/drush @@ -39,11 +39,11 @@ elif [ -f /Applications/xampp/xamppfiles/bin/php ]; then # XAMPP on OS X /Applications/xampp/xamppfiles/bin/php $SCRIPT_PATH "$@" else # We check for a command line (cli) version of php, and if found use that. - /usr/bin/env php-cli -v &> /dev/null + which php-cli >/dev/null 2>&1 if [ "$?" = 0 ] ; then - /usr/bin/env php-cli $SCRIPT_PATH "$@" + php-cli $SCRIPT_PATH "$@" else # Alternatively we run with straight php, which works on most other systems. - /usr/bin/env php $SCRIPT_PATH "$@" + php $SCRIPT_PATH "$@" fi fi diff --git a/drush.php b/drush.php index 7b2aacb..bf7d24e 100755 --- a/drush.php +++ b/drush.php @@ -1,3 +1,4 @@ +#!/usr/bin/env php ./drush.php + * _SERVER["_"] => /home/anarcat/dist/drush/./drush.php + * + * Calling php ./drush.php yields the following: + * + * _SERVER["argv"][0] => drush.php + * _SERVER["_"] => /usr/bin/php + * + * Calling env php drush.php yields the following: + * + * _SERVER["argv"][0] => drush.php + * _SERVER["_"] => /usr/bin/env + * + * The latter is a problem as there is no way to find the PHP + * executable then. We consider it's the job of the shell script + * wrapper to do that fiddling around and we will therefore assume + * that $argv[0] is a proper PHP binary. + * + * Calling sudo php drush.php yields the following: + * + * _SERVER["argv"][0] => drush.php + * _SERVER["SUDO_COMMAND"] => /usr/bin/php drush.php [arguments] + * + * Notice how _ is missing when running in sudo. + * + * Therefore, we assume that if $_ finishes with $argv[0], we can call + * it directly again. Otherwise, we assume that $_ is the php binary + * and $argv[0] is the path to drush.php. We will use __FILE__, + * however, since we do not want to rely on (potentially) relative + * paths. + * + * The DRUSH_COMMAND constant is initialised to the value of this + * function when environment.inc is loaded. + * + * @see DRUSH_COMMAND + */ +function drush_find_drush() { + if (!empty($_ENV['SUDO_COMMAND']) && empty($_SERVER['_'])) { + // 'SUDO_COMMAND' is an environment variable set by the sudo program. + // Extract only the PHP interpreter, not the rest of the command. + list($_SERVER['_'], ) = explode(' ', $_ENV['SUDO_COMMAND'], 2); + } + if (substr($_SERVER['_'], -strlen($_SERVER['argv'][0])) == $_SERVER['argv'][0]) { + // $GLOBALS['_'] and $GLOBALS['argv'][0] both finish with the same, + // which means we were called directly + $drush = realpath($_SERVER['_']); + } else { + // not called directly, we were called as an argument to PHP, which + // should be in $GLOBALS['_'] + $drush = $_SERVER['_'] . ' ' . realpath($_SERVER['argv'][0]); } - return $php; + return $drush; } /**