Exemplo n.º 1
0
def compute_coverage(branch):
    coverage_data = CoverageData()
    try:
        with project_path.join('.coverage').open() as fp:
            coverage_data.read_file(fp)
    except Exception:
        print("No coverage data found", file=sys.stderr)

    git_proc = subprocess.Popen(['git', 'diff', '-U0', branch],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
    git_output = git_proc.stdout.read()
    files = git_output.split("diff --git")

    from collections import defaultdict
    file_data = defaultdict(list)

    for the_file in files:
        filenames = re.findall('a/(.*?) b/(.*)', the_file)
        if not filenames:
            continue
        filename = project_path.join(filenames[0][1])
        if '.py' != filename.ext:
            continue
        the_file += "git_output_checker"
        the_diffs = re.findall(
            r'(@@.*?@@.*?(?=@@|git_output_checker))',
            the_file,
            re.M | re.S,
        )
        for diff in the_diffs:
            diff_args = re.match(r'@@ -(\d+)(,(\d+))*\s+\+(\d+)(,(\d+))*',
                                 diff).groups()
            if diff_args[5]:
                for extra_line in range(int(diff_args[5])):
                    file_data[filename].append(extra_line + int(diff_args[3]))
            else:
                file_data[filename].append(int(diff_args[3]))

    line_count = 0
    completed_lines = 0
    for file_changed, lines in file_data.items():
        for line in lines:
            line_count += 1
            used_lines = coverage_data.lines(file_changed)
            if not used_lines:
                continue
            if isinstance(used_lines, int):
                used_lines = set([used_lines])
            else:
                used_lines = set(used_lines)
            if line in used_lines:
                completed_lines += 1

    return float(completed_lines) / line_count * 100
Exemplo n.º 2
0
    def test_note(self):
        self.make_file(".coveragerc", """\
            [run]
            data_file = mydata.dat
            note = These are musical notes: ♫𝅗𝅥♩
            """)
        self.make_file("simple.py", """print('hello')""")
        self.run_command("coverage run simple.py")

        data = CoverageData()
        data.read_file("mydata.dat")
        infos = data.run_infos()
        self.assertEqual(len(infos), 1)
        self.assertEqual(infos[0]['note'], u"These are musical notes: ♫𝅗𝅥♩")
Exemplo n.º 3
0
    def test_note(self):
        self.make_file(".coveragerc", """\
            [run]
            data_file = mydata.dat
            note = These are musical notes: ♫𝅗𝅥♩
            """)
        self.make_file("simple.py", """print('hello')""")
        self.run_command("coverage run simple.py")

        data = CoverageData()
        data.read_file("mydata.dat")
        infos = data.run_infos()
        self.assertEqual(len(infos), 1)
        self.assertEqual(infos[0]['note'], u"These are musical notes: ♫𝅗𝅥♩")
Exemplo n.º 4
0
def compute_coverage(branch):
    coverage_data = CoverageData()
    try:
        with project_path.join('.coverage').open() as fp:
            coverage_data.read_file(fp)
    except Exception:
        print("No coverage data found", file=sys.stderr)

    git_proc = subprocess.Popen(['git', 'diff', '-U0', branch],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    git_output = git_proc.stdout.read()
    files = git_output.split("diff --git")

    from collections import defaultdict
    file_data = defaultdict(list)

    for the_file in files:
        filenames = re.findall('a/(.*?) b/(.*)', the_file)
        if not filenames:
            continue
        filename = project_path.join(filenames[0][1])
        if '.py' != filename.ext:
            continue
        the_file += "git_output_checker"
        the_diffs = re.findall('(@@.*?@@.*?(?=@@|git_output_checker))', the_file, re.M | re.S, )
        for diff in the_diffs:
            diff_args = re.match('@@ -(\d+)(,(\d+))*\s+\+(\d+)(,(\d+))*', diff).groups()
            if diff_args[5]:
                for extra_line in range(int(diff_args[5])):
                    file_data[filename].append(extra_line + int(diff_args[3]))
            else:
                file_data[filename].append(int(diff_args[3]))

    line_count = 0
    completed_lines = 0
    for file_changed, lines in file_data.items():
        for line in lines:
            line_count += 1
            used_lines = coverage_data.lines(file_changed)
            if not used_lines:
                continue
            if isinstance(used_lines, int):
                used_lines = set([used_lines])
            else:
                used_lines = set(used_lines)
            if line in used_lines:
                completed_lines += 1

    return float(completed_lines) / line_count * 100
Exemplo n.º 5
0
def run_setup(setup_script, args):

    # This used to call setuptools.sandbox's run_setup, but due to issues with
    # this and Cython (which caused segmentation faults), we now use subprocess.

    setup_script = os.path.abspath(setup_script)

    path = os.path.dirname(setup_script)
    setup_script = os.path.basename(setup_script)

    if HAS_COVERAGE:

        # In this case, we run the command using the coverage command and we
        # then collect the coverage data into a SUBPROCESS_COVERAGE list which
        # is set up at the start of the testing process and is then combined
        # into a single .coverage file at the end of the testing process.

        p = sp.Popen(['coverage', 'run', setup_script] + list(args),
                     cwd=path,
                     stdout=sp.PIPE,
                     stderr=sp.PIPE)
        stdout, stderr = p.communicate()

        cdata = CoverageData()
        cdata.read_file(os.path.join(path, '.coverage'))
        SUBPROCESS_COVERAGE.append(cdata)

    else:

        # Otherwise we just run the tests with Python

        p = sp.Popen([sys.executable, setup_script] + list(args),
                     cwd=path,
                     stdout=sp.PIPE,
                     stderr=sp.PIPE)
        stdout, stderr = p.communicate()

    sys.stdout.write(stdout.decode('utf-8'))
    sys.stderr.write(stderr.decode('utf-8'))

    if p.returncode != 0:
        raise SystemExit(p.returncode)
Exemplo n.º 6
0
def run_setup(setup_script, args):

    # This used to call setuptools.sandbox's run_setup, but due to issues with
    # this and Cython (which caused segmentation faults), we now use subprocess.

    setup_script = os.path.abspath(setup_script)

    path = os.path.dirname(setup_script)
    setup_script = os.path.basename(setup_script)

    if HAS_COVERAGE:

        # In this case, we run the command using the coverage command and we
        # then collect the coverage data into a SUBPROCESS_COVERAGE list which
        # is set up at the start of the testing process and is then combined
        # into a single .coverage file at the end of the testing process.

        p = sp.Popen(['coverage', 'run', setup_script] + list(args), cwd=path,
                     stdout=sp.PIPE, stderr=sp.PIPE)
        stdout, stderr = p.communicate()

        cdata = CoverageData()
        cdata.read_file(os.path.join(path, '.coverage'))
        SUBPROCESS_COVERAGE.append(cdata)

    else:

        # Otherwise we just run the tests with Python

        p = sp.Popen([sys.executable, setup_script] + list(args), cwd=path,
                     stdout=sp.PIPE, stderr=sp.PIPE)
        stdout, stderr = p.communicate()

    sys.stdout.write(stdout.decode('utf-8'))
    sys.stderr.write(stderr.decode('utf-8'))

    if p.returncode != 0:
        raise SystemExit(p.returncode)
Exemplo n.º 7
0
def get_cov_data(input_file):
    data = CoverageData()
    data.read_file(input_file)
    return data