1
0
Fork 0
composer/tests/Composer/Test/AllFunctionalTest.php

269 lines
8.7 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
2013-05-27 08:41:50 +00:00
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Test;
use Composer\Pcre\Preg;
use Composer\Util\Filesystem;
2022-02-22 15:47:09 +00:00
use Composer\Util\Platform;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
/**
* @group slow
*/
class AllFunctionalTest extends TestCase
{
2021-10-15 14:26:22 +00:00
/** @var string|false */
2012-11-10 18:42:29 +00:00
protected $oldcwd;
2021-10-15 14:26:22 +00:00
/** @var ?string */
protected $testDir;
2021-10-15 14:26:22 +00:00
/**
* @var string
*/
private static $pharPath;
2021-12-08 16:03:05 +00:00
public function setUp(): void
2012-11-10 18:42:29 +00:00
{
2022-02-22 15:47:09 +00:00
$this->oldcwd = Platform::getCwd();
2012-11-10 18:42:29 +00:00
chdir(__DIR__.'/Fixtures/functional');
}
protected function tearDown(): void
2012-11-10 18:42:29 +00:00
{
parent::tearDown();
2021-10-15 14:26:22 +00:00
if ($this->oldcwd) {
chdir($this->oldcwd);
}
if ($this->testDir) {
2021-02-17 22:26:40 +00:00
$fs = new Filesystem;
$fs->removeDirectory($this->testDir);
$this->testDir = null;
}
}
2021-12-08 16:03:05 +00:00
public static function setUpBeforeClass(): void
{
self::$pharPath = self::getUniqueTmpDirectory() . '/composer.phar';
}
2021-12-08 16:03:05 +00:00
public static function tearDownAfterClass(): void
{
$fs = new Filesystem;
$fs->removeDirectory(dirname(self::$pharPath));
}
public function testBuildPhar(): void
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Building the phar does not work on HHVM.');
}
2014-12-07 13:54:29 +00:00
2015-05-01 13:41:15 +00:00
$target = dirname(self::$pharPath);
$fs = new Filesystem();
2015-05-01 13:41:15 +00:00
chdir($target);
$it = new \RecursiveDirectoryIterator(__DIR__.'/../../../', \RecursiveDirectoryIterator::SKIP_DOTS);
$ri = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($ri as $file) {
$targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
if ($file->isDir()) {
$fs->ensureDirectoryExists($targetPath);
} else {
copy($file->getPathname(), $targetPath);
}
}
$proc = new Process([PHP_BINARY, '-dphar.readonly=0', './bin/compile'], $target);
$exitcode = $proc->run();
2013-03-10 12:58:49 +00:00
if ($exitcode !== 0 || trim($proc->getOutput())) {
$this->fail($proc->getOutput());
}
2017-11-30 14:58:10 +00:00
$this->assertFileExists(self::$pharPath);
copy(self::$pharPath, __DIR__.'/../../composer-test.phar');
2012-11-10 18:42:29 +00:00
}
/**
* @dataProvider getTestFiles
* @depends testBuildPhar
*/
2022-02-22 15:47:09 +00:00
public function testIntegration(string $testFile): void
{
$testData = $this->parseTestFile($testFile);
2021-02-12 10:05:13 +00:00
$this->testDir = self::getUniqueTmpDirectory();
// if a dir is present with the name of the .test file (without .test), we
// copy all its contents in the $testDir to be used to run the test with
$testFileSetupDir = substr($testFile, 0, -5);
if (is_dir($testFileSetupDir)) {
$fs = new Filesystem();
$fs->copy($testFileSetupDir, $this->testDir);
}
2022-08-17 12:20:07 +00:00
$env = [
2021-02-17 22:26:40 +00:00
'COMPOSER_HOME' => $this->testDir.'home',
'COMPOSER_CACHE_DIR' => $this->testDir.'cache',
2022-08-17 12:20:07 +00:00
];
2012-11-11 14:52:37 +00:00
2021-12-10 12:14:04 +00:00
$proc = Process::fromShellCommandline(escapeshellcmd(PHP_BINARY).' '.escapeshellarg(self::$pharPath).' --no-ansi '.$testData['RUN'], $this->testDir, $env, null, 300);
2021-02-12 10:05:13 +00:00
$output = '';
2022-08-17 12:20:07 +00:00
$exitCode = $proc->run(static function ($type, $buffer) use (&$output): void {
2021-02-12 10:05:13 +00:00
$output .= $buffer;
});
if (isset($testData['EXPECT'])) {
2021-02-12 10:05:13 +00:00
$output = trim($this->cleanOutput($output));
$expected = $testData['EXPECT'];
$line = 1;
2021-02-25 12:46:52 +00:00
for ($i = 0, $j = 0; $i < strlen($expected);) {
2021-02-12 10:05:13 +00:00
if ($expected[$i] === "\n") {
$line++;
}
if ($expected[$i] === '%') {
2022-11-17 10:34:54 +00:00
Preg::isMatchStrictGroups('{%(.+?)%}', substr($expected, $i), $match);
2021-02-12 10:05:13 +00:00
$regex = $match[1];
if (Preg::isMatch('{'.$regex.'}', substr($output, $j), $match)) {
2021-02-12 10:05:13 +00:00
$i += strlen($regex) + 2;
2022-11-17 10:34:54 +00:00
$j += strlen((string) $match[0]);
2021-02-12 10:05:13 +00:00
continue;
} else {
$this->fail(
'Failed to match pattern '.$regex.' at line '.$line.' / abs offset '.$i.': '
2021-02-25 12:46:52 +00:00
.substr($output, $j, min(strpos($output, "\n", $j) - $j, 100)).PHP_EOL.PHP_EOL.
2021-02-12 10:05:13 +00:00
'Output:'.PHP_EOL.$output
);
}
}
if ($expected[$i] !== $output[$j]) {
$this->fail(
'Output does not match expectation at line '.$line.' / abs offset '.$i.': '.PHP_EOL
2021-02-25 12:46:52 +00:00
.'-'.substr($expected, $i, min(strpos($expected, "\n", $i) - $i, 100)).PHP_EOL
.'+'.substr($output, $j, min(strpos($output, "\n", $j) - $j, 100)).PHP_EOL.PHP_EOL
2021-02-12 10:05:13 +00:00
.'Output:'.PHP_EOL.$output
);
}
$i++;
$j++;
}
}
if (isset($testData['EXPECT-REGEX'])) {
2021-12-10 12:14:04 +00:00
$this->assertMatchesRegularExpression($testData['EXPECT-REGEX'], $this->cleanOutput($output));
}
2021-02-12 10:05:13 +00:00
if (isset($testData['EXPECT-REGEXES'])) {
$cleanOutput = $this->cleanOutput($output);
foreach (explode("\n", $testData['EXPECT-REGEXES']) as $regex) {
2021-12-10 12:14:04 +00:00
$this->assertMatchesRegularExpression($regex, $cleanOutput, 'Output: '.$output);
2021-02-12 10:05:13 +00:00
}
}
if (isset($testData['EXPECT-EXIT-CODE'])) {
2021-12-10 12:14:04 +00:00
$this->assertSame($testData['EXPECT-EXIT-CODE'], $exitCode);
}
}
2021-10-15 14:26:22 +00:00
/**
* @return array<string, array<string>>
*/
public static function getTestFiles(): array
{
2022-08-17 12:20:07 +00:00
$tests = [];
foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) {
2022-08-17 12:20:07 +00:00
$tests[$file->getFilename()] = [(string) $file];
}
return $tests;
}
2021-10-15 14:26:22 +00:00
/**
2021-12-10 12:14:04 +00:00
* @return array{RUN: string, EXPECT?: string, EXPECT-EXIT-CODE?: int, EXPECT-REGEX?: string, EXPECT-REGEXES?: string, TEST?: string}
2021-10-15 14:26:22 +00:00
*/
2022-02-22 15:47:09 +00:00
private function parseTestFile(string $file): array
{
$tokens = Preg::split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file), -1, PREG_SPLIT_DELIM_CAPTURE);
2022-08-17 12:20:07 +00:00
$data = [];
$section = null;
foreach ($tokens as $token) {
if ('' === $token && null === $section) {
continue;
}
// Handle section headers.
if (null === $section) {
$section = $token;
continue;
}
$sectionData = $token;
// Allow sections to validate, or modify their section data.
switch ($section) {
case 'EXPECT-EXIT-CODE':
2017-03-08 14:07:29 +00:00
$sectionData = (int) $sectionData;
break;
2021-02-12 10:05:13 +00:00
case 'RUN':
case 'EXPECT':
case 'EXPECT-REGEX':
2021-02-12 10:05:13 +00:00
case 'EXPECT-REGEXES':
$sectionData = trim($sectionData);
break;
case 'TEST':
break;
default:
throw new \RuntimeException(sprintf(
2021-02-12 10:05:13 +00:00
'Unknown section "%s". Allowed sections: "RUN", "EXPECT", "EXPECT-EXIT-CODE", "EXPECT-REGEX", "EXPECT-REGEXES". '
.'Section headers must be written as "--HEADER_NAME--".',
2020-11-22 13:48:56 +00:00
$section
));
}
$data[$section] = $sectionData;
$section = $sectionData = null;
}
// validate data
if (!isset($data['RUN'])) {
throw new \RuntimeException('The test file must have a section named "RUN".');
}
2021-02-12 10:05:13 +00:00
if (!isset($data['EXPECT']) && !isset($data['EXPECT-REGEX']) && !isset($data['EXPECT-REGEXES'])) {
throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-REGEX", or "EXPECT-REGEXES".');
}
return $data; // @phpstan-ignore return.type
}
2022-02-22 15:47:09 +00:00
private function cleanOutput(string $output): string
{
$processed = '';
for ($i = 0; $i < strlen($output); $i++) {
if ($output[$i] === "\x08") {
$processed = substr($processed, 0, -1);
} elseif ($output[$i] !== "\r") {
$processed .= $output[$i];
}
}
return $processed;
}
2012-08-18 14:22:15 +00:00
}