From 1c864cebedc6ae5850930caf531bc5202bd2db82 Mon Sep 17 00:00:00 2001 From: corpsee Date: Sat, 23 May 2015 15:40:49 +0600 Subject: [PATCH 1/3] Fixed TapParser parse fail with valid data + added tests --- PHPCI/Plugin/Util/TapParser.php | 13 ++++---- Tests/PHPCI/Plugin/Util/TapParserTest.php | 36 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/PHPCI/Plugin/Util/TapParser.php b/PHPCI/Plugin/Util/TapParser.php index 18772a6a..132c5cce 100644 --- a/PHPCI/Plugin/Util/TapParser.php +++ b/PHPCI/Plugin/Util/TapParser.php @@ -13,9 +13,10 @@ use Symfony\Component\Yaml\Yaml; class TapParser { const TEST_COUNTS_PATTERN = '/^\d+\.\.(\d+)/'; - const TEST_LINE_PATTERN = '/^(ok|not ok)(?:\s+\d+)?(?:\s+\-)?\s*(.*?)(?:\s*#\s*(skip|todo)\s*(.*))?\s*$/i'; - const TEST_YAML_START = '/^(\s*)---/'; - const TEST_DIAGNOSTIC = '/^#/'; + const TEST_LINE_PATTERN = '/^(ok|not ok)(?:\s+\d+)?(?:\s+\-)?\s*(.*?)(?:\s*#\s*(skip|todo)\s*(.*))?\s*$/i'; + const TEST_YAML_START = '/^(\s*)---/'; + const TEST_DIAGNOSTIC = '/^#/'; + const TEST_COVERAGE = '/^Generating/'; /** * @var string @@ -81,7 +82,7 @@ class TapParser $line = $this->nextLine(); } - if (count($this->results) !== $this->testCount) { + if (false !== $this->testCount && count($this->results) !== $this->testCount) { throw new Exception(Lang::get('tap_error')); } @@ -126,13 +127,15 @@ class TapParser /** Parse a single line. * * @param string $line + * + * @throws Exception */ protected function parseLine($line) { if (preg_match(self::TEST_COUNTS_PATTERN, $line, $matches)) { $this->testCount = intval($matches[1]); - } elseif (preg_match(self::TEST_DIAGNOSTIC, $line)) { + } elseif (preg_match(self::TEST_DIAGNOSTIC, $line) || preg_match(self::TEST_COVERAGE, $line) || !$line) { return; } elseif (preg_match(self::TEST_LINE_PATTERN, $line, $matches)) { diff --git a/Tests/PHPCI/Plugin/Util/TapParserTest.php b/Tests/PHPCI/Plugin/Util/TapParserTest.php index 732a44a6..8d31d7c9 100644 --- a/Tests/PHPCI/Plugin/Util/TapParserTest.php +++ b/Tests/PHPCI/Plugin/Util/TapParserTest.php @@ -23,6 +23,28 @@ ok 1 - SomeTest::testAnother not ok 1..2 Trailing garbage ! +TAP; + $parser = new TapParser($content); + $result = $parser->parse(); + + $this->assertEquals(array( + array('pass' => true, 'severity' => 'success', 'message' => 'SomeTest::testAnother'), + array('pass' => false, 'severity' => 'fail', 'message' => ''), + ), $result); + + $this->assertEquals(1, $parser->getTotalFailures()); + } + + public function testSimple2() + { + $content = <<parse(); @@ -48,6 +70,20 @@ TAP; $parser->parse(); } + public function testTapCoverage() + { + $content = <<parse(); + + $this->assertEquals(array(), $result); + } + /** * @expectedException \Exception * @expectedExceptionMessageRegExp /Duplicated TAP/ From c20ee0533bb1ba61573b04a442762b41ac046e11 Mon Sep 17 00:00:00 2001 From: corpsee Date: Sun, 24 May 2015 12:39:27 +0600 Subject: [PATCH 2/3] Fixed TapParser::parseLine Cyclomatic Complexity --- PHPCI/Plugin/Util/TapParser.php | 112 +++++++++++++++++++++++++------- 1 file changed, 88 insertions(+), 24 deletions(-) diff --git a/PHPCI/Plugin/Util/TapParser.php b/PHPCI/Plugin/Util/TapParser.php index 132c5cce..25d3a2a5 100644 --- a/PHPCI/Plugin/Util/TapParser.php +++ b/PHPCI/Plugin/Util/TapParser.php @@ -124,6 +124,79 @@ class TapParser return false; } + /** + * @param string $line + * + * @return boolean + */ + protected function testCountLine($line) + { + if (preg_match(self::TEST_COUNTS_PATTERN, $line, $matches)) { + $this->testCount = intval($matches[1]); + + return true; + } + + return false; + } + + /** + * @param string $line + * + * @return boolean + */ + protected function testLine($line) + { + if (preg_match(self::TEST_LINE_PATTERN, $line, $matches)) { + $this->results[] = $this->processTestLine( + $matches[1], + isset($matches[2]) ? $matches[2] : '', + isset($matches[3]) ? $matches[3] : null, + isset($matches[4]) ? $matches[4] : null + ); + + return true; + } + + return false; + } + + /** + * @param string $line + * + * @return boolean + */ + protected function yamlLine($line) + { + if (preg_match(self::TEST_YAML_START, $line, $matches)) { + $diagnostic = $this->processYamlBlock($matches[1]); + $test = array_pop($this->results); + if (isset($test['message'], $diagnostic['message'])) { + $test['message'] .= PHP_EOL . $diagnostic['message']; + unset($diagnostic['message']); + } + $this->results[] = array_replace($test, $diagnostic); + + return true; + } + + return false; + } + + /** + * @param string $line + * + * @return boolean + */ + protected function garbageLine($line) + { + if (preg_match(self::TEST_DIAGNOSTIC, $line) || preg_match(self::TEST_COVERAGE, $line) || !$line) { + return true; + } + + return false; + } + /** Parse a single line. * * @param string $line @@ -132,32 +205,23 @@ class TapParser */ protected function parseLine($line) { - if (preg_match(self::TEST_COUNTS_PATTERN, $line, $matches)) { - $this->testCount = intval($matches[1]); - - } elseif (preg_match(self::TEST_DIAGNOSTIC, $line) || preg_match(self::TEST_COVERAGE, $line) || !$line) { + if ($this->garbageLine($line)) { return; - - } elseif (preg_match(self::TEST_LINE_PATTERN, $line, $matches)) { - $this->results[] = $this->processTestLine( - $matches[1], - isset($matches[2]) ? $matches[2] : '', - isset($matches[3]) ? $matches[3] : null, - isset($matches[4]) ? $matches[4] : null - ); - - } elseif (preg_match(self::TEST_YAML_START, $line, $matches)) { - $diagnostic = $this->processYamlBlock($matches[1]); - $test = array_pop($this->results); - if (isset($test['message'], $diagnostic['message'])) { - $test['message'] .= PHP_EOL . $diagnostic['message']; - unset($diagnostic['message']); - } - $this->results[] = array_replace($test, $diagnostic); - - } else { - throw new Exception(sprintf('Incorrect TAP data, line %d: %s', $this->lineNumber, $line)); } + + if ($this->testCountLine($line)) { + return; + } + + if ($this->testLine($line)) { + return; + } + + if ($this->yamlLine($line)) { + return; + } + + throw new Exception(sprintf('Incorrect TAP data, line %d: %s', $this->lineNumber, $line)); } /** From 3f02e63c92bd99fe7990a6f9b2aa23f1b978d19b Mon Sep 17 00:00:00 2001 From: corpsee Date: Sun, 24 May 2015 12:43:43 +0600 Subject: [PATCH 3/3] Fixed TapParser::parseLine Cyclomatic Complexity --- PHPCI/Plugin/Util/TapParser.php | 36 ++++----------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/PHPCI/Plugin/Util/TapParser.php b/PHPCI/Plugin/Util/TapParser.php index 25d3a2a5..451b73f0 100644 --- a/PHPCI/Plugin/Util/TapParser.php +++ b/PHPCI/Plugin/Util/TapParser.php @@ -124,22 +124,6 @@ class TapParser return false; } - /** - * @param string $line - * - * @return boolean - */ - protected function testCountLine($line) - { - if (preg_match(self::TEST_COUNTS_PATTERN, $line, $matches)) { - $this->testCount = intval($matches[1]); - - return true; - } - - return false; - } - /** * @param string $line * @@ -183,20 +167,6 @@ class TapParser return false; } - /** - * @param string $line - * - * @return boolean - */ - protected function garbageLine($line) - { - if (preg_match(self::TEST_DIAGNOSTIC, $line) || preg_match(self::TEST_COVERAGE, $line) || !$line) { - return true; - } - - return false; - } - /** Parse a single line. * * @param string $line @@ -205,11 +175,13 @@ class TapParser */ protected function parseLine($line) { - if ($this->garbageLine($line)) { + if (preg_match(self::TEST_DIAGNOSTIC, $line) || preg_match(self::TEST_COVERAGE, $line) || !$line) { return; } - if ($this->testCountLine($line)) { + if (preg_match(self::TEST_COUNTS_PATTERN, $line, $matches)) { + $this->testCount = intval($matches[1]); + return; }