diff --git a/commands/core/init.drush.inc b/commands/core/init.drush.inc index fb4a728670..150cd578a1 100644 --- a/commands/core/init.drush.inc +++ b/commands/core/init.drush.inc @@ -20,6 +20,7 @@ function init_drush_command() { 'global-options' => array('editor', 'bg'), 'options' => array( 'edit' => 'Open the new config file in an editor.', + 'add-path' => "Always add Drush to the $PATH in the user's .bashrc file, even if it is already in the $PATH. Use --no-add-path to skip updating .bashrc with the Drush $PATH. Default is to update .bashrc only if Drush is not already in the $PATH.", ), 'examples' => array( 'drush core-init --edit' => 'Enrich Bash and open drush config file in editor.', @@ -69,11 +70,12 @@ function drush_init_core_init() { // If Drush is not in the $PATH, then figure out which // path to add so that Drush can be found globally. - if (!drush_which("drush")) { - $drush_path = drush_find_path_to_drush(); - $drush_path = preg_replace("%^$home/%", '$HOME/', $drush_path); + $add_path = drush_get_option('add-path', NULL); + if ((!drush_which("drush") || $add_path) && ($add_path !== FALSE)) { + $drush_path = drush_find_path_to_drush($home); + $drush_path = preg_replace("%^" . preg_quote($home) . "/%", '$HOME/', $drush_path); - $bashrc_additions["%$drush_path%"] = "# Path to Drush, added by 'drush init'\n\$PATH=\$PATH:$drush_path\n\n"; + $bashrc_additions["%$drush_path%"] = "# Path to Drush, added by 'drush init'\nexport \$PATH=\"\$PATH:$drush_path\"\n\n"; } // Modify the user's bashrc file, adding our customizations. @@ -126,5 +128,5 @@ function drush_find_path_to_drush($home) { } // Fallback is to use the directory that Drush is in. - return dirname(DRUSH_BASE_PATH); + return DRUSH_BASE_PATH; } diff --git a/tests/initCommandTest.php b/tests/initCommandTest.php index b1acafbbc6..1a4db4bf0f 100644 --- a/tests/initCommandTest.php +++ b/tests/initCommandTest.php @@ -12,7 +12,7 @@ class initCommandCase extends CommandUnishTestCase { function testInitCommand() { // Call `drush core-init` - $this->drush('core-init', array(), array('backend' => NULL)); + $this->drush('core-init', array(), array('backend' => NULL, 'add-path' => TRUE)); $parsed = $this->parse_backend_output($this->getOutput()); // First test to ensure that the command claimed to have made the expected progress $this->assertLogHasMessage($parsed['log'], "Copied example Drush configuration file", 'ok'); @@ -25,8 +25,10 @@ function testInitCommand() { $this->assertFileExists("$home/.drush/drush.bashrc"); $this->assertFileExists("$home/.bashrc"); - // Check to see if the .bashrc file sources our drush.bashrc file. + // Check to see if the .bashrc file sources our drush.bashrc file, + // and whether it adds the path to UNISH_DRUSH to the $PATH $bashrc_contents = file_get_contents("$home/.bashrc"); $this->assertContains('drush.bashrc', $bashrc_contents); + $this->assertContains(dirname(UNISH_DRUSH), $bashrc_contents); } }