-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathComposerSandbox.php
More file actions
441 lines (370 loc) · 11.3 KB
/
ComposerSandbox.php
File metadata and controls
441 lines (370 loc) · 11.3 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
namespace Creativestyle\Composer\Patchset\Tests\Functional\Fixtures;
use Composer\IO\ConsoleIO;
use Composer\IO\IOInterface;
use Composer\Package\Package;
use Composer\Satis\Builder\PackagesBuilder;
use Composer\Satis\PackageSelection\PackageSelection;
use Composer\Util\Filesystem;
use Composer\Util\ProcessExecutor;
use Composer\Factory as ComposerFactory;
use Composer\Config as ComposerConfig;
use Composer\Repository\RepositoryInterface as ComposerRepositoryInterface;
use Composer\Repository\ArrayRepository as ComposerArrayRepository;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
/**
* Sandbox helper for doing functional composer tests.
*
* Provides a fixture-based local repository and en environment with it's own composer cache, config.
*/
class ComposerSandbox
{
const DEFAULT_PACKAGE_FIXTURES_DIR = __DIR__ . '/packages';
/**
* @var bool
*/
public static $debugOutputEnabled = false;
/**
* Path to root dir of the project being tested
*
* @var string
*/
private $selfPackageDir;
/**
* Where the package fixtures are located
*
* @var string
*/
private $packageFixturesDir;
/**
* Root temporary dir for this class instance
*
* @var string
*/
private $tempDir;
/**
* @var string
*/
private $tempBinDir;
/**
* @var string
*/
private $tempComposerHomeDir;
/**
* @var string
*/
private $tempProjectDir;
/**
* @var string
*/
private $tempRepositoryDir;
/**
* @var ProcessExecutor
*/
private $executor;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var ComposerRepositoryInterface
*/
private $packages;
/**
* @var InputInterface
*/
private $input;
/**
* @var OutputInterface
*/
private $output;
/**
* @var IOInterface
*/
private $io;
/**
* @var ProjectSandbox[]
*/
private $projectSandboxes = [];
/**
* @param string|null $packageFixturesDir
* @param string|null $selfPackageDir
* @param string|null $tempDir
*/
public function __construct(
$packageFixturesDir = null,
$selfPackageDir = null,
$tempDir = null
) {
$this->executor = new ProcessExecutor();
$this->filesystem = new Filesystem($this->executor);
if (null === $packageFixturesDir) {
$packageFixturesDir = self::DEFAULT_PACKAGE_FIXTURES_DIR;
}
if (null === $tempDir) {
$tempDir = sys_get_temp_dir() . '/' . uniqid('composer-plugin-patchset-test-fixtures');
}
if (null === $selfPackageDir) {
$selfPackageDir = getcwd();
}
$this->input = new StringInput('');
$this->output = static::$debugOutputEnabled ? new ConsoleOutput() : new NullOutput();
$this->io = new ConsoleIO($this->input, $this->output, new HelperSet());
$this->selfPackageDir = $selfPackageDir;
$this->packageFixturesDir = $packageFixturesDir;
$this->tempDir = $tempDir;
$this->tempBinDir = $this->tempDir . '/bin';
$this->tempComposerHomeDir = $this->tempDir . '/composer';
$this->tempProjectDir = $this->tempDir . '/project';
$this->tempRepositoryDir = $this->tempDir . '/repo';
$this->init();
}
private function init()
{
if (null !== $this->packages) {
throw new \RuntimeException('Already initialized');
}
$this->io->write(sprintf("\n--- Initializing composer sandbox with fixtures from <info>%s</info> ---\n", $this->packageFixturesDir));
$this->filesystem->ensureDirectoryExists($this->tempDir);
$this->filesystem->ensureDirectoryExists($this->tempBinDir);
$this->filesystem->ensureDirectoryExists($this->tempComposerHomeDir);
$this->filesystem->ensureDirectoryExists($this->tempProjectDir);
$this->filesystem->ensureDirectoryExists($this->tempRepositoryDir);
$this->packages = $this->gatherPackages();
$this->buildRepository($this->packages, $this->tempRepositoryDir);
// This has to be done after repo is built as to not confuse composer/satis earlier
$this->writeProjectComposerConfig();
}
private function gatherPackages()
{
$configFinder = Finder::create()
->in($this->packageFixturesDir)
->files()
->name('composer.json');
$configs = array_map('strval', iterator_to_array($configFinder));
$packageList = array_map([$this, 'collectPackage'], $configs);
// Add self as dev-master
$packageList[] = $this->collectPackage($this->selfPackageDir . '/composer.json', null, 'dev-master');
return new ComposerArrayRepository($packageList);
}
/**
* @param string $configFile
* @param string|null $name
* @param string|null $version
* @return Package
*/
private function collectPackage($configFile, $name = null, $version = null)
{
$configData = json_decode(file_get_contents($configFile), true);
if (null === $configData) {
throw new \RuntimeException("Could not decode JSON in ${$configFile}: " . json_last_error_msg());
}
if (null === $name) {
if (!isset($configData['name'])) {
throw new \RuntimeException("No package name found in ${$configFile}");
}
$name = $configData['name'];
}
if (null === $version) {
if (!isset($configData['version'])) {
throw new \RuntimeException("No package version found in ${$configFile}");
}
$version = $configData['version'];
}
return new FixturePackage(
$name,
$version,
dirname($configFile),
$configData
);
}
/**
* @return array
*/
private function buildBaseComposerConfig()
{
return [
'config' => [
'home' => $this->getTempComposerHomeDir(),
'cache-dir' => $this->getTempComposerHomeDir() . '/cache',
'data-dir' => $this->getTempComposerHomeDir() . '/data',
]
];
}
/**
* @param ComposerArrayRepository $packages
* @return array
*/
private function buildSatisComposerConfig($packages)
{
return array_merge($this->buildBaseComposerConfig(), [
'repositories' => $this->buildRepositoriesConfig($packages),
]);
}
/**
* @param ComposerArrayRepository $packages
* @return array
*/
private function buildSatisConfig($packages)
{
return array_merge($this->buildSatisComposerConfig($packages), [
'name' => 'Testing Satis Repository'
]);
}
/**
* @return array
*/
private function buildProjectComposerConfig()
{
return array_merge(
$this->buildBaseComposerConfig(),
[
'repositories' => [
[
'type' => 'composer',
'url' => 'file://' . $this->getTempRepositoryDir()
],
[
// Never search packagist, this will speed the tests up by large margin
'packagist.org' => false
]
]
]
);
}
/**
* @param ComposerArrayRepository $packages
* @param string $repoDir
*/
private function buildRepository($packages, $repoDir)
{
// Let's do not pull all packages from packagist, this would make this slow as hell
unset(ComposerConfig::$defaultRepositories['packagist'], ComposerConfig::$defaultRepositories['packagist.org']);
$satisConfig = $this->buildSatisConfig($packages);
$composerConfig = $this->buildSatisComposerConfig($packages);
$composer = ComposerFactory::create($this->io, $composerConfig);
$packageSelection = new PackageSelection(
$this->output,
$repoDir,
$satisConfig,
false
);
$builder = new PackagesBuilder(
$this->output,
$repoDir,
$satisConfig,
false
);
$builder->dump($packageSelection->select($composer, true));
}
/**
* @param ComposerArrayRepository $packages
* @return array
*/
private function buildRepositoriesConfig($packages)
{
$repos = [];
/** @var FixturePackage $package */
foreach ($packages->getPackages() as $package) {
$repos[] = [
'type' => 'package',
'package' => $package->buildPackageRepositoryData()
];
}
return $repos;
}
private function writeProjectComposerConfig()
{
file_put_contents(
$this->getComposerConfigFilePath(),
json_encode($this->buildProjectComposerConfig(), JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE)
);
}
/**
* @param string $name
* @param string $version
* @param array $config Data to be written to composer.json
* @return ProjectSandbox
*/
public function createProjectSandBox($name, $version = 'dev-master', array $config = [])
{
$project = new ProjectSandbox(
$name,
$version,
$config,
$this,
$this->executor,
$this->filesystem,
$this->io
);
$this->projectSandboxes[] = $project;
return $project;
}
/**
* @return string
*/
public function getTempBinDir()
{
return $this->tempBinDir;
}
/**
* @return string
*/
public function getTempComposerHomeDir()
{
return $this->tempComposerHomeDir;
}
/**
* @return string
*/
public function getTempProjectDir()
{
return $this->tempProjectDir;
}
/**
* @return string
*/
public function getTempRepositoryDir()
{
return $this->tempRepositoryDir;
}
/**
* @param string $packageName
* @param string $packageVersion
* @return string|null
*/
public function getPackageDataDir($packageName, $packageVersion = '*')
{
/** @var FixturePackage $package */
$package = $this->packages->findPackage($packageName, $packageVersion);
if (!$package) {
return null;
}
return $package->getDataDir();
}
/**
* @return string
*/
public function getComposerConfigFilePath()
{
return $this->getTempComposerHomeDir() . '/config.json';
}
public function cleanupProjects()
{
foreach ($this->projectSandboxes as $projectSandbox) {
$projectSandbox->cleanup();
}
$this->projectSandboxes = [];
}
public function cleanup()
{
$this->cleanupProjects();
$this->filesystem->removeDirectory($this->tempDir);
}
}