From 5818d51af0b035aa8c5b21b20843db40bb05b2c5 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 14 Aug 2012 01:16:40 +0200 Subject: [PATCH] Refactor functional tests to fit existing tests better --- .../functional/create_project_command.test | 12 ++ tests/Composer/Test/Fixtures/installer/SAMPLE | 4 +- .../Test/Fixtures/installer/update-all.test | 2 +- .../Test/Functional/AllFunctionalTest.php | 125 --------------- .../create_project_command.test | 5 - tests/Composer/Test/FunctionalTest.php | 146 ++++++++++++++++++ tests/Composer/Test/InstallerTest.php | 2 +- 7 files changed, 163 insertions(+), 133 deletions(-) create mode 100644 tests/Composer/Test/Fixtures/functional/create_project_command.test delete mode 100755 tests/Composer/Test/Functional/AllFunctionalTest.php delete mode 100755 tests/Composer/Test/Functional/CreateProjectCommand/create_project_command.test create mode 100644 tests/Composer/Test/FunctionalTest.php diff --git a/tests/Composer/Test/Fixtures/functional/create_project_command.test b/tests/Composer/Test/Fixtures/functional/create_project_command.test new file mode 100644 index 000000000..453980602 --- /dev/null +++ b/tests/Composer/Test/Fixtures/functional/create_project_command.test @@ -0,0 +1,12 @@ +--RUN-- +create-project seld/jsonlint %testDir% 1.0.0 +--EXPECT-- +Installing seld/jsonlint (1.0.0) + - Installing seld/jsonlint (1.0.0) + Downloading: 100% + +Created project in %testDir% +Installing dependencies +Nothing to install or update +Writing lock file +Generating autoload files diff --git a/tests/Composer/Test/Fixtures/installer/SAMPLE b/tests/Composer/Test/Fixtures/installer/SAMPLE index 90afe7058..47b29f62c 100644 --- a/tests/Composer/Test/Fixtures/installer/SAMPLE +++ b/tests/Composer/Test/Fixtures/installer/SAMPLE @@ -8,9 +8,11 @@ --INSTALLED-- ---INSTALLED:DEV-- +--INSTALLED-DEV-- --RUN-- install +--EXPECT-LOCK-- + --EXPECT-- \ No newline at end of file diff --git a/tests/Composer/Test/Fixtures/installer/update-all.test b/tests/Composer/Test/Fixtures/installer/update-all.test index b5806687e..35a2e9337 100644 --- a/tests/Composer/Test/Fixtures/installer/update-all.test +++ b/tests/Composer/Test/Fixtures/installer/update-all.test @@ -32,7 +32,7 @@ Updates updateable packages { "name": "a/a", "version": "1.0.0" }, { "name": "a/c", "version": "1.0.0" } ] ---INSTALLED:DEV-- +--INSTALLED-DEV-- [ { "name": "a/b", "version": "1.0.0" } ] diff --git a/tests/Composer/Test/Functional/AllFunctionalTest.php b/tests/Composer/Test/Functional/AllFunctionalTest.php deleted file mode 100755 index 7ad615947..000000000 --- a/tests/Composer/Test/Functional/AllFunctionalTest.php +++ /dev/null @@ -1,125 +0,0 @@ -parseTestFile($testFile); - - $cmd = 'php '.__DIR__.'/../../../../bin/composer '.$testData['command']; - $proc = new Process($cmd); - $exitcode = $proc->run(); - - if (isset($testData['output'])) { - $this->assertEquals($testData['output'], $proc->getOutput()); - } - if (isset($testData['outputregex'])) { - $this->assertRegExp($testData['outputregex'], $proc->getOutput()); - } - if (isset($testData['erroroutput'])) { - $this->assertEquals($testData['erroroutput'], $proc->getErrorOutput()); - } - if (isset($testData['erroroutputregex'])) { - $this->assertRegExp($testData['erroroutputregex'], $proc->getErrorOutput()); - } - if (isset($testData['exitcode'])) { - $this->assertSame($testData['exitcode'], $exitcode); - } - - // Clean up. - $fs = new Filesystem(); - if (isset($testData['test_dir']) && is_dir($testData['test_dir'])) { - $fs->removeDirectory($testData['test_dir']); - } - } - - public function getTestFiles() - { - $tests = array(); - foreach (Finder::create()->in(__DIR__)->name('*.test')->files() as $file) { - $tests[] = array($file); - } - - return $tests; - } - - private function parseTestFile(\SplFileInfo $file) - { - $tokens = preg_split('#(?:^|\n\n)-- ([a-zA-Z]+) --\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE); - $data = array(); - $section = null; - - $varRegex = '#%([^%]+)%#'; - $variableReplacer = function($match) use (&$data) { - list(, $var) = $match; - - switch ($var) { - case 'testDir': - $testDir = sys_get_temp_dir().'/composer_functional_test'.uniqid(mt_rand(), true); - $data['test_dir'] = $testDir; - - return $testDir; - - default: - throw new \InvalidArgumentException(sprintf('Unknown variable "%s". Supported variables: "testDir"', $var)); - } - }; - - for ($i=0,$c=count($tokens); $i<$c; $i++) { - if ('' === $tokens[$i] && null === $section) { - continue; - } - - // Handle section headers. - if (null === $section) { - $section = strtolower($tokens[$i]); - continue; - } - - $sectionData = $tokens[$i]; - - // Allow sections to validate, or modify their section data. - switch ($section) { - case 'command': - $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData); - break; - - case 'exitcode': - $sectionData = (integer) $sectionData; - - case 'erroroutputregex': - case 'outputregex': - case 'erroroutput': - case 'output': - $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData); - break; - - default: - throw new \RuntimeException(sprintf('Unknown section "%s". Allowed sections: "command", "output", "erroroutput", "exitcode", "outputregex", "erroroutputregex". ' - .'Section headers must be written as "-- HEADER_NAME --" and be preceded by an empty line if not at the start of the file.', $section)); - } - - $data[$section] = $sectionData; - $section = $sectionData = null; - } - - // validate data - if (!isset($data['command'])) { - throw new \RuntimeException('The test file must have a section named "COMMAND".'); - } - if (!isset($data['output']) && !isset($data['erroroutput']) && !isset($data['outputregex']) && !isset($data['erroroutputregex'])) { - throw new \RuntimeException('The test file must have a section named "OUTPUT", "ERROROUTPUT", "OUTPUTREGEX", or "ERROROUTPUTREGEX".'); - } - - return $data; - } -} \ No newline at end of file diff --git a/tests/Composer/Test/Functional/CreateProjectCommand/create_project_command.test b/tests/Composer/Test/Functional/CreateProjectCommand/create_project_command.test deleted file mode 100755 index d36540197..000000000 --- a/tests/Composer/Test/Functional/CreateProjectCommand/create_project_command.test +++ /dev/null @@ -1,5 +0,0 @@ --- COMMAND -- -create-project jms/metadata %testDir% - --- OUTPUTREGEX -- -#^Installing jms/metadata \(dev-master [a-f0-9]{40}\)# \ No newline at end of file diff --git a/tests/Composer/Test/FunctionalTest.php b/tests/Composer/Test/FunctionalTest.php new file mode 100644 index 000000000..66161498c --- /dev/null +++ b/tests/Composer/Test/FunctionalTest.php @@ -0,0 +1,146 @@ +parseTestFile($testFile); + + $cmd = 'php '.__DIR__.'/../../../bin/composer --no-ansi '.$testData['RUN']; + $proc = new Process($cmd); + $exitcode = $proc->run(); + + if (isset($testData['EXPECT'])) { + $this->assertEquals($testData['EXPECT'], $this->cleanOutput($proc->getOutput())); + } + if (isset($testData['EXPECT-REGEX'])) { + $this->assertRegExp($testData['EXPECT-REGEX'], $this->cleanOutput($proc->getOutput())); + } + if (isset($testData['EXPECT-ERROR'])) { + $this->assertEquals($testData['EXPECT-ERROR'], $this->cleanOutput($proc->getErrorOutput())); + } + if (isset($testData['EXPECT-ERROR-REGEX'])) { + $this->assertRegExp($testData['EXPECT-ERROR-REGEX'], $this->cleanOutput($proc->getErrorOutput())); + } + if (isset($testData['EXPECT-EXIT-CODE'])) { + $this->assertSame($testData['EXPECT-EXIT-CODE'], $exitcode); + } + + // Clean up. + $fs = new Filesystem(); + if (isset($testData['test_dir']) && is_dir($testData['test_dir'])) { + $fs->removeDirectory($testData['test_dir']); + } + } + + public function getTestFiles() + { + $tests = array(); + foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) { + $tests[] = array($file); + } + + return $tests; + } + + private function parseTestFile(\SplFileInfo $file) + { + $tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE); + $data = array(); + $section = null; + + $testDir = sys_get_temp_dir().'/composer_functional_test'.uniqid(mt_rand(), true); + $varRegex = '#%([a-zA-Z_-]+)%#'; + $variableReplacer = function($match) use (&$data, $testDir) { + list(, $var) = $match; + + switch ($var) { + case 'testDir': + $data['test_dir'] = $testDir; + + return $testDir; + + default: + throw new \InvalidArgumentException(sprintf('Unknown variable "%s". Supported variables: "testDir"', $var)); + } + }; + + for ($i = 0, $c = count($tokens); $i < $c; $i++) { + if ('' === $tokens[$i] && null === $section) { + continue; + } + + // Handle section headers. + if (null === $section) { + $section = $tokens[$i]; + continue; + } + + $sectionData = $tokens[$i]; + + // Allow sections to validate, or modify their section data. + switch ($section) { + case 'RUN': + $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData); + break; + + case 'EXPECT-EXIT-CODE': + $sectionData = (integer) $sectionData; + + case 'EXPECT': + case 'EXPECT-REGEX': + case 'EXPECT-ERROR': + case 'EXPECT-ERROR-REGEX': + $sectionData = preg_replace_callback($varRegex, $variableReplacer, $sectionData); + break; + + default: + throw new \RuntimeException(sprintf( + 'Unknown section "%s". Allowed sections: "RUN", "EXPECT", "EXPECT-ERROR", "EXPECT-EXIT-CODE", "EXPECT-REGEX", "EXPECT-ERROR-REGEX". ' + .'Section headers must be written as "--HEADER_NAME--".', + $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".'); + } + if (!isset($data['EXPECT']) && !isset($data['EXPECT-ERROR']) && !isset($data['EXPECT-REGEX']) && !isset($data['EXPECT-ERROR-REGEX'])) { + throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-ERROR", "EXPECT-REGEX", or "EXPECT-ERROR-REGEX".'); + } + + return $data; + } + + private function cleanOutput($output) + { + $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; + } +} \ No newline at end of file diff --git a/tests/Composer/Test/InstallerTest.php b/tests/Composer/Test/InstallerTest.php index d10b5778d..ebb684894 100644 --- a/tests/Composer/Test/InstallerTest.php +++ b/tests/Composer/Test/InstallerTest.php @@ -247,7 +247,7 @@ class InstallerTest extends TestCase --COMPOSER--\s*(?P'.$content.')\s* (?:--LOCK--\s*(?P'.$content.'))?\s* (?:--INSTALLED--\s*(?P'.$content.'))?\s* - (?:--INSTALLED:DEV--\s*(?P'.$content.'))?\s* + (?:--INSTALLED-DEV--\s*(?P'.$content.'))?\s* --RUN--\s*(?P.*?)\s* (?:--EXPECT-LOCK--\s*(?P'.$content.'))?\s* --EXPECT--\s*(?P.*?)\s*