Skip to content

Commit

Permalink
Add support for Composer 2 (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored Sep 5, 2020
1 parent e708d75 commit ee2e9aa
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 13 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ cache:

env:
global:
secure: "S7EIgyCsWVhv8ppi5t7FhbBQ6eo9Hn+ZpXNIoZ0t1EX0qMKdG1idKGtb6NIO1YDG8cVy1KkPBOUde32Bk62PVK0dDYlD0bH9CczPRt8Fi3BP/t+6uLgXuweOpWNgoy/41nONO4hIOUxL4cWgfeqI6ajuCHa0hF/dD3mbUPH+FU0/mnggWxaUfDWplPqmkiYjyC/CUm4F3ixtXryG+GYQkl8uDNou0bDuLBCuzDYN98AFNEqsj4auWo70nU4mKmEzHjSz7dpLnGmEDA0c/4XuMilsucYyvaZDjQzTro4tMAyg0d/4F0l/njWKIuydRWCMG9NzuyXfVU4hiybLgaQJauM3p/k9jdBKNBk1AMTXmm2uuyFkP+hsH4tIUtuGOym5HM1FMH56Fd95JVN9f1eRGHxKLbOf4dO8COxiyGXRI+FUzpAZrsygA28vQ/JSTIPaBVnsk73MSlUQRGO4SymKABvM3ntzaPEhuaPu/e6q826yJQ9SLlUa4Iw5N8HBlkPPDxuyeDYQkhMNBJrg7rJ0E8g3kGrckW+exIqIjr6FGaL82i7yLmtC+lpZ34rC3UbuYbYtgN/k6A1e3K4OSEybuSkFWVScK+9P5JYVQOP+c14rom3cbBFikG0pwc+TEyjWKkOQKwRHBXyih+60OhbcwShrUxiRGwyuFAZt5jVqyKw="
- COMPOSER_VERSION='1'
- secure: "S7EIgyCsWVhv8ppi5t7FhbBQ6eo9Hn+ZpXNIoZ0t1EX0qMKdG1idKGtb6NIO1YDG8cVy1KkPBOUde32Bk62PVK0dDYlD0bH9CczPRt8Fi3BP/t+6uLgXuweOpWNgoy/41nONO4hIOUxL4cWgfeqI6ajuCHa0hF/dD3mbUPH+FU0/mnggWxaUfDWplPqmkiYjyC/CUm4F3ixtXryG+GYQkl8uDNou0bDuLBCuzDYN98AFNEqsj4auWo70nU4mKmEzHjSz7dpLnGmEDA0c/4XuMilsucYyvaZDjQzTro4tMAyg0d/4F0l/njWKIuydRWCMG9NzuyXfVU4hiybLgaQJauM3p/k9jdBKNBk1AMTXmm2uuyFkP+hsH4tIUtuGOym5HM1FMH56Fd95JVN9f1eRGHxKLbOf4dO8COxiyGXRI+FUzpAZrsygA28vQ/JSTIPaBVnsk73MSlUQRGO4SymKABvM3ntzaPEhuaPu/e6q826yJQ9SLlUa4Iw5N8HBlkPPDxuyeDYQkhMNBJrg7rJ0E8g3kGrckW+exIqIjr6FGaL82i7yLmtC+lpZ34rC3UbuYbYtgN/k6A1e3K4OSEybuSkFWVScK+9P5JYVQOP+c14rom3cbBFikG0pwc+TEyjWKkOQKwRHBXyih+60OhbcwShrUxiRGwyuFAZt5jVqyKw="
matrix:
- COMPOSER_FLAGS='--no-interaction --no-progress --no-suggest --prefer-dist'

Expand All @@ -23,8 +24,10 @@ matrix:
env: DEPLOY=true
- php: '7.3'
env: PHAR_READONLY='true'
- php: '7.3'
- php: '7.4'
env: COVERAGE='true'
- php: '7.4'
env: COMPOSER_VERSION='snapshot'
allow_failures:
- php: nightly
fast_finish: true
Expand All @@ -36,6 +39,7 @@ before_install:
fi
- set -eo pipefail
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- composer self-update --$COMPOSER_VERSION --no-interaction
- composer validate
- |
if [ "DEPLOY" != "true" ]; then
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
"ext-phar": "*",
"amphp/parallel-functions": "^0.1.3",
"beberlei/assert": "^3.2",
"composer/package-versions-deprecated": "^1.8",
"composer/semver": "^1.5",
"composer/xdebug-handler": "^1.3.2",
"hoa/compiler": "^3.17",
"humbug/php-scoper": "^0.13",
"justinrainbow/json-schema": "^5.2.9",
"nikic/iter": "^2.0",
"nikic/php-parser": "^4.2",
"ocramius/package-versions": "^1.4",
"opis/closure": "^3.2",
"paragonie/pharaoh": "^0.5",
"phpseclib/phpseclib": "^2.0",
Expand Down
32 changes: 31 additions & 1 deletion src/Composer/ComposerOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ final class ComposerOrchestrator
{
use NotInstantiable;

public static function getVersion(): string
{
$composerExecutable = self::retrieveComposerExecutable();
$command = [$composerExecutable, '--version'];

$getVersionProcess = new Process($command);

$getVersionProcess->run();

if (false === $getVersionProcess->isSuccessful()) {
throw new RuntimeException(
'Could not determine the Composer version.',
0,
new ProcessFailedException($getVersionProcess)
);
}

$output = $getVersionProcess->getOutput();

if (preg_match('/^Composer version ([^\\s]+)/', $output, $match) > 0) {
return $match[1];
}

throw new RuntimeException('Could not determine the Composer version.');
}

public static function dumpAutoload(
Whitelist $whitelist,
string $prefix,
Expand Down Expand Up @@ -176,7 +202,11 @@ private static function retrieveAutoloadFile(string $composerExecutable, Compile
$vendorDirProcess->run();

if (false === $vendorDirProcess->isSuccessful()) {
new ProcessFailedException($vendorDirProcess);
throw new RuntimeException(
'Could not retrieve the vendor dir.',
0,
new ProcessFailedException($vendorDirProcess)
);
}

return trim($vendorDirProcess->getOutput()).'/autoload.php';
Expand Down
25 changes: 23 additions & 2 deletions tests/Composer/ComposerOrchestratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,17 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_lock_and_in
'vendor/composer/autoload_static.php',
'vendor/composer/ClassLoader.php',
'vendor/composer/installed.json',
'vendor/composer/LICENSE',
];

if ($this->isInstalledWithComposer2(self::FIXTURES.'/dir001')) {
$expectedPaths[] = 'vendor/composer/installed.php';
$expectedPaths[] = 'vendor/composer/InstalledVersions.php';
$expectedPaths[] = 'vendor/composer/LICENSE';
$expectedPaths[] = 'vendor/composer/platform_check.php';
} else {
$expectedPaths[] = 'vendor/composer/LICENSE';
}

$actualPaths = $this->retrievePaths();

$this->assertSame($expectedPaths, $actualPaths);
Expand Down Expand Up @@ -332,9 +340,17 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_lock_and_in
'vendor/composer/autoload_static.php',
'vendor/composer/ClassLoader.php',
'vendor/composer/installed.json',
'vendor/composer/LICENSE',
];

if ($this->isInstalledWithComposer2(self::FIXTURES.'/dir003')) {
$expectedPaths[] = 'vendor/composer/installed.php';
$expectedPaths[] = 'vendor/composer/InstalledVersions.php';
$expectedPaths[] = 'vendor/composer/LICENSE';
$expectedPaths[] = 'vendor/composer/platform_check.php';
} else {
$expectedPaths[] = 'vendor/composer/LICENSE';
}

$actualPaths = $this->retrievePaths();

$this->assertSame($expectedPaths, $actualPaths);
Expand Down Expand Up @@ -625,4 +641,9 @@ private function retrievePaths(): array

return $this->normalizePaths(iterator_to_array($finder, false), true);
}

private function isInstalledWithComposer2(string $baseDir): bool
{
return file_exists($baseDir.'/vendor/composer/InstalledVersions.php');
}
}
85 changes: 78 additions & 7 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use function json_encode;
use const JSON_PRETTY_PRINT;
use KevinGH\Box\Compactor\Php;
use KevinGH\Box\Composer\ComposerOrchestrator;
use KevinGH\Box\Console\DisplayNormalizer;
use function KevinGH\Box\FileSystem\chmod;
use function KevinGH\Box\FileSystem\dump_file;
Expand Down Expand Up @@ -78,6 +79,16 @@ class CompileTest extends CommandTestCase

private const FIXTURES_DIR = __DIR__.'/../../../fixtures/build';

private static $runComposer2 = false;

/**
* {@inheritdoc}
*/
public static function setUpBeforeClass(): void
{
self::$runComposer2 = version_compare(ComposerOrchestrator::getVersion(), '2', '>=');
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -110,6 +121,12 @@ public function test_it_can_build_a_PHAR_file(): void

$shebang = sprintf('#!%s', (new PhpExecutableFinder())->find());

$numberOfFiles = 41;
if (self::$runComposer2) {
// From Composer 2 there is one more class: Composer\InstalledVersions
$numberOfFiles++;
}

dump_file(
'box.json',
json_encode(
Expand Down Expand Up @@ -192,7 +209,7 @@ public function test_it_can_build_a_PHAR_file(): void
No recommendation found.
No warning found.
// PHAR: 41 files (100B)
// PHAR: $numberOfFiles files (100B)
// You can inspect the generated PHAR with the "info" command.
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
Expand Down Expand Up @@ -267,6 +284,7 @@ public function test_it_can_build_a_PHAR_file(): void
'/.box/vendor/composer/autoload_psr4.php',
'/.box/vendor/composer/autoload_real.php',
'/.box/vendor/composer/autoload_static.php',
'/.box/vendor/composer/platform_check.php',
'/.box/vendor/composer/semver/',
'/.box/vendor/composer/semver/LICENSE',
'/.box/vendor/composer/semver/src/',
Expand Down Expand Up @@ -299,6 +317,12 @@ public function test_it_can_build_a_PHAR_file(): void
'/vendor/composer/autoload_static.php',
];

if (!self::$runComposer2) {
$expectedFiles = array_values(array_filter($expectedFiles, static function ($file): bool {
return $file !== '/.box/vendor/composer/platform_check.php';
}));
}

$actualFiles = $this->retrievePharFiles($phar);

$this->assertSame($expectedFiles, $actualFiles);
Expand Down Expand Up @@ -367,6 +391,12 @@ public function test_it_can_build_a_PHAR_without_any_configuration(): void

$version = get_box_version();

$numberOfFiles = 45;
if (self::$runComposer2) {
// From Composer 2 there is one more class: Composer\InstalledVersions
$numberOfFiles++;
}

$expected = <<<OUTPUT
____
Expand Down Expand Up @@ -406,7 +436,7 @@ public function test_it_can_build_a_PHAR_without_any_configuration(): void
No recommendation found.
No warning found.
// PHAR: 45 files (100B)
// PHAR: $numberOfFiles files (100B)
// You can inspect the generated PHAR with the "info" command.
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
Expand Down Expand Up @@ -487,6 +517,7 @@ public function test_it_can_build_a_PHAR_without_any_configuration(): void
'/.box/vendor/composer/autoload_psr4.php',
'/.box/vendor/composer/autoload_real.php',
'/.box/vendor/composer/autoload_static.php',
'/.box/vendor/composer/platform_check.php',
'/.box/vendor/composer/semver/',
'/.box/vendor/composer/semver/LICENSE',
'/.box/vendor/composer/semver/src/',
Expand Down Expand Up @@ -522,6 +553,12 @@ public function test_it_can_build_a_PHAR_without_any_configuration(): void
'/vendor/composer/autoload_static.php',
];

if (!self::$runComposer2) {
$expectedFiles = array_values(array_filter($expectedFiles, static function ($file): bool {
return $file !== '/.box/vendor/composer/platform_check.php';
}));
}

$actualFiles = $this->retrievePharFiles($phar);

$this->assertSame($expectedFiles, $actualFiles);
Expand Down Expand Up @@ -782,6 +819,14 @@ public function test_it_can_build_a_PHAR_file_in_verbose_mode(): void

$shebang = sprintf('#!%s', (new PhpExecutableFinder())->find());

$numberOfClasses = 0;
$numberOfFiles = 41;
if (self::$runComposer2) {
// From Composer 2 there is one more class: Composer\InstalledVersions
$numberOfClasses++;
$numberOfFiles++;
}

dump_file(
'box.json',
json_encode(
Expand Down Expand Up @@ -856,7 +901,7 @@ public function test_it_can_build_a_PHAR_file_in_verbose_mode(): void
? Dumping the Composer autoloader
> '/usr/local/bin/composer' 'dump-autoload' '--classmap-authoritative' '--no-dev'
Generating optimized autoload files (authoritative)
Generated optimized autoload files (authoritative) containing 0 classes
Generated optimized autoload files (authoritative) containing $numberOfClasses classes
? Removing the Composer dump artefacts
? No compression
Expand All @@ -871,7 +916,7 @@ public function test_it_can_build_a_PHAR_file_in_verbose_mode(): void
No recommendation found.
No warning found.
// PHAR: 41 files (100B)
// PHAR: $numberOfFiles files (100B)
// You can inspect the generated PHAR with the "info" command.
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
Expand All @@ -896,6 +941,14 @@ public function test_it_can_build_a_PHAR_file_in_very_verbose_mode(): void

$shebang = sprintf('#!%s', (new PhpExecutableFinder())->find());

$numberOfClasses = 0;
$numberOfFiles = 41;
if (self::$runComposer2) {
// From Composer 2 there is one more class: Composer\InstalledVersions
$numberOfClasses++;
$numberOfFiles++;
}

dump_file(
'box.json',
json_encode(
Expand Down Expand Up @@ -974,7 +1027,7 @@ public function test_it_can_build_a_PHAR_file_in_very_verbose_mode(): void
? Dumping the Composer autoloader
> '/usr/local/bin/composer' 'dump-autoload' '--classmap-authoritative' '--no-dev' '-v'
Generating optimized autoload files (authoritative)
Generated optimized autoload files (authoritative) containing 0 classes
Generated optimized autoload files (authoritative) containing $numberOfClasses classes
? Removing the Composer dump artefacts
? No compression
Expand All @@ -989,7 +1042,7 @@ public function test_it_can_build_a_PHAR_file_in_very_verbose_mode(): void
No recommendation found.
No warning found.
// PHAR: 41 files (100B)
// PHAR: $numberOfFiles files (100B)
// You can inspect the generated PHAR with the "info" command.
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
Expand Down Expand Up @@ -2849,6 +2902,12 @@ public function test_it_can_generate_a_PHAR_with_docker(): void

$version = get_box_version();

$numberOfFiles = 37;
if (self::$runComposer2) {
// From Composer 2 there is one more class: Composer\InstalledVersions
$numberOfFiles++;
}

$expected = <<<OUTPUT
____
Expand Down Expand Up @@ -2888,7 +2947,7 @@ public function test_it_can_generate_a_PHAR_with_docker(): void
No recommendation found.
No warning found.
// PHAR: 37 files (100B)
// PHAR: $numberOfFiles files (100B)
// You can inspect the generated PHAR with the "info" command.
// Memory usage: 5.00MB (peak: 10.00MB), time: 0.00s
Expand Down Expand Up @@ -2957,6 +3016,18 @@ private function normalizeDisplay(string $display): string
$display
);

$display = preg_replace(
'/\[debug\] Increased the maximum number of open file descriptors from \([^\)]+\) to \([^\)]+\)' . PHP_EOL . '/',
'',
$display
);

$display = str_replace(
'[debug] Restored the maximum number of open file descriptors' . PHP_EOL,
'',
$display
);

return DisplayNormalizer::removeTrailingSpaces($display);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/RequirementChecker/RequirementsDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function test_it_dumps_the_requirement_checker_files(
'vendor/composer/semver/src/VersionParser.php',
];

if (file_exists(__DIR__ . '/../../.requirement-checker/vendor/composer/platform_check.php')) {
$expectedFiles[] = 'vendor/composer/platform_check.php';
}

sort($expectedFiles);

$this->assertSame(
Expand Down

0 comments on commit ee2e9aa

Please sign in to comment.