forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompleteTest.php
209 lines (195 loc) · 11.4 KB
/
completeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
namespace Unish;
/**
* @group base
*/
class completeCase extends CommandUnishTestCase {
/**
* Write a config file that contains our configuration file.
*/
static function setUpBeforeClass() {
parent::setUpBeforeClass();
$contents = "
<?php
\$options['shell-aliases'] = array(
'uninstall' => 'pm-uninstall',
);
";
file_put_contents(UNISH_SANDBOX . '/drushrc.php', trim($contents));
}
public function testComplete() {
// We copy our completetest commandfile into our path.
// We cannot use --include since complete deliberately avoids drush
// command dispatch.
copy(dirname(__FILE__) . '/completetest.drush.inc', getenv('HOME') . '/.drush/completetest.drush.inc');
$sites = $this->setUpDrupal(2);
$env = key($sites);
$root = $this->webroot();
// We copy the unit test command into (only) our dev site, so we have a
// detectable difference we can use to detect cache correctness between
// sites.
mkdir("$root/sites/$env/modules");
copy(dirname(__FILE__) . '/completetestsite.drush.inc', "$root/sites/$env/modules/completetestsite.drush.inc");
// Clear the cache, so it finds our test command.
$this->drush('php-eval', array('drush_cache_clear_all();'), array(), '@' . $env);
// Create a sample directory and file to test file/directory completion.
mkdir("astronaut");
mkdir("asteroid");
mkdir("asteroid/ceres");
mkdir("asteroid/chiron");
touch('astronaut/aldrin.tar.gz');
touch('astronaut/armstrong.tar.gz');
touch('astronaut/yuri gagarin.tar.gz');
touch('zodiac.tar.gz');
touch('zodiac.txt');
// Create directory for temporary debug logs.
mkdir(UNISH_SANDBOX . '/complete-debug');
// Test cache clearing for global cache, which should affect all
// environments. First clear the cache:
$this->drush('php-eval', array('drush_complete_cache_clear();'));
// Confirm we get cache rebuilds for runs both in and out of a site
// which is expected since these should resolve to separate cache IDs.
$this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra', FALSE);
$this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf', FALSE);
// Next, rerun and check results to confirm cache IDs are generated
// correctly on our fast bootstrap when returning the cached result.
$this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra');
$this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf');
// Test cache clearing for a completion type, which should be effective only
// for current environment - i.e. a specific site should not be effected.
$this->drush('php-eval', array('drush_complete_cache_clear("command-names");'));
$this->verifyComplete('@dev aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-zebra');
$this->verifyComplete('aaaaaaaard-', 'aaaaaaaard-ant', 'aaaaaaaard-wolf', FALSE);
// Test cache clearing for a command specific completion type, which should
// be effective only for current environment. Prime caches first.
$this->verifyComplete('@dev aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
$this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
$this->drush('php-eval', array('drush_complete_cache_clear("arguments", "aaaaaaaard");'));
// We cleared the global cache for this argument, not the site specific
// cache should still exist.
$this->verifyComplete('@dev aaaaaaaard a', 'aardvark', 'aardwolf');
$this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf', FALSE);
// Test overall context sensitivity - almost all of these are cache hits.
// No context (i.e. "drush <tab>"), should list aliases and commands.
$this->verifyComplete('""', '@dev', 'zzzzzzzzebra');
// Site alias alone.
$this->verifyComplete('@', '@dev', '@stage');
// Command alone.
$this->verifyComplete('aaaaaaaa', 'aaaaaaaard', 'aaaaaaaard-wolf');
// Command with single result.
$this->verifyComplete('aaaaaaaard-v', 'aaaaaaaard-vark', 'aaaaaaaard-vark');
// Command with no results should produce no output.
$this->verifyComplete('dont-name-a-command-like-this', '', '');
// Commands that start the same as another command (i.e. aaaaaaaard is a
// valid command, but we should still list aaaaaaaardwolf when completing on
// "aaaaaaaard").
$this->verifyComplete('@dev aaaaaaaard', 'aaaaaaaard', 'aaaaaaaard-zebra');
// Global option alone.
$this->verifyComplete('--n', '--no', '--notify-audio');
// Site alias + command.
$this->verifyComplete('@dev aaaaaaaa', 'aaaaaaaard', 'aaaaaaaard-zebra');
// Site alias + command, should allow no further site aliases or commands.
$this->verifyComplete('@dev aaaaaaaard-wolf @', '', '', FALSE);
$this->verifyComplete('@dev aaaaaaaard-wolf aaaaaaaa', '', '');
// Command + command option.
$this->verifyComplete('aaaaaaaard --', '--ears', '--nose');
// Site alias + command + command option.
$this->verifyComplete('@dev aaaaaaaard --', '--ears', '--nose');
// Command + all arguments
$this->verifyComplete('aaaaaaaard ""', 'aardvark', 'zebra');
// Command + argument.
$this->verifyComplete('aaaaaaaard a', 'aardvark', 'aardwolf');
// Site alias + command + regular argument.
// Note: this is checked implicitly by the argument cache testing above.
if ($this->is_windows()) {
$this->markTestSkipped('Complete tests not fully working nor needed on Windows.');
}
// Site alias + command + file/directory argument tests.
// Current directory substrings.
// NOTE: This command arg has not been used yet, so cache miss is expected.
$this->verifyComplete('archive-restore ""', 'asteroid/', 'zodiac.tar.gz', FALSE);
$this->verifyComplete('archive-restore a', 'asteroid/', 'astronaut/');
$this->verifyComplete('archive-restore ast', 'asteroid/', 'astronaut/');
$this->verifyComplete('archive-restore aste', 'asteroid/', 'asteroid/');
$this->verifyComplete('archive-restore asteroid', 'asteroid/', 'asteroid/');
$this->verifyComplete('archive-restore asteroid/', 'ceres', 'chiron');
$this->verifyComplete('archive-restore asteroid/ch', 'asteroid/chiron/', 'asteroid/chiron/');
$this->verifyComplete('archive-restore astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
$this->verifyComplete('archive-restore astronaut/y', 'astronaut/yuri\ gagarin.tar.gz', 'astronaut/yuri\ gagarin.tar.gz');
// Leading dot style current directory substrings.
$this->verifyComplete('archive-restore .', './asteroid/', './zodiac.tar.gz');
$this->verifyComplete('archive-restore ./', './asteroid/', './zodiac.tar.gz');
$this->verifyComplete('archive-restore ./a', './asteroid/', './astronaut/');
$this->verifyComplete('archive-restore ./ast', './asteroid/', './astronaut/');
$this->verifyComplete('archive-restore ./aste', './asteroid/', './asteroid/');
$this->verifyComplete('archive-restore ./asteroid', './asteroid/', './asteroid/');
$this->verifyComplete('archive-restore ./asteroid/', 'ceres', 'chiron');
$this->verifyComplete('archive-restore ./asteroid/ch', './asteroid/chiron/', './asteroid/chiron/');
$this->verifyComplete('archive-restore ./astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
$this->verifyComplete('archive-restore ./astronaut/y', './astronaut/yuri\ gagarin.tar.gz', './astronaut/yuri\ gagarin.tar.gz');
// Absolute path substrings.
$path = getcwd();
$this->verifyComplete('archive-restore ' . $path, $path . '/', $path . '/');
$this->verifyComplete('archive-restore ' . $path . '/', 'asteroid', 'zodiac.tar.gz');
$this->verifyComplete('archive-restore ' . $path . '/a', $path . '/asteroid', $path . '/astronaut');
$this->verifyComplete('archive-restore ' . $path . '/ast', 'asteroid', 'astronaut');
$this->verifyComplete('archive-restore ' . $path . '/aste', $path . '/asteroid/', $path . '/asteroid/');
$this->verifyComplete('archive-restore ' . $path . '/asteroid', $path . '/asteroid/', $path . '/asteroid/');
$this->verifyComplete('archive-restore ' . $path . '/asteroid/', $path . '/asteroid/ceres', $path . '/asteroid/chiron');
$this->verifyComplete('archive-restore ' . $path . '/asteroid/ch', $path . '/asteroid/chiron/', $path . '/asteroid/chiron/');
$this->verifyComplete('archive-restore ' . $path . '/astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
$this->verifyComplete('archive-restore ' . $path . '/astronaut/y', $path . '/astronaut/yuri\ gagarin.tar.gz', $path . '/astronaut/yuri\ gagarin.tar.gz');
// Absolute via parent path substrings.
$this->verifyComplete('archive-restore ' . $path . '/asteroid/../astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
$this->verifyComplete('archive-restore ' . $path . '/asteroid/../astronaut/y', $path . '/asteroid/../astronaut/yuri\ gagarin.tar.gz', $path . '/asteroid/../astronaut/yuri\ gagarin.tar.gz');
// Parent directory path substrings.
chdir('asteroid/chiron');
$this->verifyComplete('archive-restore ../../astronaut/', 'aldrin.tar.gz', 'yuri gagarin.tar.gz');
$this->verifyComplete('archive-restore ../../astronaut/y', '../../astronaut/yuri\ gagarin.tar.gz', '../../astronaut/yuri\ gagarin.tar.gz');
chdir($path);
}
/**
* Helper function to call completion and make common checks.
*
* @param $command
* The command line to attempt to complete.
* @param $first
* String indicating the expected first completion suggestion.
* @param $last
* String indicating the expected last completion suggestion.
* @param bool $cache_hit
* Optional parameter, if TRUE or omitted the debug log is checked to
* ensure a cache hit on the last cache debug log entry, if FALSE then a
* cache miss is checked for.
*/
function verifyComplete($command, $first, $last, $cache_hit = TRUE) {
// We capture debug output to a separate file, so we can check for cache
// hits/misses.
$debug_file = tempnam(UNISH_SANDBOX . '/complete-debug', 'complete-debug');
// Commands should take the format:
// drush --early=includes/complete.inc [--complete-debug] drush [@alias] [command]...
$exec = sprintf('%s --early=includes/complete.inc --config=%s --complete-debug %s %s 2> %s', UNISH_DRUSH, UNISH_SANDBOX . '/drushrc.php', UNISH_DRUSH, $command, $debug_file);
$this->execute($exec);
$result = $this->getOutputAsList();
$actual = reset($result);
$this->assertEquals("$command: (f) $first", "$command: (f) $actual");
$actual = end($result);
$this->assertEquals("$command: (l) $last", "$command: (l) $actual");
// If checking for HIT, we ensure no MISS exists, if checking for MISS we
// ensure no HIT exists. However, we exclude the first cache report, since
// it is expected that the command-names cache (loaded when matching
// command names) may sometimes be a HIT even when we are testing for a MISS
// in the actual cache we are loading to complete against.
$check_not_exist = 'HIT';
if ($cache_hit) {
$check_not_exist = 'MISS';
}
$contents = file_get_contents($debug_file);
// Find the all cache messages of type "-complete-"
preg_match_all("/Cache [A-Z]* cid:.*-complete-/", $contents, $matches);
$contents = implode("\n", $matches[0]);
$first_cache_pos = strpos($contents, 'Cache ') + 6;
$this->assertFalse(strpos($contents, 'Cache ' . $check_not_exist . ' cid', $first_cache_pos));
unlink($debug_file);
}
}