Skip to content

Commit

Permalink
Compile InstalledVersions.php if present (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored Sep 27, 2020
1 parent f0d1c47 commit e65178c
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ matrix:
- php: '7.4'
env: COVERAGE='true'
- php: '7.4'
env: COMPOSER_VERSION='snapshot'
env: COMPOSER_VERSION='2'
allow_failures:
- php: nightly
fast_finish: true
Expand Down
15 changes: 14 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ tm: $(TU_BOX_DEPS) $(INFECTION)

.PHONY: e2e
e2e: ## Runs all the end-to-end tests
e2e: e2e_php_settings_checker e2e_scoper_alias e2e_scoper_whitelist e2e_check_requirements e2e_symfony
e2e: e2e_php_settings_checker e2e_scoper_alias e2e_scoper_whitelist e2e_check_requirements e2e_symfony e2e_composer_installed_versions

.PHONY: e2e_scoper_alias
e2e_scoper_alias: ## Runs the end-to-end tests to check that the PHP-Scoper config API regarding the prefix alias is working
Expand Down Expand Up @@ -273,6 +273,15 @@ e2e_symfony: fixtures/build/dir012/vendor box

diff fixtures/build/dir012/expected-output fixtures/build/dir012/actual-output

.PHONY: e2e_composer_installed_versions
e2e_composer_installed_versions: ## Packages an app using Composer\InstalledVersions
e2e_composer_installed_versions: fixtures/build/dir013/vendor box
./box compile --working-dir fixtures/build/dir013

php fixtures/build/dir013/bin/run.phar > fixtures/build/dir013/actual-output

diff fixtures/build/dir013/expected-output fixtures/build/dir013/actual-output

.PHONY: blackfire
blackfire: ## Profiles the compile step
blackfire: box
Expand Down Expand Up @@ -355,6 +364,10 @@ fixtures/build/dir012/vendor:
composer install --working-dir fixtures/build/dir012
touch $@

fixtures/build/dir013/vendor:
composer install --working-dir fixtures/build/dir013
touch $@

.PHONY: fixtures/default_stub.php
fixtures/default_stub.php:
bin/generate_default_stub
Expand Down
3 changes: 3 additions & 0 deletions fixtures/build/dir013/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
bin/run.phar
actual-output
18 changes: 18 additions & 0 deletions fixtures/build/dir013/bin/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env php
<?php declare(strict_types = 1);

require_once __DIR__ . '/../vendor/autoload.php';

set_error_handler(function($errno, $errstr, $errfile, $errline, $errcontext) {
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}

throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
});

// force autoloader
class_exists(\Composer\InstalledVersions::class);

echo "OK\n";
4 changes: 4 additions & 0 deletions fixtures/build/dir013/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "bin/run",
"force-autodiscovery": true
}
5 changes: 5 additions & 0 deletions fixtures/build/dir013/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bin": [
"bin/run"
]
}
18 changes: 18 additions & 0 deletions fixtures/build/dir013/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fixtures/build/dir013/expected-output
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OK
9 changes: 8 additions & 1 deletion src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public static function create(?string $file, stdClass $raw): self
);

/**
* @var string[]
* @var string[] $excludedPaths
* @var Closure $blacklistFilter
*/
[$excludedPaths, $blacklistFilter] = self::retrieveBlacklistFilter(
Expand Down Expand Up @@ -1398,6 +1398,13 @@ private static function retrieveAllDirectoriesToInclude(
$filesToAppend[] = $installedJsonFiles;
}

// The InstalledVersions.php file is necessary since Composer v2 adds it to the autoloader class map
$installedVersionsPhp = self::normalizePath($vendorDir.'/composer/InstalledVersions.php', $basePath);

if (file_exists($installedVersionsPhp)) {
$filesToAppend[] = $installedVersionsPhp;
}

$vendorPackages = toArray(values(map(
$toString,
Finder::create()
Expand Down
28 changes: 16 additions & 12 deletions tests/Console/Command/CompileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public function test_it_can_build_a_PHAR_file(): void

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

dump_file(
Expand Down Expand Up @@ -277,6 +277,7 @@ public function test_it_can_build_a_PHAR_file(): void
'/.box/vendor/autoload.php',
'/.box/vendor/composer/',
'/.box/vendor/composer/ClassLoader.php',
'/.box/vendor/composer/InstalledVersions.php',
'/.box/vendor/composer/LICENSE',
'/.box/vendor/composer/autoload_classmap.php',
'/.box/vendor/composer/autoload_namespaces.php',
Expand Down Expand Up @@ -322,7 +323,8 @@ public function test_it_can_build_a_PHAR_file(): void

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

Expand Down Expand Up @@ -396,8 +398,8 @@ public function test_it_can_build_a_PHAR_without_any_configuration(): void

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

$expected = <<<OUTPUT
Expand Down Expand Up @@ -514,6 +516,7 @@ public function test_it_can_build_a_PHAR_without_any_configuration(): void
'/.box/vendor/autoload.php',
'/.box/vendor/composer/',
'/.box/vendor/composer/ClassLoader.php',
'/.box/vendor/composer/InstalledVersions.php',
'/.box/vendor/composer/LICENSE',
'/.box/vendor/composer/autoload_classmap.php',
'/.box/vendor/composer/autoload_namespaces.php',
Expand Down Expand Up @@ -562,7 +565,8 @@ public function test_it_can_build_a_PHAR_without_any_configuration(): void

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

Expand Down Expand Up @@ -829,9 +833,9 @@ public function test_it_can_build_a_PHAR_file_in_verbose_mode(): void
$numberOfClasses = 0;
$numberOfFiles = 45;
if (self::$runComposer2) {
// From Composer 2 there is one more class: Composer\InstalledVersions
// From Composer 2 there are more files
++$numberOfClasses;
++$numberOfFiles;
$numberOfFiles += 2;
}

dump_file(
Expand Down Expand Up @@ -951,9 +955,9 @@ public function test_it_can_build_a_PHAR_file_in_very_verbose_mode(): void
$numberOfClasses = 0;
$numberOfFiles = 45;
if (self::$runComposer2) {
// From Composer 2 there is one more class: Composer\InstalledVersions
// From Composer 2 there are more files
++$numberOfClasses;
++$numberOfFiles;
$numberOfFiles += 2;
}

dump_file(
Expand Down Expand Up @@ -2911,8 +2915,8 @@ public function test_it_can_generate_a_PHAR_with_docker(): void

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

$expected = <<<OUTPUT
Expand Down
1 change: 1 addition & 0 deletions tests/RequirementChecker/RequirementsDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function test_it_dumps_the_requirement_checker_files(
];

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

Expand Down

0 comments on commit e65178c

Please sign in to comment.