Example #1
0
    def _get_src_path_line_nodes(self, xml_document, src_path):
        """
        Returns a list of nodes containing line information for `src_path`
        in `xml_document`.

        If file is not present in `xml_document`, return None
        """

        # Remove git_root from src_path for searching the correct filename
        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `diff_cover/violations_reporter.py`
        # search for `violations_reporter.py`
        src_rel_path = GitPathTool.relative_path(src_path)

        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `other_package/some_file.py`
        # search for `/home/user/work/diff-cover/other_package/some_file.py`
        src_abs_path = GitPathTool.absolute_path(src_path)

        xpath = ".//class"
        classes = [
            class_tree for class_tree in xml_document.findall(xpath) or []
        ]
        classes = ([
            clazz for clazz in classes if clazz.get('filename') == src_abs_path
        ] or [
            clazz for clazz in classes if clazz.get('filename') == src_rel_path
        ])
        if not classes:
            return None

        lines = [clazz.findall('./lines/line') for clazz in classes]
        return [elem for elem in itertools.chain(*lines)]
    def _get_src_path_line_nodes(self, xml_document, src_path):
        """
        Returns a list of nodes containing line information for `src_path`
        in `xml_document`.

        If file is not present in `xml_document`, return None
        """

        # Remove git_root from src_path for searching the correct filename
        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `diff_cover/violations_reporter.py`
        # search for `violations_reporter.py`
        src_rel_path = GitPathTool.relative_path(src_path)

        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `other_package/some_file.py`
        # search for `/home/user/work/diff-cover/other_package/some_file.py`
        src_abs_path = GitPathTool.absolute_path(src_path)

        xpath_template = ".//class[@filename='{0}']/lines/line"
        xpath = None

        src_node_xpath = ".//class[@filename='{0}']".format(src_rel_path)
        if xml_document.find(src_node_xpath) is not None:
            xpath = xpath_template.format(src_rel_path)

        src_node_xpath = ".//class[@filename='{0}']".format(src_abs_path)
        if xml_document.find(src_node_xpath) is not None:
            xpath = xpath_template.format(src_abs_path)

        if xpath is None:
            return None

        return xml_document.findall(xpath)
    def _get_src_path_line_nodes(self, xml_document, src_path):
        """
        Returns a list of nodes containing line information for `src_path`
        in `xml_document`.

        If file is not present in `xml_document`, return None
        """

        # Remove git_root from src_path for searching the correct filename
        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `diff_cover/violations_reporter.py`
        # search for `violations_reporter.py`
        src_rel_path = GitPathTool.relative_path(src_path)

        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `other_package/some_file.py`
        # search for `/home/user/work/diff-cover/other_package/some_file.py`
        src_abs_path = GitPathTool.absolute_path(src_path)

        xpath = ".//class"
        classes = [class_tree for class_tree in xml_document.findall(xpath)
                   or []]
        classes = ([clazz for clazz in classes
                    if clazz.get('filename') == src_abs_path]
                   or
                   [clazz for clazz in classes
                    if clazz.get('filename') == src_rel_path])
        if not classes:
            return None

        lines = [clazz.findall('./lines/line') for clazz in classes]
        return [elem for elem in itertools.chain(*lines)]
Example #4
0
    def test_set_cwd_unicode_byte_passed_in_for_cwd(self):
        self._set_git_root(b"\xe2\x94\xbb\xe2\x94\x81\xe2\x94\xbb\n--encoding=utf-8\n")
        expected = '\u253b\u2501\u253b/other_package/file.py'
        cwd = b'\\u253b\\u2501\\u253b/diff_cover'

        GitPathTool.set_cwd(cwd)
        path = GitPathTool.absolute_path('other_package/file.py')

        # Expect absolute path to file.py
        self.assertEqual(path, expected)
Example #5
0
    def test_absolute_path(self):
        self._set_git_root(b'/home/user/work dir/diff-cover\n--encoding=utf-8\n')
        expected = '/home/user/work dir/diff-cover/other_package/file.py'
        cwd = '/home/user/work dir/diff-cover/diff_cover'

        GitPathTool.set_cwd(cwd)
        path = GitPathTool.absolute_path('other_package/file.py')

        # Expect absolute path to file.py
        self.assertEqual(path, expected)
Example #6
0
    def test_set_cwd_unicode(self):
        self._set_git_root(b"\xe2\x94\xbb\xe2\x94\x81\xe2\x94\xbb")
        expected = "\u253b\u2501\u253b/other_package/file.py"
        cwd = "\\u253b\\u2501\\u253b/diff_cover\n--encoding=utf-8\n"

        GitPathTool.set_cwd(cwd)
        path = GitPathTool.absolute_path("other_package/file.py")

        # Expect absolute path to file.py
        self.assertEqual(path, expected)
Example #7
0
    def test_absolute_path(self):
        self._set_git_root('/home/user/work/diff-cover')
        expected = '/home/user/work/diff-cover/other_package/file.py'
        cwd = '/home/user/work/diff-cover/diff_cover'

        tool = GitPathTool(cwd)
        path = tool.absolute_path('other_package/file.py')

        # Expect absolute path to file.py
        self.assertEqual(path, expected)
Example #8
0
    def test_absolute_path(self):
        self._set_git_root('/home/user/work/diff-cover')
        expected = '/home/user/work/diff-cover/other_package/file.py'
        cwd = '/home/user/work/diff-cover/diff_cover'

        tool = GitPathTool(cwd)
        path = tool.absolute_path('other_package/file.py')

        # Expect absolute path to file.py
        self.assertEqual(path, expected)
Example #9
0
    def test_absolute_path(self):
        self._set_git_root(b'/home/user/work/diff-cover\n--encoding=utf-8\n')
        expected = '/home/user/work/diff-cover/other_package/file.py'
        cwd = '/home/user/work/diff-cover/diff_cover'

        GitPathTool.set_cwd(cwd)
        path = GitPathTool.absolute_path('other_package/file.py')

        # Expect absolute path to file.py
        self.assertEqual(path, expected)
    def _get_classes(self, xml_document, src_path):
        """
        Given a path and parsed xml_document provides class nodes
        with the relevant lines

        First, we look to see if xml_document contains a source
        node providing paths to search for

        If we don't have that we check each nodes filename attribute
        matches an absolute path

        Finally, if we found no nodes, we check the filename attribute
        for the relative path
        """
        # Remove git_root from src_path for searching the correct filename
        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `diff_cover/violations_reporter.py`
        # search for `violations_reporter.py`
        src_rel_path = self._to_unix_path(GitPathTool.relative_path(src_path))

        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `other_package/some_file.py`
        # search for `/home/user/work/diff-cover/other_package/some_file.py`
        src_abs_path = self._to_unix_path(GitPathTool.absolute_path(src_path))

        # cobertura sometimes provides the sources for the measurements
        # within it. If we have that we outta use it
        sources = xml_document.findall("sources/source")
        sources = [source.text for source in sources if source.text]
        classes = [class_tree for class_tree in xml_document.findall(".//class") or []]

        classes = (
            [
                clazz
                for clazz in classes
                if src_abs_path
                in [
                    self._to_unix_path(
                        os.path.join(source.strip(), clazz.get("filename"))
                    )
                    for source in sources
                ]
            ]
            or [
                clazz
                for clazz in classes
                if self._to_unix_path(clazz.get("filename")) == src_abs_path
            ]
            or [
                clazz
                for clazz in classes
                if self._to_unix_path(clazz.get("filename")) == src_rel_path
            ]
        )
        return classes
Example #11
0
    def test_absolute_path(self):
        self._set_git_root(
            b"/home/user/work dir/diff-cover\n--encoding=utf-8\n")
        expected = "/home/user/work dir/diff-cover/other_package/file.py"
        cwd = "/home/user/work dir/diff-cover/diff_cover"

        GitPathTool.set_cwd(cwd)
        path = GitPathTool.absolute_path("other_package/file.py")

        # Expect absolute path to file.py
        self.assertEqual(path, expected)
Example #12
0
    def test_set_cwd_unicode_byte_passed_in_for_cwd(self):
        self._set_git_root(
            b"\xe2\x94\xbb\xe2\x94\x81\xe2\x94\xbb\n--encoding=utf-8\n")
        expected = '\u253b\u2501\u253b/other_package/file.py'
        cwd = b'\\u253b\\u2501\\u253b/diff_cover'

        GitPathTool.set_cwd(cwd)
        path = GitPathTool.absolute_path('other_package/file.py')

        # Expect absolute path to file.py
        self.assertEqual(path, expected)
Example #13
0
def test_set_cwd_unicode(process):
    process.communicate.return_value = (
        b"\xe2\x94\xbb\xe2\x94\x81\xe2\x94\xbb", b"")

    expected = "\u253b\u2501\u253b/other_package/file.py"
    cwd = "\\u253b\\u2501\\u253b/diff_cover\n--encoding=utf-8\n"

    GitPathTool.set_cwd(cwd)
    path = GitPathTool.absolute_path("other_package/file.py")

    # Expect absolute path to file.py
    assert path == expected
Example #14
0
    def _get_classes(self, xml_document, src_path):
        """
        Given a path and parsed xml_document provides class nodes
        with the relevant lines

        First, we look to see if xml_document contains a source
        node providing paths to search for

        If we don't have that we check each nodes filename attribute
        matches an absolute path

        Finally, if we found no nodes, we check the filename attribute
        for the relative path
        """
        # Remove git_root from src_path for searching the correct filename
        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `diff_cover/violations_reporter.py`
        # search for `violations_reporter.py`
        src_rel_path = self._to_unix_path(GitPathTool.relative_path(src_path))

        # If cwd is `/home/user/work/diff-cover/diff_cover`
        # and src_path is `other_package/some_file.py`
        # search for `/home/user/work/diff-cover/other_package/some_file.py`
        src_abs_path = self._to_unix_path(GitPathTool.absolute_path(src_path))

        # cobertura sometimes provides the sources for the measurements
        # within it. If we have that we outta use it
        sources = xml_document.findall('sources/source')
        sources = [source.text for source in sources]
        classes = [class_tree
                   for class_tree in xml_document.findall(".//class")
                   or []]

        classes = (
            [clazz for clazz in classes if
             src_abs_path in [
                 self._to_unix_path(
                     os.path.join(
                         source,
                         clazz.get('filename')
                     )
                 ) for source in sources]]
            or
            [clazz for clazz in classes if
             self._to_unix_path(clazz.get('filename')) == src_abs_path]
            or
            [clazz for clazz in classes if
             self._to_unix_path(clazz.get('filename')) == src_rel_path]
        )
        return classes
Example #15
0
def test_absolute_path(process):
    process.communicate.return_value = (
        b"/home/user/work dir/diff-cover\n--encoding=utf-8\n",
        b"",
    )

    expected = "/home/user/work dir/diff-cover/other_package/file.py"
    cwd = "/home/user/work dir/diff-cover/diff_cover"

    GitPathTool.set_cwd(cwd)
    path = GitPathTool.absolute_path("other_package/file.py")

    # Expect absolute path to file.py
    assert path == expected