Esempio n. 1
0
    def test_invalid(self):
        """If `overrides` is not a dict, raise an exception."""
        original = ""
        overrides = "something that is not a dict"

        with self.assertRaises(ValueError):
            api.add_to_attribute("tests", overrides, original)
    def test_invalid_001(self):
        """Raise an exception if an invalid help attribute was given."""
        original = textwrap.dedent("""\
            name = "whatever"

            help = []
            """)

        with self.assertRaises(ValueError):
            api.add_to_attribute("help", {"foo": "bar"}, original)
    def test_empty_004(self):
        """Don't add any extries because the override cannot be empty."""
        original = textwrap.dedent("""\
            name = "whatever"

            help = []
            """)

        with self.assertRaises(ValueError):
            api.add_to_attribute("help", "", original)
    def test_invalid_002(self):
        """Report an issue because you cannot override a list with a string."""
        original = textwrap.dedent("""\
            name = "whatever"

            help = [
                ["foo", "bar"],
            ]
            """)

        with self.assertRaises(ValueError):
            api.add_to_attribute("help", "something", original)
Esempio n. 5
0
    def test_invalid(self):
        """Raise an exception if an invalid type or invalid input is given to `requires`."""
        text = 'name = "some_package"'
        overrides = None

        with self.assertRaises(ValueError):
            api.add_to_attribute("requires", overrides, text)

        overrides = []

        with self.assertRaises(ValueError):
            api.add_to_attribute("requires", overrides, text)
Esempio n. 6
0
    def _test(self, expected, text, overrides, remove=False, append=False):
        """Run a test and check if it makes the expected results.

        Args:
            expected (str): The output of `text` mixed with `overrides`.
            text (str): The raw Rez package.py input.
            overrides (list[str], optional): The data that will
                append / remove / replace requires.
                e.g. "some_package-2+<3".
            remove (bool, optional): If True, the attribute is removed,
                not added. If False, `overrides` are added. Default is False.
            append (bool, optional):
                If False and `graph` contains an existing version for a
                Rez package which conflicts with `data`, that version
                conflict is resolved as best as possible. If True,
                `data` is forced onto `graph`, without considering
                existing package requirements. Default is False.

        """
        if remove:
            api.remove_from_attribute("requires", overrides, text)

            return

        results = api.add_to_attribute("requires",
                                       overrides,
                                       text,
                                       append=append)
        self.assertEqual(expected, results)
    def _test(self, expected, text, overrides):
        """Check that `overrides` is added to `text` as expected.

        Args:
            expected (str): The output of `text` mixed with `overrides`.
            text (str): The raw Rez package.py input.
            overrides (str or list[list[str, str]]): The data that will append / replace help.

        """
        results = api.add_to_attribute("help", overrides, text)
        self.assertEqual(expected, results)
Esempio n. 8
0
    def test_empty_002(self):
        """Don't add any extries because `overrides` cannot be empty."""
        textwrap.dedent("""\
            tests = {
                "another": {
                    "command": "more",
                    "requires": ["information"],
                },
                "bar": {
                    "command": "thing",
                    "requires": ["whatever-1"],
                },
                "foo": "thing",
                "second_thing": {
                    "command": "and more",
                    "requires": ["information"],
                },
            }
            """)

        overrides = {}

        with self.assertRaises(ValueError):
            api.add_to_attribute("tests", overrides, " ")
def _bump(package, increment, new_dependencies):
    rez_bump_api.bump(package, **{increment: 1})

    with open(package.filepath, "r") as handler:
        code = handler.read()

    new_code = api.add_to_attribute("requires", new_dependencies, code)

    with filesystem.make_path_writable(
            os.path.dirname(os.path.dirname(package.filepath))):
        with serialise.open_file_for_write(package.filepath) as handler:
            handler.write(new_code)

    root = finder.get_package_root(package)

    return finder.get_nearest_rez_package(root)
    def test_list_override(self):
        """Replace a label entry of an existing list."""
        original = textwrap.dedent("""\
            name = "whatever"

            help = [
                ["thing", "blah"],
            ]
            """)

        expected = textwrap.dedent("""\
            name = "whatever"

            help = [
                ["thing", "blah"],
                ["thing", "blah"],
            ]
            """)

        results = api.add_to_attribute("help", [["thing", "blah"]],
                                       original,
                                       append=True)
        self.assertEqual(expected, results)
def _add_new_requirement_packages(package,
                                  namespaces,
                                  requirements,
                                  force=False):
    """Add new Rez package requirements to a Rez package, if needed.

    If no import statements were changed then this function does
    nothing. After all, if the imports of a package were changed then
    there's no way a Rez package's requirements should be any different.

    Args:
        package (:class:`rez.packges_.DeveloperPackage`):
            Some Rez package whose requirements may change as a result
            of this function getting ran.
        namespaces (iter[str]): The Python dot-separated namespaces that a Rez package uses.
            In short, these are all of the import statements that a
            Rez package has inside of it and can be thought of as its
            "dependencies". The function uses this list to figure out
            if `user_namespaces` is actually still being imported.
        requirements (iter[tuple[:class:`rez.vendor.version.requirement.Requirement`, tuple[str]]]):
            Each Rez package that might get added to `package` and a
            series of Python namespaces that the Rez package defines.
            If there's any overlap between the package's namespaces and
            the full `namespaces` then that means that `package` depends
            on the Rez package and so it is added as a dependency to
            `package`.
        force (bool, optional):
            If True, change every requirement that already exists in
            `package` and `requirements`. If False, only change package
            requirements if `requirements` is in the list of changed
            `namespaces`. Default is False.

    Returns:
        bool: If `package` was changed as a result of this function.

    """
    packages_to_add = set()

    if force:
        existing = set(requirement.name
                       for requirement in package.requires or [])
        packages_to_add = set(
            str(package_) for package_, _ in requirements
            if package_.name in existing)
    else:
        for package_, package_namespaces in requirements:
            if not _is_package_needed(tuple(package_namespaces), namespaces):
                continue

            packages_to_add.add(str(package_))

    if not packages_to_add:
        # Nothing to do so exit early.
        return False

    with open(package.filepath, "r") as handler:
        code = handler.read()

    new_code = api.add_to_attribute("requires", list(packages_to_add), code)

    with filesystem.make_path_writable(
            os.path.dirname(os.path.dirname(package.filepath))):
        with serialise.open_file_for_write(package.filepath) as handler:
            handler.write(new_code)

    return True
Esempio n. 12
0
 def _test(self, expected, text, overrides):
     results = api.add_to_attribute("tests", overrides, text)
     self.assertEqual(expected, results)