6 * Tests for Shell aliases.
10 class shellAliasesCase extends CommandUnishTestCase {
13 * Write a config file that contains the shell-aliases array.
20 \$options['shell-aliases'] = array(
21 'glopts' => 'topic core-global-options',
22 'pull' => '!git pull',
23 'echosimple' => '!echo {{@target}}',
24 'echotest' => '!echo {{@target}} {{%root}} {{%mypath}}',
25 'compound-command' => '!cd {{%sandbox}} && echo second',
28 file_put_contents(UNISH_SANDBOX . '/drushrc.php', trim($contents));
29 if (!file_exists(UNISH_SANDBOX . '/b')) {
30 mkdir(UNISH_SANDBOX . '/b');
35 \$options['shell-aliases'] = array(
36 'also' => '!echo alternate config file included too',
39 file_put_contents(UNISH_SANDBOX . '/b/drushrc.php', trim($contents));
40 $aliases['myalias'] = array(
41 'root' => '/path/to/drupal',
42 'uri' => 'mysite.org',
44 'path-aliases' => array (
45 '%mypath' => '/srv/data/mypath',
46 '%sandbox' => UNISH_SANDBOX,
49 $contents = $this->unish_file_aliases($aliases);
50 file_put_contents(UNISH_SANDBOX . '/aliases.drushrc.php', $contents);
54 * Test shell aliases to Drush commands.
56 public function testShellAliasDrushLocal() {
58 'config' => UNISH_SANDBOX,
60 $this->drush('glopts', array(), $options);
61 $output = $this->getOutput();
62 $this->assertContains('These options are applicable to most drush commands.', $output, 'Successfully executed local shell alias to drush command');
66 * Test shell aliases to Bash commands. Assure we pass along extra arguments
69 public function testShellAliasBashLocal() {
71 'config' => UNISH_SANDBOX,
75 $this->drush('pull', array('origin'), $options, NULL, NULL, self::EXIT_SUCCESS, '2>&1');
76 $output = $this->getOutput();
77 $this->assertContains('Calling proc_open(git pull origin --rebase);', $output);
80 public function testShellAliasDrushRemote() {
82 'config' => UNISH_SANDBOX,
86 $this->drush('glopts', array(), $options, 'user@server/path/to/drupal#sitename');
87 // $expected might be different on non unix platforms. We shall see.
88 // n.b. --config is not included in calls to remote systems.
89 $bash = $this->escapeshellarg('drush --config=drush-sandbox --nocolor --uri=sitename --root=/path/to/drupal core-topic core-global-options 2>&1');
90 $expected = "Simulating backend invoke: ssh -t user@server $bash 2>&1";
91 $output = $this->getOutput();
92 // Remove any coverage arguments. The filename changes, so it's not possible
93 // to create a string for assertEquals, and the need for both shell escaping
94 // and regexp escaping different parts of the expected output for
95 // assertRegexp makes it easier just to remove the argument before checking
97 $output = preg_replace('{--drush-coverage=[^ ]+ }', '', $output);
98 $output = preg_replace('{--config=[^ ]+ +}', '--config=drush-sandbox ', $output);
99 $this->assertEquals($expected, $output, 'Expected remote shell alias to a drush command was built');
102 public function testShellAliasBashRemote() {
104 'config' => UNISH_SANDBOX,
109 $this->drush('pull', array('origin'), $options, 'user@server/path/to/drupal#sitename', NULL, self::EXIT_SUCCESS, '2>&1');
110 // $expected might be different on non unix platforms. We shall see.
111 $exec = self::escapeshellarg('cd /path/to/drupal && git pull origin --rebase');
112 $expected = "Calling proc_open(ssh user@server $exec);";
113 $output = $this->getOutput();
114 $this->assertEquals($expected, $output, 'Expected remote shell alias to a bash command was built');
118 * Test shell aliases with simple replacements -- no alias.
120 public function testShellAliasSimpleReplacement() {
122 'config' => UNISH_SANDBOX,
124 $this->drush('echosimple', array(), $options);
125 // Windows command shell prints quotes (but not always?). See http://drupal.org/node/1452944.
127 $output = $this->getOutput();
128 $this->assertEquals($expected, $output);
132 * Test shell aliases with complex replacements -- no alias.
134 public function testShellAliasReplacementNoAlias() {
136 'config' => UNISH_SANDBOX,
138 // echo test has replacements that are not satisfied, so this is expected to return an error.
139 $this->drush('echotest', array(), $options, NULL, NULL, self::EXIT_ERROR);
143 * Test shell aliases with replacements -- alias.
145 public function testShellAliasReplacementWithAlias() {
147 'config' => UNISH_SANDBOX,
148 'alias-path' => UNISH_SANDBOX,
150 $this->drush('echotest', array(), $options, '@myalias');
151 // Windows command shell prints quotes (not always?). See http://drupal.org/node/1452944.
152 $expected = '@myalias';
153 $expected .= ' /path/to/drupal /srv/data/mypath';
154 $output = $this->getOutput();
155 $this->assertEquals($expected, $output);
159 * Test shell aliases with replacements and compound commands.
161 public function testShellAliasCompoundCommands() {
163 'config' => UNISH_SANDBOX,
164 'alias-path' => UNISH_SANDBOX,
166 $this->drush('compound-command', array(), $options, '@myalias');
167 $expected = 'second';
168 $output = $this->getOutput();
169 $this->assertEquals($expected, $output);
174 * Test shell aliases with multiple config files.
176 public function testShellAliasMultipleConfigFiles() {
178 'config' => UNISH_SANDBOX . "/b" . PATH_SEPARATOR . UNISH_SANDBOX,
179 'alias-path' => UNISH_SANDBOX,
181 $this->drush('also', array(), $options);
182 $expected = "alternate config file included too";
183 $output = $this->getOutput();
184 $this->assertEquals($expected, $output);