forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrushScriptTest.php
148 lines (124 loc) · 5.6 KB
/
drushScriptTest.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
<?php
namespace Unish;
use Webmozart\PathUtil\Path;
/**
* Tests for the 'drush' script itself
*/
class drushScriptCase extends CommandUnishTestCase {
/**
* Test `PHP_OPTIONS=... drush`
*/
public function testPhpOptionsTest() {
$this->markTestSkipped('Environment variables not yet passed along to Process by execute().');
// @todo: could probably run this test on mingw
if ($this->is_windows()) {
$this->markTestSkipped('Environment variable tests not currently functional on Windows.');
}
$options = array();
$env = array('PHP_OPTIONS' => '-d default_mimetype="text/drush"');
$this->drush('ev', array('print ini_get("default_mimetype");'), $options, NULL, NULL, self::EXIT_SUCCESS, NULL, $env);
$output = $this->getOutput();
$this->assertEquals('text/drush', $output);
}
public function testDrushFinder() {
$this->markTestSkipped('The Finder is not long for this world. Disabling this test.');
$globalDrushDotPhp = Path::join(self::getDrush(), '../drush.php');
// Control: test `drush --root ` ... with no site-local Drush
$drush_location = $this->getDrushLocation();
$this->assertEquals($globalDrushDotPhp, $drush_location);
// We will try copying a site-local Drush to
// all of the various locations the 'drush finder'
// might expect to find it.
$drush_locations = array(
"vendor",
"../vendor",
"sites/all/vendor",
"sites/all",
);
foreach ($drush_locations as $drush_base) {
$drush_root = $this->create_site_local_drush($drush_base);
// Test `drush --root ` ... with a site-local Drush
$drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
$this->assertEquals(realpath($drush_root . '/drush.php'), realpath($drush_location));
// Ensure that --local was NOT added
$result = $this->drush('ev', array('return drush_get_option("local");'), array('root' => $this->webroot()));
$output = $this->getOutput();
$this->assertEquals("", $output);
// Run the `drush --root` test again, this time with
// a drush.wrapper script in place.
$this->createDrushWrapper($drush_base);
$drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
$this->assertEquals(realpath($drush_root . '/drush.php'), realpath($drush_location));
// Test to see if --local was added
$result = $this->drush('ev', array('var_export(drush_get_option("local"));'), array('root' => $this->webroot()));
$output = $this->getOutput();
$this->assertEquals("true", $output);
// Get rid of the symlink and site-local Drush we created
$this->remove_site_local_drush($drush_base);
}
// Next, try again with a site-local Drush in a location
// that Drush does not search.
$mysterious_location = "path/drush/does/not/search";
$drush_root = $this->create_site_local_drush($mysterious_location);
// We should not find the site-local Drush without a Drush wrapper.
$drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
$this->assertEquals($globalDrushDotPhp, $drush_location);
$this->createDrushWrapper($mysterious_location);
// Now that there is a Drush wrapper, we should be able to find the site-local Drush.
$drush_location = $this->getDrushLocation(array('root' => $this->webroot()));
$this->assertEquals(realpath($drush_root . '/drush.php'), $drush_location);
}
/**
* Copy UNISH_DRUSH into the specified site-local location.
*/
function create_site_local_drush($drush_base) {
$drush_root = $this->webroot() . '/' . $drush_base . '/drush/drush';
$bin_dir = $this->webroot() . '/' . $drush_base . '/bin';
$this->mkdir(dirname($drush_root));
$this->recursive_copy(dirname(UNISH_DRUSH), $drush_root);
@chmod($drush_root . '/drush', 0777);
@chmod($drush_root . '/drush.launcher', 0777);
$this->mkdir($bin_dir);
symlink($drush_root . '/drush', $bin_dir . '/drush');
return $drush_root;
}
function remove_site_local_drush($drush_base) {
// Get rid of the symlink and site-local Drush we created
unish_file_delete_recursive($this->webroot() . '/' . $drush_base . '/drush/drush');
unlink($this->webroot() . '/' . $drush_base . '/bin/drush');
if (file_exists($this->webroot() . '/drush.wrapper')) {
unlink($this->webroot() . '/drush.wrapper');
}
}
/**
* TODO: Create a Drush wrapper script, and copy it to
* to the root of the fake Drupal site, and point it
* at the specified site-local Drush script.
*/
function createDrushWrapper($drush_base) {
$drush_launcher = $drush_base . '/drush/drush/drush.launcher';
$drush_wrapper_src = dirname(UNISH_DRUSH) . '/examples/drush.wrapper';
$drush_wrapper_contents = file_get_contents($drush_wrapper_src);
$drush_wrapper_contents = preg_replace('#\.\./vendor/bin/drush.launcher#', $drush_launcher, $drush_wrapper_contents);
$drush_wrapper_target = $this->webroot() . '/drush.wrapper';
file_put_contents($drush_wrapper_target, $drush_wrapper_contents);
@chmod($drush_wrapper_target, 0777);
}
/**
* Get the current location of the Drush script via
* `drush status 'Drush script' --format=yaml`. This
* will return results other than UNISH_DRUSH in the
* presence of a site-local Drush.
*/
function getDrushLocation($options = array()) {
$options += array(
'format' => 'yaml',
'verbose' => NULL,
'fields' => 'drush-script',
);
$result = $this->drush('status', [], $options);
$output = $this->getOutput();
list($key, $value) = explode(": ", $output);
return trim($value, "'");
}
}