Changeset View
Changeset View
Standalone View
Standalone View
src/unit/engine/PytestTestEngine.php
| Show First 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | if (empty($coverage_data)) { | ||||
| return array(); | return array(); | ||||
| } | } | ||||
| $coverage_dom = new DOMDocument(); | $coverage_dom = new DOMDocument(); | ||||
| $coverage_dom->loadXML($coverage_data); | $coverage_dom->loadXML($coverage_data); | ||||
| $paths = $this->getPaths(); | $paths = $this->getPaths(); | ||||
| $reports = array(); | $reports = array(); | ||||
| $classes = $coverage_dom->getElementsByTagName("class"); | $classes = $coverage_dom->getElementsByTagName('class'); | ||||
| foreach ($classes as $class) { | foreach ($classes as $class) { | ||||
| // filename is actually python module path with ".py" at the end, | // filename is actually python module path with ".py" at the end, | ||||
| // e.g.: tornado.web.py | // e.g.: tornado.web.py | ||||
| $relative_path = explode(".", $class->getAttribute("filename")); | $relative_path = explode('.', $class->getAttribute('filename')); | ||||
| array_pop($relative_path); | array_pop($relative_path); | ||||
| $relative_path = implode("/", $relative_path); | $relative_path = implode('/', $relative_path); | ||||
| // first we check if the path is a directory (a Python package), if it is | // first we check if the path is a directory (a Python package), if it is | ||||
| // set relative and absolute paths to have __init__.py at the end. | // set relative and absolute paths to have __init__.py at the end. | ||||
| $absolute_path = Filesystem::resolvePath($relative_path); | $absolute_path = Filesystem::resolvePath($relative_path); | ||||
| if (is_dir($absolute_path)) { | if (is_dir($absolute_path)) { | ||||
| $relative_path .= "/__init__.py"; | $relative_path .= '/__init__.py'; | ||||
| $absolute_path .= "/__init__.py"; | $absolute_path .= '/__init__.py'; | ||||
| } | } | ||||
| // then we check if the path with ".py" at the end is file (a Python | // then we check if the path with ".py" at the end is file (a Python | ||||
| // submodule), if it is - set relative and absolute paths to have | // submodule), if it is - set relative and absolute paths to have | ||||
| // ".py" at the end. | // ".py" at the end. | ||||
| if (is_file($absolute_path.".py")) { | if (is_file($absolute_path.'.py')) { | ||||
| $relative_path .= ".py"; | $relative_path .= '.py'; | ||||
| $absolute_path .= ".py"; | $absolute_path .= '.py'; | ||||
| } | } | ||||
| if (!file_exists($absolute_path)) { | if (!file_exists($absolute_path)) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| if (!in_array($relative_path, $paths)) { | if (!in_array($relative_path, $paths)) { | ||||
| continue; | continue; | ||||
| } | } | ||||
| // get total line count in file | // get total line count in file | ||||
| $line_count = count(file($absolute_path)); | $line_count = count(file($absolute_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[$relative_path] = $coverage; | $reports[$relative_path] = $coverage; | ||||
| } | } | ||||
| return $reports; | return $reports; | ||||
| } | } | ||||
| } | } | ||||