Пример #1
0
class PMDParser(object):
    def __init__(self):
        self.reader = Reader()

    def parse(self, path: str, file_type: str) -> dict:
        return_val = {'warning': 0}
        try:
            if not equals(file_type, 'xml'):
                raise FileExtensionError
            test_result = self.reader.read_xml_file(path)
            data = test_result['pmd']['file']
        except FileExtensionError:
            colored_print(
                'Sorry, there is no information you can get from this type of file.',
                'orange')
        except ValueError:
            pass
        else:
            if equals(isinstance(data, dict), True):
                data = [data]
            for file in data:
                if 'violation' in file:
                    if equals(isinstance(file['violation'], dict), True):
                        file['violation'] = [file['violation']]
                    return_val['warning'] += len(file['violation'])
        finally:
            return return_val
Пример #2
0
class CoverageParser(object):
    def __init__(self):
        self.reader = Reader()

    def parse(self, path: str, file_type: str) -> dict:
        return_val = {'test_val': 0}
        if equals(file_type, 'html'):
            coverage_data = self.reader.read_html_file(path)
            rate = int(coverage_data['html']['body']['div'][2]['table']
                       ['tfoot']['tr']['td'][4]['text'].rstrip('%'))
            return_val['test_val'] = rate
        elif equals(file_type, 'xml'):
            coverage_data = self.reader.read_xml_file(path)
            rate = coverage_data['coverage']['@line-rate'].split('.')[1][:2]
            return_val['test_val'] = int(rate)
        elif equals(file_type, 'json'):
            coverage_data = self.reader.read_json_file(path)
            return_val['test_val'] = int(
                coverage_data['totals']['percent_covered'])
        else:
            colored_print(
                'Sorry, there is no information you can get from this type of file.',
                'orange')
            return_val['test_val'] = -1

        return return_val
Пример #3
0
class UnittestParser(object):
    def __init__(self):
        self.reader = Reader()

    def parse(self, path: str, file_type: str) -> dict:
        return_val = {
            'success': 0,
            'fail': 0,
            'skip': 0
        }
        if file_type == 'xml':
            xml = self.reader.read_xml_file(path)
            data = xml['testsuites']['testsuite']
            total = int(data['@tests'])
            fail = int(data['@failures']) + int(data['@errors'])
            skip = int(data['@skipped'])
            success = total - fail - skip
            return_val['success'] = success
            return_val['fail'] = fail
            return_val['skip'] = skip
        elif file_type == 'html':
            report_list = glob.glob(path + '/*.html')
            for file in report_list:
                html = self.reader.read_html_file(file)
                data = html['html']['body']['div']['div'][0]['div']['p'][2]['text']
                data = [x.rstrip(',').rstrip(':') for x in data.split()]
                r = {}
                for i in range(0, len(data), 2):
                    r[data[i]] = data[i + 1]
                return_val['success'] += int(r['Pass']) if 'Pass' in r else 0
                return_val['fail'] += int(r['Fail']) if 'Fail' in r else 0
                return_val['skip'] += int(r['Skip']) if 'Skip' in r else 0

        return return_val
Пример #4
0
class SeleniumParser(object):
    def __init__(self):
        self.reader = Reader()

    def parse(self, path: str, file_type: str) -> dict:
        return_val: dict = {'success': 0, 'fail': 0, 'skip': 0}
        if file_type == 'xml':
            xml_path = glob.glob(path + '/**/*.xml', recursive=True)
            for xml_file in xml_path:
                selenium_data = self.reader.read_xml_file(xml_file)
                total = int(selenium_data['testsuites']['@tests'])
                fail = int(selenium_data['testsuites']['@failures'])
                success = total - fail
                return_val['success'] += success
                return_val['fail'] += fail
        else:
            colored_print(
                'Sorry, there is no information you can get from this type of file.',
                'orange')

        return return_val
Пример #5
0
class ESLintParser(object):
    def __init__(self):
        self.reader = Reader()

    def parse(self, path: str, file_type: str) -> dict:
        def _checkstyle() -> dict:
            def find_warnings(file_tag: dict):
                if 'error' not in file_tag:
                    return 0
                if isinstance(file_tag['error'], list):
                    return len(file_tag['error'])
                return 1
            xml = self.reader.read_xml_file(path)
            data = xml['checkstyle']['file']
            warnings = 0
            if isinstance(data, list):
                for tag in data:
                    warnings += find_warnings(tag)
            else:
                warnings = find_warnings(data)
            return {
                'warning': warnings
            }

        def _codeframe() -> dict:
            txt = self.reader.read_raw_file(path)
            result_line = txt.splitlines()[-2]
            result = re.sub('[^0-9]', '', result_line)
            return {
                'warning': int(result[0]) + int(result[1])
            }

        def _compact() -> dict:
            txt = self.reader.read_raw_file(path)
            result_line = txt.splitlines()[-1]
            return {
                'warning': int(result_line.split()[0])
            }

        def _junit() -> dict:
            xml = self.reader.read_xml_file(path)
            return {
                'warning': int(xml['testsuites']['testsuite']['@errors'])
            }

        def _jslint_xml() -> dict:
            xml = self.reader.read_xml_file(path)
            return {
                'warning': len(xml['jslint']['file']['issue'])
            }

        def _json() -> dict:
            json = self.reader.read_json_file(path)
            warning = int(json[0]['errorCount']) + int(json[0]['warningCount'])
            return {
                'warning': warning
            }

        def _json_with_metadata() -> dict:
            json = self.reader.read_json_file(path)
            warning = int(json['results'][0]['errorCount']) + int(json['results'][0]['warningCount'])
            return {
                'warning': warning
            }

        if equals(file_type, 'checkstyle'):
            return _checkstyle()
        if equals(file_type, 'codeframe'):
            return _codeframe()
        if equals(file_type, 'compact'):
            return _compact()
        if equals(file_type, 'junit'):
            return _junit()
        if equals(file_type, 'jslint-xml'):
            return _jslint_xml()
        if equals(file_type, 'json'):
            return _json()
        if equals(file_type, 'json-with-metadata'):
            return _json_with_metadata()

        colored_print('Sorry, there is no information you can get from this type of file.', 'orange')