Example #1
0
def test_outer_inner():
    code_blocks = dict(parse.find_elements(
        open(umbrella_file).read(), ["outer", "outer.inner"]))

    assert len(code_blocks) == 2

    assert len(code_blocks["outer"]) == 4

    assert len(code_blocks["outer.inner"]) == 2
Example #2
0
def test_find_module():
    code_blocks = list(parse.find_elements(open(umbrella_file).read(), [""]))

    assert len(code_blocks) == 1

    for name, lines in code_blocks:
        assert name == ""
        assert len(lines) == 92
        assert lines[0] == ('"""')
        assert lines[-1] == ('    pass')
Example #3
0
    def compare_files(self, match_source, current_source, matches):
        """
        For 2 sources of the same file, compare the part that we're
        intereted in and add errors only if those parts are different.
        """
        elements = {match.element for match in matches}
        match_elements = dict(parse.find_elements(match_source, elements))
        current_elements = dict(parse.find_elements(current_source, elements))

        match_keys = frozenset(match_elements)
        current_keys = frozenset(current_elements)

        # Missing functions/classes in matched (should not exist)
        unexpectedly_missing = current_keys - match_keys
        if unexpectedly_missing:
            raise ValueError(
                "Raincoat was misconfigured. The following elements "
                "do not exist in the file : {}. Offending Raincoat "
                "comments are located here : {}".format(
                    ", ".join(unexpectedly_missing),
                    # TODO : refine the next line.
                    ", ".join(str(match) for match in matches)))

        # Missing functions/classes in current
        disappeared_elements = match_keys - current_keys
        for element in disappeared_elements:
            for match in matches:
                if match.element == element:
                    self.add_error(
                        "Code object {} has disappeared"
                        "".format(element), match)

        common_keys = match_keys & current_keys

        for match in matches:
            if match.element not in common_keys:
                continue

            match_block = match_elements[match.element]
            current_block = current_elements[match.element]
            match.check(checker=self,
                        match_block=match_block,
                        current_block=current_block)
Example #4
0
def test_one_liner():
    one_liner_file = os.path.join(umbrella_dir, "oneliner.py")

    code_blocks = list(parse.find_elements(open(one_liner_file).read(), ["a"]))

    assert len(code_blocks) == 1

    for name, lines in code_blocks:
        assert name == "a"
        assert len(lines) == 1
        assert lines == ['def a(): pass  # noqa']
Example #5
0
def test_find_class():
    code_blocks = list(
        parse.find_elements(open(umbrella_file).read(), ["Umbrella"]))

    assert len(code_blocks) == 1

    for name, lines in code_blocks:
        assert name == "Umbrella"
        assert len(lines) == 27
        assert lines[0] == ('class Umbrella(object):')
        assert lines[-1] == ('        return True')
Example #6
0
def test_find_method():
    code_blocks = list(
        parse.find_elements(open(umbrella_file).read(), ["Umbrella.open"]))

    assert len(code_blocks) == 1

    for name, lines in code_blocks:
        assert name == "Umbrella.open"
        assert len(lines) == 9
        assert lines[0] == ('    def open(self):')
        assert lines[-1] == ('        self.side = "pointy side up"')
Example #7
0
def test_find_function():
    code_blocks = list(
        parse.find_elements(open(umbrella_file).read(), ["use_umbrella"]))

    assert len(code_blocks) == 1

    for name, lines in code_blocks:
        assert name == "use_umbrella"
        assert len(lines) == 15
        assert lines[0] == ('def use_umbrella(umbrella, action):')
        assert lines[-1] == ('    umbrella.put_pouch()')
Example #8
0
def test_find_class():
    code_blocks = list(
        parse.find_elements(open(umbrella_file).read(), ["Umbrella"]))

    assert len(code_blocks) == 1
    name, lines = code_blocks[0]

    print(lines)
    assert name == "Umbrella"
    assert len(lines) == 27
    assert lines[0] == ("class Umbrella(object):")
    assert lines[-1] == ("        return True")
Example #9
0
def test_find_function():
    code_blocks = list(
        parse.find_elements(open(umbrella_file).read(), ["use_umbrella"]))

    assert len(code_blocks) == 1
    name, lines = code_blocks[0]

    print(lines)

    assert name == "use_umbrella"
    assert len(lines) == 15
    assert lines[0] == ("def use_umbrella(umbrella, action):")
    assert lines[-1] == ("    umbrella.put_pouch()")
Example #10
0
 def get_elements(self, source_keys):
     grouped_keys = group_composite(source_keys)
     for source_key, files_dict in grouped_keys.items():
         files_source = self.get_source(source_key, set(files_dict))
         for path, element_names in files_dict.items():
             file_source = files_source[path]
             if file_source is constants.FILE_NOT_FOUND:
                 for element_name in element_names:
                     full_key = (source_key, path, element_name)
                     yield full_key, constants.FILE_NOT_FOUND
             else:
                 elements = parse.find_elements(file_source, element_names)
                 for element_name, element_source in elements:
                     full_key = (source_key, path, element_name)
                     yield full_key, element_source
Example #11
0
    def compare_packages(self, packages, match_info):
        for package_key, (match_source, current_source) in packages.items():
            package_info = match_info[package_key]
            package_name, package_version = package_key
            files = dict(self.get_differences(
                match_dict=match_source,
                current_dict=current_source))

            for path, (match_file, current_file) in files.items():
                file_info = package_info[path]
                if match_file is None:
                    for match in self.get_all_matches(file_info):
                        yield ("Invalid Raincoat PyPI comment : {} does not "
                               "exist in {}=={}"
                               .format(path, package_name, package_version),
                               match)
                    continue

                if current_file is None:
                    for match in self.get_all_matches(file_info):
                        yield ("File {} disappeared from {}"
                               .format(path, package_name),
                               match)
                    continue

                names = list(file_info)
                match_elements = dict(
                    parse.find_elements(match_file, names))
                current_elements = dict(
                    parse.find_elements(current_file, names))

                elements = dict(self.get_differences(
                    match_dict=match_elements,
                    current_dict=current_elements))

                if None in file_info:
                    for match in file_info[None]:
                        yield self.compare_blocks(
                            match=match,
                            match_block=match_file.splitlines(),
                            current_block=current_file.splitlines(),
                            path=path)

                for name, (match_block, current_block) in elements.items():
                    if not name:
                        continue

                    block_info = file_info[name]
                    if match_block is None:
                        for match in block_info:
                            yield ("Invalid Raincoat PyPI comment : {} does "
                                   "not exist in {} in {}=={}"
                                   .format(name, path, package_name,
                                           package_version),
                                   match)
                        continue

                    if current_block is None:
                        for match in block_info:
                            yield ("{} disappeared from {} in {}"
                                   .format(name, path, package_name),
                                   match)
                        continue

                    for match in block_info:
                        yield self.compare_blocks(
                            match=match,
                            match_block=match_block,
                            current_block=current_block,
                            path=path)
Example #12
0
def test_find_several():
    code_blocks = list(
        parse.find_elements(
            open(umbrella_file).read(), ["use_umbrella", "Umbrella.open", ""]))

    assert len(code_blocks) == 3
Example #13
0
def test_empty_file():
    list(parse.find_elements("", ["a"]))
Example #14
0
def test_find_function_not_found():
    code_blocks = list(parse.find_elements(" ", ["a"]))

    assert code_blocks == [("a", constants.ELEMENT_NOT_FOUND)]