com phpruntests: Fixing the parser to deal with empty sections: src/taskScheduler/rtTaskSchedulerFile.php src/testcase/rtPhpRunner.php
src/testcase/rtPhpTest.php src/testcase/rtPhpTestFile.php src/testcase/sections/executablesections/rtCleanSection.php
src/testcase/sections/executablesections/rtFileSection.php src/testcase/sections/executablesections/rtSkipIfSection.php
src/testgroup/rtPhpTestGroup.php src/testrun/rtPhpTestRun.php
| From: | David Soria Parra | Date: | Wed, 15 Jul 2009 10:35:04 +0000 |
| Subject: | com phpruntests: Fixing the parser to deal with empty sections: src/taskScheduler/rtTaskSchedulerFile.php src/testcase/rtPhpRunner.php src/testcase/rtPhpTest.php src/testcase/rtPhpTestFile.php src/testcase/sections/executablesections/rtCleanSection.php src/testcase/sections/executablesections/rtFileSection.php src/testcase/sections/executablesections/rtSkipIfSection.php src/testgroup/rtPhpTestGroup.php src/testrun/rtPhpTestRun.php |
||
| Groups: | php.qa | ||
| Request: | Send a blank email to php-qa+get-66346@lists.php.net to get a copy of this message | ||
Commit: 892ae11e82f4e559675028cb37aba0012bc89d59
Author: Zoe Slattery <zoe@php.net> Wed, 15 Jul 2009 10:35:04 +0000
Parents: f93756873ba52431f7fb2e236b299b0d55bb6d7a
Branches: master
Link: http://git.php.net/?p=phpruntests.git;a=commitdiff;h=892ae11e82f4e559675028cb37aba0012bc89d59
Log:
Fixing the parser to deal with empty sections
Changed paths:
M src/taskScheduler/rtTaskSchedulerFile.php
M src/testcase/rtPhpRunner.php
M src/testcase/rtPhpTest.php
M src/testcase/rtPhpTestFile.php
M src/testcase/sections/executablesections/rtCleanSection.php
M src/testcase/sections/executablesections/rtFileSection.php
M src/testcase/sections/executablesections/rtSkipIfSection.php
M src/testgroup/rtPhpTestGroup.php
M src/testrun/rtPhpTestRun.php
diff --git a/src/taskScheduler/rtTaskSchedulerFile.php b/src/taskScheduler/rtTaskSchedulerFile.php index ec4d4e7..15ca5ad 100644 --- a/src/taskScheduler/rtTaskSchedulerFile.php +++ b/src/taskScheduler/rtTaskSchedulerFile.php @@ -21,18 +21,6 @@ class rtTaskSchedulerFile extends rtTaskScheduler private $groupTasks = false; // are the tasks stored in groups? - /** - * the signal-handler is called by the interrupt- or quit-signal. this is - * necessary to cleanup the tmp files and terminate the script correct. - * - * @param int $signal - */ - public static function signalHandler($signal) - { - exit(0); - } - - /** * sets the task-list which has to be an array of task-objects. * it's also possible to use a multidimensional array. in this case the @@ -88,8 +76,6 @@ class rtTaskSchedulerFile extends rtTaskScheduler if ($this->processCount > sizeof($this->taskList)) { $this->processCount = sizeof($this->taskList); } - - // distribute the task to the children $this->distributeTasks(); @@ -113,14 +99,7 @@ class rtTaskSchedulerFile extends rtTaskScheduler break; } } - - - // register signal-handler - /* - pcntl_signal(SIGINT, "rtTaskSchedulerFile::signalHandler"); - pcntl_signal(SIGQUIT, "rtTaskSchedulerFile::signalHandler"); - */ - + // wait until all child-processes are terminated for ($i=0; $i<$this->processCount; $i++) { pcntl_waitpid($this->pidStore[$i], $status); @@ -195,8 +174,6 @@ class rtTaskSchedulerFile extends rtTaskScheduler unlink(self::TMP_FILE.$cid); } - - $this->pidStore = array(); } diff --git a/src/testcase/rtPhpRunner.php b/src/testcase/rtPhpRunner.php index 7a79b06..e7aacb8 100644 --- a/src/testcase/rtPhpRunner.php +++ b/src/testcase/rtPhpRunner.php @@ -58,8 +58,6 @@ class rtPhpRunner fclose($pipes[0]); - $counter = 0; - while (true) { /* hide errors from interrupted syscalls */ $r = $pipes; @@ -69,19 +67,14 @@ class rtPhpRunner $n = @stream_select($r, $w, $e, $this->timeOut); if ($n === false) { - throw new rtException('Stream select failure in rtPhpRunner'); + throw new rtPhpRunnerException('Stream select failure in rtPhpRunner'); } else if ($n === 0) { proc_terminate($proc); - throw new rtException('The process running test code has timed out in rtPhpRunner'); + throw new rtPhpRunnerException ('The process running test code has timed out in rtPhpRunner'); } else if ($n > 0) { - $counter++; - if ($counter > 100) { - proc_terminate($proc); - throw new rtException('The process running test code has timed out in rtPhpRunner'); - } - $line = fread($pipes[1], 8192); + $line = fread($pipes[1], 8192); if (strlen($line) == 0) { - // EOF + /* EOF */ break; } $data .= (binary) $line; diff --git a/src/testcase/rtPhpTest.php b/src/testcase/rtPhpTest.php index 9662db2..b573637 100644 --- a/src/testcase/rtPhpTest.php +++ b/src/testcase/rtPhpTest.php @@ -42,7 +42,7 @@ class rtPhpTest { $lastSection = end($this->sectionHeadings); $start=0; - + foreach($this->sectionHeadings as $keyNumber => $sectionKey) { if($keyNumber < count($this->sectionHeadings) - 1) { @@ -53,9 +53,9 @@ class rtPhpTest for($index=$start; $index<count($this->contents); $index++) if($this->contents[$index] == "--".$sectionKey."--") { //Found the beginning of the section - + for($contentsLine=$index + 1; $contentsLine<count($this->contents); $contentsLine ++) { - + if( ($this->contents[$contentsLine] == "--".$nextKey."--") || ($contentsLine == count($this->contents))) { //Found the end of the section OR the end of the test $start = $contentsLine - 1; @@ -82,7 +82,6 @@ class rtPhpTest //Identify the file and expect section types $this->fileSection = $this->setFileSection(); $this->expectSection = $this->setExpectSection(); - $this->fileSection->setExecutableFileName($this->getName()); } diff --git a/src/testcase/rtPhpTestFile.php b/src/testcase/rtPhpTestFile.php index 05d1bd2..e9a7306 100644 --- a/src/testcase/rtPhpTestFile.php +++ b/src/testcase/rtPhpTestFile.php @@ -29,6 +29,21 @@ class rtPhpTestFile 'rtIsValidSectionName', 'rtIsSectionImplemented' ); + + private function isSectionHeading($string) { + return preg_match("/^\s*--[A-Z]+(_[A-Z]+|)--/", $string); + } + + private function getUntrimmedSectionHeading($string) { + preg_match("/^\s*(--[A-Z]+(_[A-Z]+|)--)/", $string, $matches); + return $matches[1]; + } + + private function getSectionHeading($string) { + preg_match("/^\s*--([A-Z]+(_[A-Z]+|))--/", $string, $matches); + return $matches[1]; + } + /** * Reads the contents of the test file and creates an array of the contents. @@ -42,18 +57,48 @@ class rtPhpTestFile $this->testContents = file($this->testFileName); } + /* + * Trims the lines endings - if the line is a section header it gets trimmed to either side of the --. + * This is to avoid problems with tests that have spurious characters after the header. + */ public function normaliseLineEndings() { for ($i=0; $i<count($this->testContents); $i++) { - //This is not nice but there are a huge number of tests with random spacs at the end of the section header - if (preg_match("/^\s*--([A-Z]+(_[A-Z]+|))--/", $this->testContents[$i], $matches)) { - $this->sectionHeadings[] = $matches[1]; - $this->testContents[$i] = '--' . $matches[1] . '--'; - } else { + //Just trim the contents lines here not the section header lines + if ($this->isSectionHeading($this->testContents[$i])) { + $this->testContents[$i] = $this->getUntrimmedSectionHeading($this->testContents[$i]); + }else { $this->testContents[$i] = rtrim($this->testContents[$i], $this->carriageReturn.$this->newLine); } } } + + /* + * Removes and discards any empty test sections + * Constructs a list of section headingg, stripped of their -- identifiers. + */ + public function removeEmptySections() { + $tempArray = array(); + + for ($i=0; $i<count($this->testContents) - 1; $i++) { + $nextLine = $this->testContents[$i+1]; + $thisLine = $this->testContents[$i]; + if ($this->isSectionHeading($thisLine)) { + if (!$this->isSectionHeading($nextLine)) { + $tempArray[] = $this->getUntrimmedSectionHeading($thisLine); + $this->sectionHeadings[] = $this->getSectionHeading($thisLine); + } + } else { + $tempArray[] = $thisLine; + } + } + + if($this->isSectionHeading(end($this->testContents))) { + $this->sectionHeadings[] = $this->getSectionHeading(end($this->testContents)); + } + $tempArray[] = end($this->testContents); + $this->testContents = $tempArray; + } public function arePreConditionsMet() { @@ -94,5 +139,6 @@ class rtPhpTestFile { return $this->testExitMessage; } + } ?> diff --git a/src/testcase/sections/executablesections/rtCleanSection.php b/src/testcase/sections/executablesections/rtCleanSection.php index 89d1e38..ace4959 100644 --- a/src/testcase/sections/executablesections/rtCleanSection.php +++ b/src/testcase/sections/executablesections/rtCleanSection.php @@ -43,7 +43,7 @@ class rtCleanSection extends rtExecutableSection $testStatus->setMessage('fail_clean','Execution of clean section failed: '.trim($this->output) ); } - } catch (rtException $e) { + } catch (rtPhpRunnerException $e) { $testStatus->setTrue('fail_clean'); $testStatus->setMessage('fail_clean',$e->getMessage); } diff --git a/src/testcase/sections/executablesections/rtFileSection.php b/src/testcase/sections/executablesections/rtFileSection.php index 5222dc5..dc3f7b5 100644 --- a/src/testcase/sections/executablesections/rtFileSection.php +++ b/src/testcase/sections/executablesections/rtFileSection.php @@ -67,7 +67,7 @@ class rtFileSection extends rtExecutableSection } - } catch (rtException $e) { + } catch (rtPhpRunnerException $e) { $testStatus->setTrue('fail'); $testStatus->setMessage('fail', $e->getMessage() ); } diff --git a/src/testcase/sections/executablesections/rtSkipIfSection.php b/src/testcase/sections/executablesections/rtSkipIfSection.php index 6302ae1..44276e1 100644 --- a/src/testcase/sections/executablesections/rtSkipIfSection.php +++ b/src/testcase/sections/executablesections/rtSkipIfSection.php @@ -51,7 +51,7 @@ class rtSkipIfSection extends rtExecutableSection $testStatus->setMessage('warn', $matches[1]); } } - } catch (rtException $e) { + } catch (rtPhpRunnerException $e) { $testStatus->setTrue('fail_skip'); $testStatus->setMessage('fail_skip', 'Failed to execute skipif section' . $e->getMessage()); } diff --git a/src/testgroup/rtPhpTestGroup.php b/src/testgroup/rtPhpTestGroup.php index 200f4f9..94cfd69 100644 --- a/src/testgroup/rtPhpTestGroup.php +++ b/src/testgroup/rtPhpTestGroup.php @@ -41,6 +41,7 @@ class rtPhpTestGroup $testFile = new rtPhpTestFile(); $testFile->doRead($testFileName); $testFile->normaliseLineEndings(); + $testFile->removeEmptySections(); //The test name is the full path to the test file name without the .phpt diff --git a/src/testrun/rtPhpTestRun.php b/src/testrun/rtPhpTestRun.php index f2e37e6..eb3661f 100644 --- a/src/testrun/rtPhpTestRun.php +++ b/src/testrun/rtPhpTestRun.php @@ -124,7 +124,10 @@ class rtPhpTestRun $testFile = new rtPhpTestFile(); $testFile->doRead($testName); $testFile->normaliseLineEndings(); - + $testFile->removeEmptySections(); + + // var_dump($testFile->getSectionHeadings()); + // var_dump($testFile->getContents()); $testStatus = new rtTestStatus($testFile->getTestName());
diff --git a/src/taskScheduler/rtTaskSchedulerFile.php b/src/taskScheduler/rtTaskSchedulerFile.php index ec4d4e7..15ca5ad 100644 --- a/src/taskScheduler/rtTaskSchedulerFile.php +++ b/src/taskScheduler/rtTaskSchedulerFile.php @@ -21,18 +21,6 @@ class rtTaskSchedulerFile extends rtTaskScheduler private $groupTasks = false; // are the tasks stored in groups? - /** - * the signal-handler is called by the interrupt- or quit-signal. this is - * necessary to cleanup the tmp files and terminate the script correct. - * - * @param int $signal - */ - public static function signalHandler($signal) - { - exit(0); - } - - /** * sets the task-list which has to be an array of task-objects. * it's also possible to use a multidimensional array. in this case the @@ -88,8 +76,6 @@ class rtTaskSchedulerFile extends rtTaskScheduler if ($this->processCount > sizeof($this->taskList)) { $this->processCount = sizeof($this->taskList); } - - // distribute the task to the children $this->distributeTasks(); @@ -113,14 +99,7 @@ class rtTaskSchedulerFile extends rtTaskScheduler break; } } - - - // register signal-handler - /* - pcntl_signal(SIGINT, "rtTaskSchedulerFile::signalHandler"); - pcntl_signal(SIGQUIT, "rtTaskSchedulerFile::signalHandler"); - */ - + // wait until all child-processes are terminated for ($i=0; $i<$this->processCount; $i++) { pcntl_waitpid($this->pidStore[$i], $status); @@ -195,8 +174,6 @@ class rtTaskSchedulerFile extends rtTaskScheduler unlink(self::TMP_FILE.$cid); } - - $this->pidStore = array(); } diff --git a/src/testcase/rtPhpRunner.php b/src/testcase/rtPhpRunner.php index 7a79b06..e7aacb8 100644 --- a/src/testcase/rtPhpRunner.php +++ b/src/testcase/rtPhpRunner.php @@ -58,8 +58,6 @@ class rtPhpRunner fclose($pipes[0]); - $counter = 0; - while (true) { /* hide errors from interrupted syscalls */ $r = $pipes; @@ -69,19 +67,14 @@ class rtPhpRunner $n = @stream_select($r, $w, $e, $this->timeOut); if ($n === false) { - throw new rtException('Stream select failure in rtPhpRunner'); + throw new rtPhpRunnerException('Stream select failure in rtPhpRunner'); } else if ($n === 0) { proc_terminate($proc); - throw new rtException('The process running test code has timed out in rtPhpRunner'); + throw new rtPhpRunnerException ('The process running test code has timed out in rtPhpRunner'); } else if ($n > 0) { - $counter++; - if ($counter > 100) { - proc_terminate($proc); - throw new rtException('The process running test code has timed out in rtPhpRunner'); - } - $line = fread($pipes[1], 8192); + $line = fread($pipes[1], 8192); if (strlen($line) == 0) { - // EOF + /* EOF */ break; } $data .= (binary) $line; diff --git a/src/testcase/rtPhpTest.php b/src/testcase/rtPhpTest.php index 9662db2..b573637 100644 --- a/src/testcase/rtPhpTest.php +++ b/src/testcase/rtPhpTest.php @@ -42,7 +42,7 @@ class rtPhpTest { $lastSection = end($this->sectionHeadings); $start=0; - + foreach($this->sectionHeadings as $keyNumber => $sectionKey) { if($keyNumber < count($this->sectionHeadings) - 1) { @@ -53,9 +53,9 @@ class rtPhpTest for($index=$start; $index<count($this->contents); $index++) if($this->contents[$index] == "--".$sectionKey."--") { //Found the beginning of the section - + for($contentsLine=$index + 1; $contentsLine<count($this->contents); $contentsLine ++) { - + if( ($this->contents[$contentsLine] == "--".$nextKey."--") || ($contentsLine == count($this->contents))) { //Found the end of the section OR the end of the test $start = $contentsLine - 1; @@ -82,7 +82,6 @@ class rtPhpTest //Identify the file and expect section types $this->fileSection = $this->setFileSection(); $this->expectSection = $this->setExpectSection(); - $this->fileSection->setExecutableFileName($this->getName()); } diff --git a/src/testcase/rtPhpTestFile.php b/src/testcase/rtPhpTestFile.php index 05d1bd2..e9a7306 100644 --- a/src/testcase/rtPhpTestFile.php +++ b/src/testcase/rtPhpTestFile.php @@ -29,6 +29,21 @@ class rtPhpTestFile 'rtIsValidSectionName', 'rtIsSectionImplemented' ); + + private function isSectionHeading($string) { + return preg_match("/^\s*--[A-Z]+(_[A-Z]+|)--/", $string); + } + + private function getUntrimmedSectionHeading($string) { + preg_match("/^\s*(--[A-Z]+(_[A-Z]+|)--)/", $string, $matches); + return $matches[1]; + } + + private function getSectionHeading($string) { + preg_match("/^\s*--([A-Z]+(_[A-Z]+|))--/", $string, $matches); + return $matches[1]; + } + /** * Reads the contents of the test file and creates an array of the contents. @@ -42,18 +57,48 @@ class rtPhpTestFile $this->testContents = file($this->testFileName); } + /* + * Trims the lines endings - if the line is a section header it gets trimmed to either side of the --. + * This is to avoid problems with tests that have spurious characters after the header. + */ public function normaliseLineEndings() { for ($i=0; $i<count($this->testContents); $i++) { - //This is not nice but there are a huge number of tests with random spacs at the end of the section header - if (preg_match("/^\s*--([A-Z]+(_[A-Z]+|))--/", $this->testContents[$i], $matches)) { - $this->sectionHeadings[] = $matches[1]; - $this->testContents[$i] = '--' . $matches[1] . '--'; - } else { + //Just trim the contents lines here not the section header lines + if ($this->isSectionHeading($this->testContents[$i])) { + $this->testContents[$i] = $this->getUntrimmedSectionHeading($this->testContents[$i]); + }else { $this->testContents[$i] = rtrim($this->testContents[$i], $this->carriageReturn.$this->newLine); } } } + + /* + * Removes and discards any empty test sections + * Constructs a list of section headingg, stripped of their -- identifiers. + */ + public function removeEmptySections() { + $tempArray = array(); + + for ($i=0; $i<count($this->testContents) - 1; $i++) { + $nextLine = $this->testContents[$i+1]; + $thisLine = $this->testContents[$i]; + if ($this->isSectionHeading($thisLine)) { + if (!$this->isSectionHeading($nextLine)) { + $tempArray[] = $this->getUntrimmedSectionHeading($thisLine); + $this->sectionHeadings[] = $this->getSectionHeading($thisLine); + } + } else { + $tempArray[] = $thisLine; + } + } + + if($this->isSectionHeading(end($this->testContents))) { + $this->sectionHeadings[] = $this->getSectionHeading(end($this->testContents)); + } + $tempArray[] = end($this->testContents); + $this->testContents = $tempArray; + } public function arePreConditionsMet() { @@ -94,5 +139,6 @@ class rtPhpTestFile { return $this->testExitMessage; } + } ?> diff --git a/src/testcase/sections/executablesections/rtCleanSection.php b/src/testcase/sections/executablesections/rtCleanSection.php index 89d1e38..ace4959 100644 --- a/src/testcase/sections/executablesections/rtCleanSection.php +++ b/src/testcase/sections/executablesections/rtCleanSection.php @@ -43,7 +43,7 @@ class rtCleanSection extends rtExecutableSection $testStatus->setMessage('fail_clean','Execution of clean section failed: '.trim($this->output) ); } - } catch (rtException $e) { + } catch (rtPhpRunnerException $e) { $testStatus->setTrue('fail_clean'); $testStatus->setMessage('fail_clean',$e->getMessage); } diff --git a/src/testcase/sections/executablesections/rtFileSection.php b/src/testcase/sections/executablesections/rtFileSection.php index 5222dc5..dc3f7b5 100644 --- a/src/testcase/sections/executablesections/rtFileSection.php +++ b/src/testcase/sections/executablesections/rtFileSection.php @@ -67,7 +67,7 @@ class rtFileSection extends rtExecutableSection } - } catch (rtException $e) { + } catch (rtPhpRunnerException $e) { $testStatus->setTrue('fail'); $testStatus->setMessage('fail', $e->getMessage() ); } diff --git a/src/testcase/sections/executablesections/rtSkipIfSection.php b/src/testcase/sections/executablesections/rtSkipIfSection.php index 6302ae1..44276e1 100644 --- a/src/testcase/sections/executablesections/rtSkipIfSection.php +++ b/src/testcase/sections/executablesections/rtSkipIfSection.php @@ -51,7 +51,7 @@ class rtSkipIfSection extends rtExecutableSection $testStatus->setMessage('warn', $matches[1]); } } - } catch (rtException $e) { + } catch (rtPhpRunnerException $e) { $testStatus->setTrue('fail_skip'); $testStatus->setMessage('fail_skip', 'Failed to execute skipif section' . $e->getMessage()); } diff --git a/src/testgroup/rtPhpTestGroup.php b/src/testgroup/rtPhpTestGroup.php index 200f4f9..94cfd69 100644 --- a/src/testgroup/rtPhpTestGroup.php +++ b/src/testgroup/rtPhpTestGroup.php @@ -41,6 +41,7 @@ class rtPhpTestGroup $testFile = new rtPhpTestFile(); $testFile->doRead($testFileName); $testFile->normaliseLineEndings(); + $testFile->removeEmptySections(); //The test name is the full path to the test file name without the .phpt diff --git a/src/testrun/rtPhpTestRun.php b/src/testrun/rtPhpTestRun.php index f2e37e6..eb3661f 100644 --- a/src/testrun/rtPhpTestRun.php +++ b/src/testrun/rtPhpTestRun.php @@ -124,7 +124,10 @@ class rtPhpTestRun $testFile = new rtPhpTestFile(); $testFile->doRead($testName); $testFile->normaliseLineEndings(); - + $testFile->removeEmptySections(); + + // var_dump($testFile->getSectionHeadings()); + // var_dump($testFile->getContents()); $testStatus = new rtTestStatus($testFile->getTestName());