Changeset View
Changeset View
Standalone View
Standalone View
src/unit/engine/NoseTestEngine.php
| Show All 12 Lines | public function run() { | ||||
| $paths = $this->getPaths(); | $paths = $this->getPaths(); | ||||
| $affected_tests = array(); | $affected_tests = array(); | ||||
| foreach ($paths as $path) { | foreach ($paths as $path) { | ||||
| $absolute_path = Filesystem::resolvePath($path); | $absolute_path = Filesystem::resolvePath($path); | ||||
| if (is_dir($absolute_path)) { | if (is_dir($absolute_path)) { | ||||
| $absolute_test_path = Filesystem::resolvePath("tests/".$path); | $absolute_test_path = Filesystem::resolvePath('tests/'.$path); | ||||
| if (is_readable($absolute_test_path)) { | if (is_readable($absolute_test_path)) { | ||||
| $affected_tests[] = $absolute_test_path; | $affected_tests[] = $absolute_test_path; | ||||
| } | } | ||||
| } | } | ||||
| if (is_readable($absolute_path)) { | if (is_readable($absolute_path)) { | ||||
| $filename = basename($path); | $filename = basename($path); | ||||
| $directory = dirname($path); | $directory = dirname($path); | ||||
| // assumes directory layout: tests/<package>/test_<module>.py | // assumes directory layout: tests/<package>/test_<module>.py | ||||
| $relative_test_path = "tests/".$directory."/test_".$filename; | $relative_test_path = 'tests/'.$directory.'/test_'.$filename; | ||||
| $absolute_test_path = Filesystem::resolvePath($relative_test_path); | $absolute_test_path = Filesystem::resolvePath($relative_test_path); | ||||
| if (is_readable($absolute_test_path)) { | if (is_readable($absolute_test_path)) { | ||||
| $affected_tests[] = $absolute_test_path; | $affected_tests[] = $absolute_test_path; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | foreach (Futures($futures)->limit(4) as $test_path => $future) { | ||||
| $xunit_tmp, | $xunit_tmp, | ||||
| $cover_tmp); | $cover_tmp); | ||||
| } | } | ||||
| return array_mergev($results); | return array_mergev($results); | ||||
| } | } | ||||
| public function buildTestFuture($path, $xunit_tmp, $cover_tmp) { | public function buildTestFuture($path, $xunit_tmp, $cover_tmp) { | ||||
| $cmd_line = csprintf("nosetests --with-xunit --xunit-file=%s", | $cmd_line = csprintf('nosetests --with-xunit --xunit-file=%s', | ||||
| $xunit_tmp); | $xunit_tmp); | ||||
| if ($this->getEnableCoverage() !== false) { | if ($this->getEnableCoverage() !== false) { | ||||
| $cmd_line .= csprintf(" --with-coverage --cover-xml " . | $cmd_line .= csprintf(' --with-coverage --cover-xml ' . | ||||
| "--cover-xml-file=%s", | '--cover-xml-file=%s', | ||||
| $cover_tmp); | $cover_tmp); | ||||
| } | } | ||||
| return new ExecFuture("%C %s", $cmd_line, $path); | return new ExecFuture('%C %s', $cmd_line, $path); | ||||
| } | } | ||||
| public function parseTestResults($source_path, $xunit_tmp, $cover_tmp) { | public function parseTestResults($source_path, $xunit_tmp, $cover_tmp) { | ||||
| $results = $this->parser->parseTestResults( | $results = $this->parser->parseTestResults( | ||||
| Filesystem::readFile($xunit_tmp)); | Filesystem::readFile($xunit_tmp)); | ||||
| // coverage is for all testcases in the executed $path | // coverage is for all testcases in the executed $path | ||||
| if ($this->getEnableCoverage() !== false) { | if ($this->getEnableCoverage() !== false) { | ||||
| $coverage = $this->readCoverage($cover_tmp, $source_path); | $coverage = $this->readCoverage($cover_tmp, $source_path); | ||||
| foreach ($results as $result) { | foreach ($results as $result) { | ||||
| $result->setCoverage($coverage); | $result->setCoverage($coverage); | ||||
| } | } | ||||
| } | } | ||||
| return $results; | return $results; | ||||
| } | } | ||||
| public function readCoverage($cover_file, $source_path) { | public function readCoverage($cover_file, $source_path) { | ||||
| $coverage_dom = new DOMDocument(); | $coverage_dom = new DOMDocument(); | ||||
| $coverage_dom->loadXML(Filesystem::readFile($cover_file)); | $coverage_dom->loadXML(Filesystem::readFile($cover_file)); | ||||
| $reports = array(); | $reports = array(); | ||||
| $classes = $coverage_dom->getElementsByTagName("class"); | $classes = $coverage_dom->getElementsByTagName('class'); | ||||
| foreach ($classes as $class) { | foreach ($classes as $class) { | ||||
| $path = $class->getAttribute("filename"); | $path = $class->getAttribute('filename'); | ||||
| $root = $this->getWorkingCopy()->getProjectRoot(); | $root = $this->getWorkingCopy()->getProjectRoot(); | ||||
| if (!Filesystem::isDescendant($path, $root)) { | if (!Filesystem::isDescendant($path, $root)) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| // get total line count in file | // get total line count in file | ||||
| $line_count = count(phutil_split_lines(Filesystem::readFile($path))); | $line_count = count(phutil_split_lines(Filesystem::readFile($path))); | ||||
| $coverage = ""; | $coverage = ''; | ||||
| $start_line = 1; | $start_line = 1; | ||||
| $lines = $class->getElementsByTagName("line"); | $lines = $class->getElementsByTagName('line'); | ||||
| for ($ii = 0; $ii < $lines->length; $ii++) { | for ($ii = 0; $ii < $lines->length; $ii++) { | ||||
| $line = $lines->item($ii); | $line = $lines->item($ii); | ||||
| $next_line = intval($line->getAttribute("number")); | $next_line = intval($line->getAttribute('number')); | ||||
| for ($start_line; $start_line < $next_line; $start_line++) { | for ($start_line; $start_line < $next_line; $start_line++) { | ||||
| $coverage .= "N"; | $coverage .= 'N'; | ||||
| } | } | ||||
| if (intval($line->getAttribute("hits")) == 0) { | if (intval($line->getAttribute('hits')) == 0) { | ||||
| $coverage .= "U"; | $coverage .= 'U'; | ||||
| } else if (intval($line->getAttribute("hits")) > 0) { | } else if (intval($line->getAttribute('hits')) > 0) { | ||||
| $coverage .= "C"; | $coverage .= 'C'; | ||||
| } | } | ||||
| $start_line++; | $start_line++; | ||||
| } | } | ||||
| if ($start_line < $line_count) { | if ($start_line < $line_count) { | ||||
| foreach (range($start_line, $line_count) as $line_num) { | foreach (range($start_line, $line_count) as $line_num) { | ||||
| $coverage .= "N"; | $coverage .= 'N'; | ||||
| } | } | ||||
| } | } | ||||
| $reports[$path] = $coverage; | $reports[$path] = $coverage; | ||||
| } | } | ||||
| return $reports; | return $reports; | ||||
| } | } | ||||
| } | } | ||||