Ejemplo n.º 1
0
    def test_when_supports_color(self, mock_args):
        stack = contextlib.ExitStack()
        stack.enter_context(
            mock.patch.object(colorlog,
                              "escape_codes",
                              new={
                                  "red": 0,
                                  "green": 1,
                                  "blue": 2
                              }))
        supports_color = stack.enter_context(
            mock.patch.object(ux, "supports_color", return_value=True))
        read_text = stack.enter_context(
            mock.patch.object(importlib.resources, "read_text"))
        template = stack.enter_context(mock.patch.object(string, "Template"))
        write = stack.enter_context(mock.patch.object(sys.stdout, "write"))
        abspath = stack.enter_context(
            mock.patch.object(os.path, "abspath", return_value="some path"))
        dirname = stack.enter_context(mock.patch.object(os.path, "dirname"))

        with stack:
            ux.print_banner("hikari", True, False)

        args = {
            # Hikari stuff.
            "hikari_version": "2.2.2",
            "hikari_git_sha1": "12345678",
            "hikari_copyright": "© 2020 Nekokatt",
            "hikari_license": "MIT",
            "hikari_install_location": "some path",
            "hikari_documentation_url":
            "https://nekokatt.github.io/hikari/docs",
            "hikari_discord_invite": "https://discord.gg/Jx4cNGG",
            "hikari_source_url": "https://nekokatt.github.io/hikari",
            "python_implementation": "CPython",
            "python_version": "4.0.0",
            "system_description": "Machine Potato 1.0.0",
            "red": 0,
            "green": 1,
            "blue": 2,
        }

        template.assert_called_once_with(read_text())
        template().safe_substitute.assert_called_once_with(args)
        write.assert_called_once_with(template().safe_substitute())
        dirname.assert_called_once_with("~/hikari")
        abspath.assert_called_once_with(dirname())
        supports_color.assert_called_once_with(True, False)
Ejemplo n.º 2
0
    def test_use_extra_args(self, mock_args):
        stack = contextlib.ExitStack()
        stack.enter_context(
            mock.patch.object(colorlog.escape_codes, "escape_codes", new={}))
        stack.enter_context(mock.patch.object(time, "sleep"))
        read_text = stack.enter_context(
            mock.patch.object(importlib.resources, "read_text"))
        template = stack.enter_context(mock.patch.object(string, "Template"))
        builtins_open = stack.enter_context(mock.patch.object(
            builtins, "open"))
        stack.enter_context(
            mock.patch.object(os.path, "abspath", return_value="some path"))

        extra_args = {
            "extra_argument_1": "one",
            "extra_argument_2": "two",
        }

        with stack:
            ux.print_banner("hikari", True, False, extra_args=extra_args)

        args = {
            # Hikari stuff.
            "hikari_version": "2.2.2",
            "hikari_git_sha1": "12345678",
            "hikari_copyright": "© 2020 Nekokatt",
            "hikari_license": "MIT",
            "hikari_install_location": "some path",
            "hikari_documentation_url":
            "https://nekokatt.github.io/hikari/docs",
            "hikari_discord_invite": "https://discord.gg/Jx4cNGG",
            "hikari_source_url": "https://nekokatt.github.io/hikari",
            "python_implementation": "CPython",
            "python_version": "4.0.0",
            "system_description": "Machine Potato 1.0.0",
        }

        args.update(extra_args)

        template.assert_called_once_with(read_text())
        template().safe_substitute.assert_called_once_with(args)
        builtins_open.assert_called_once_with(1, "w", encoding="utf-8")
        builtins_open.return_value.__enter__.return_value.write.assert_called_once_with(
            template().safe_substitute())
Ejemplo n.º 3
0
    def print_banner(
        banner: typing.Optional[str],
        allow_color: bool,
        force_color: bool,
        extra_args: typing.Optional[typing.Dict[str, str]] = None,
    ) -> None:
        """Print the banner.

        This allows library vendors to override this behaviour, or choose to
        inject their own "branding" on top of what hikari provides by default.

        Normal users should not need to invoke this function, and can simply
        change the `banner` argument passed to the constructor to manipulate
        what is displayed.

        Parameters
        ----------
        banner : typing.Optional[builtins.str]
            The package to find a `banner.txt` in.
        allow_color : builtins.bool
            A flag that allows advising whether to allow color if supported or
            not. Can be overridden by setting a `"CLICOLOR"` environment
            variable to a non-`"0"` string.
        force_color : builtins.bool
            A flag that allows forcing color to always be output, even if the
            terminal device may not support it. Setting the `"CLICOLOR_FORCE"`
            environment variable to a non-`"0"` string will override this.

        !!! note
            `force_color` will always take precedence over `allow_color`.
        extra_args : typing.Optional[typing.Dict[builtins.str, builtins.str]]
            If provided, extra $-substitutions to use when printing the banner.
            Default substitutions can not be overwritten.

        Raises
        ------
        builtins.ValueError
            If `extra_args` contains a default $-substitution.
        """
        ux.print_banner(banner, allow_color, force_color, extra_args=extra_args)
Ejemplo n.º 4
0
    def test_overwrite_args_raises_error(self, mock_args):
        stack = contextlib.ExitStack()
        stack.enter_context(mock.patch.object(time, "sleep"))
        stack.enter_context(
            mock.patch.object(colorlog.escape_codes, "escape_codes", new={}))
        stack.enter_context(mock.patch.object(importlib.resources,
                                              "read_text"))
        stack.enter_context(mock.patch.object(string, "Template"))
        stack.enter_context(mock.patch.object(sys.stdout, "write"))
        stack.enter_context(
            mock.patch.object(os.path, "abspath", return_value="some path"))

        extra_args = {
            "hikari_version": "overwrite",
        }

        with stack:
            with pytest.raises(
                    ValueError,
                    match=
                    r"Cannot overwrite \$-substitution `hikari_version`. Please use a different key."
            ):
                ux.print_banner("hikari", True, False, extra_args=extra_args)
Ejemplo n.º 5
0
    def test_when_package_is_none(self):
        with mock.patch.object(sys.stdout, "write") as write:
            ux.print_banner(None, True, False)

        write.assert_not_called()
Ejemplo n.º 6
0
 def print_banner(banner: typing.Optional[str], allow_color: bool,
                  force_color: bool) -> None:
     ux.print_banner(banner, allow_color, force_color)
     if banner == "hikari":
         sys.stdout.write(f"Thank you for using lightbulb!\n")