forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unish.sut.php
executable file
·90 lines (78 loc) · 2.77 KB
/
unish.sut.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
#!/usr/bin/env php
<?php
/**
* This script performs setup and then calls `composer install`. It may not autoload code.
*/
require __DIR__ . '/includes/filesystem.inc';
require __DIR__ . '/tests/unish.inc';
list($unish_tmp, $unish_sandbox, $unish_drush_dir) = unishGetPaths();
unish_validate();
$return = unish_setup_sut($unish_sandbox);
exit($return);
function unish_validate() {
if (basename(__DIR__) != 'drush') {
fwrite(STDERR, 'The drush directory must end in /drush in order to run the tests. This is due to the "path" repository in tests/resources/composer.json');
exit(1);
}
}
/**
* Use Composer to build a Drupal codebase, with this Drush symlinked into /vendor.
* @param string $unish_sandbox Path to sandbox.
* @return integer
* Exit code.
*/
function unish_setup_sut($unish_sandbox) {
$working_dir = dirname($unish_sandbox) . DIRECTORY_SEPARATOR . 'build-drush-sut';
$target_dir = dirname($working_dir) . DIRECTORY_SEPARATOR . 'drush-sut';
drush_delete_dir($working_dir, TRUE);
$codebase = 'tests/resources/codebase';
drush_copy_dir($codebase, $working_dir);
$composer_json = getenv('COMPOSER') ?: 'composer.json';
foreach ([$composer_json] as $filename) {
$path = $working_dir . "/$filename";
if (file_exists($path)) {
$contents = file_get_contents($path);
$new_contents = replace_token($contents);
file_put_contents($path, $new_contents);
}
}
$alias_contents = <<<EOT
dev:
root: $target_dir/web
uri: dev
stage:
root: $target_dir/web
uri: stage
EOT;
mkdir("$working_dir/drush");
mkdir("$working_dir/drush/site-aliases");
file_put_contents("$working_dir/drush/site-aliases/sut.alias.yml", $alias_contents);
$verbose = unishIsVerbose();
$cmd = "composer $verbose install --prefer-dist --no-progress --no-suggest --working-dir " . escapeshellarg($working_dir);
fwrite(STDERR, 'Executing: ' . $cmd . "\n");
exec($cmd, $output, $return);
// If requirements force it to, Composer downloads a second Drush, instead of symlink.
$drush_sut = $working_dir . '/vendor/drush/drush';
if (!is_link($drush_sut)) {
fwrite(STDERR, "Drush not symlinked in the System-Under-Test.\n");
$return = 1;
}
// Move the sut into place
drush_delete_dir($target_dir, TRUE);
rename($working_dir, $target_dir);
// If there is no 'vendor' directory in the Drush home dir, then make
// a symlink from the SUT
if (!is_dir(__DIR__ . '/vendor')) {
symlink("$target_dir/vendor", __DIR__ . '/vendor');
}
return $return;
}
/**
* Replace a token with the /path/to/drush for this install.
*
* @param $contents
*/
function replace_token($contents) {
// @todo Use https://getcomposer.org/doc/03-cli.md#modifying-repositories if it can edit composer.lock too.
return str_replace('%PATH-TO-DRUSH%', __DIR__, $contents);
}