Skip to content

Commit

Permalink
Minor clean-up to core-init command. Also, add a --add-path option to…
Browse files Browse the repository at this point in the history
… core-init, mostly so that we can force the path writing in our tests and assert that it happened correctly.
  • Loading branch information
greg-1-anderson committed Nov 11, 2015
1 parent 1243ab4 commit 62b08fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 7 additions & 5 deletions commands/core/init.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
6 changes: 4 additions & 2 deletions tests/initCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
}
}

0 comments on commit 62b08fa

Please sign in to comment.