1
0
Fork 0

Don't use a regex to parse installer tests to allow for longer tests

pull/3681/head
Nils Adermann 2015-01-27 16:56:39 +01:00
parent 825b4b9c63
commit 5e5eb069dc
1 changed files with 90 additions and 53 deletions

View File

@ -241,10 +241,10 @@ class InstallerTest extends TestCase
} }
$installationManager = $composer->getInstallationManager(); $installationManager = $composer->getInstallationManager();
$this->assertSame($expect, implode("\n", $installationManager->getTrace())); $this->assertSame(rtrim($expect), implode("\n", $installationManager->getTrace()));
if ($expectOutput) { if ($expectOutput) {
$this->assertEquals($expectOutput, $output); $this->assertEquals(rtrim($expectOutput), rtrim($output));
} }
} }
@ -258,21 +258,7 @@ class InstallerTest extends TestCase
continue; continue;
} }
$test = file_get_contents($file->getRealpath()); $testData = $this->readTestFile($file, $fixturesDir);
$content = '(?:.(?!--[A-Z]))+';
$pattern = '{^
--TEST--\s*(?P<test>.*?)\s*
(?:--CONDITION--\s*(?P<condition>'.$content.'))?\s*
--COMPOSER--\s*(?P<composer>'.$content.')\s*
(?:--LOCK--\s*(?P<lock>'.$content.'))?\s*
(?:--INSTALLED--\s*(?P<installed>'.$content.'))?\s*
--RUN--\s*(?P<run>.*?)\s*
(?:--EXPECT-LOCK--\s*(?P<expectLock>'.$content.'))?\s*
(?:--EXPECT-OUTPUT--\s*(?P<expectOutput>'.$content.'))?\s*
(?:--EXPECT-EXIT-CODE--\s*(?P<expectExitCode>\d+))?\s*
--EXPECT--\s*(?P<expect>.*?)\s*
$}xs';
$installed = array(); $installed = array();
$installedDev = array(); $installedDev = array();
@ -280,11 +266,10 @@ class InstallerTest extends TestCase
$expectLock = array(); $expectLock = array();
$expectExitCode = 0; $expectExitCode = 0;
if (preg_match($pattern, $test, $match)) {
try { try {
$message = $match['test']; $message = $testData['TEST'];
$condition = !empty($match['condition']) ? $match['condition'] : null; $condition = !empty($testData['CONDITION']) ? $testData['CONDITION'] : null;
$composer = JsonFile::parseJson($match['composer']); $composer = JsonFile::parseJson($testData['COMPOSER']);
if (isset($composer['repositories'])) { if (isset($composer['repositories'])) {
foreach ($composer['repositories'] as &$repo) { foreach ($composer['repositories'] as &$repo) {
@ -301,32 +286,84 @@ class InstallerTest extends TestCase
} }
} }
if (!empty($match['lock'])) { if (!empty($testData['LOCK'])) {
$lock = JsonFile::parseJson($match['lock']); $lock = JsonFile::parseJson($testData['LOCK']);
if (!isset($lock['hash'])) { if (!isset($lock['hash'])) {
$lock['hash'] = md5(json_encode($composer)); $lock['hash'] = md5(json_encode($composer));
} }
} }
if (!empty($match['installed'])) { if (!empty($testData['INSTALLED'])) {
$installed = JsonFile::parseJson($match['installed']); $installed = JsonFile::parseJson($testData['INSTALLED']);
} }
$run = $match['run']; $run = $testData['RUN'];
if (!empty($match['expectLock'])) { if (!empty($testData['EXPECT-LOCK'])) {
$expectLock = JsonFile::parseJson($match['expectLock']); $expectLock = JsonFile::parseJson($testData['EXPECT-LOCK']);
} }
$expectOutput = $match['expectOutput']; $expectOutput = isset($testData['EXPECT-OUTPUT']) ? $testData['EXPECT-OUTPUT'] : null;
$expect = $match['expect']; $expect = $testData['EXPECT'];
$expectExitCode = (int) $match['expectExitCode']; $expectExitCode = isset($testData['EXPECT-EXIT-CODE']) ? (int) $testData['EXPECT-EXIT-CODE'] : 0;
} catch (\Exception $e) { } catch (\Exception $e) {
die(sprintf('Test "%s" is not valid: '.$e->getMessage(), str_replace($fixturesDir.'/', '', $file))); die(sprintf('Test "%s" is not valid: '.$e->getMessage(), str_replace($fixturesDir.'/', '', $file)));
} }
} else {
die(sprintf('Test "%s" is not valid, did not match the expected format.', str_replace($fixturesDir.'/', '', $file)));
}
$tests[basename($file)] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $composer, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectExitCode); $tests[basename($file)] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $composer, $lock, $installed, $run, $expectLock, $expectOutput, $expect, $expectExitCode);
} }
return $tests; return $tests;
} }
protected function readTestFile(\SplFileInfo $file, $fixturesDir)
{
$tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE);
$sectionInfo = array(
'TEST' => true,
'CONDITION' => false,
'COMPOSER' => true,
'LOCK' => false,
'INSTALLED' => false,
'RUN' => true,
'EXPECT-LOCK' => false,
'EXPECT-OUTPUT' => false,
'EXPECT-EXIT-CODE' => false,
'EXPECT' => true,
);
$section = null;
foreach ($tokens as $i => $token)
{
if (null === $section && empty($token)) {
continue; // skip leading blank
}
if (null === $section) {
if (!isset($sectionInfo[$token])) {
throw new \RuntimeException(sprintf(
'The test file "%s" must not contain a section named "%s".',
str_replace($fixturesDir.'/', '', $file),
$token
));
}
$section = $token;
continue;
}
$sectionData = $token;
$data[$section] = $sectionData;
$section = $sectionData = null;
}
foreach ($sectionInfo as $section => $required) {
if ($required && !isset($data[$section])) {
throw new \RuntimeException(sprintf(
'The test file "%s" must have a section named "%s".',
str_replace($fixturesDir.'/', '', $file),
$section
));
}
}
return $data;
}
} }