Exemple #1
0
def test_confirm_with_user_defaults_without_tty(mock_input, mock_isatty):
    mock_isatty.return_value = False

    assert confirm_with_user("prompt", default=True) is True
    assert confirm_with_user("prompt", default=False) is False

    assert mock_input.mock_calls == []
Exemple #2
0
def test_confirm_with_user_defaults_with_tty(mock_input, mock_isatty):
    mock_input.return_value = ""
    mock_isatty.return_value = True

    assert confirm_with_user("prompt", default=True) is True
    assert mock_input.mock_calls == [call("prompt [Y/n]: ")]
    mock_input.reset_mock()

    assert confirm_with_user("prompt", default=False) is False
    assert mock_input.mock_calls == [call("prompt [y/N]: ")]
Exemple #3
0
def test_confirm_with_user_pause_emitter(mock_isatty, emitter):
    """The emitter should be paused when using the terminal."""
    mock_isatty.return_value = True

    def fake_input(prompt):
        """Check if the Emitter is paused."""
        assert emitter.paused
        return ""

    with patch("charmcraft.utils.input", fake_input):
        confirm_with_user("prompt")
Exemple #4
0
    def ensure_provider_is_available(cls) -> None:
        """Ensure provider is available, prompting the user to install it if required.

        :raises CommandError: if provider is not available.
        """
        if not multipass.is_installed():
            if confirm_with_user(
                "Multipass is required, but not installed. Do you wish to install Multipass "
                "and configure it with the defaults?",
                default=False,
            ):
                try:
                    multipass.install()
                except multipass.MultipassInstallationError as error:
                    raise CommandError(
                        "Failed to install Multipass. Visit https://multipass.run/ for "
                        "instructions on installing Multipass for your operating system."
                    ) from error
            else:
                raise CommandError(
                    "Multipass is required, but not installed. Visit https://multipass.run/ for "
                    "instructions on installing Multipass for your operating system."
                )

        try:
            multipass.ensure_multipass_is_ready()
        except multipass.MultipassError as error:
            raise CommandError(str(error)) from error
Exemple #5
0
def ensure_provider_is_available() -> None:
    """Ensure provider is available, prompting the user to install it if required.

    :raises CommandError: if provider is not available.
    """
    if is_provider_available():
        return

    if confirm_with_user(
        "LXD is required, but not installed. Do you wish to install LXD "
        "and configure it with the defaults?",
        default=False,
    ):
        try:
            lxd_installer.install()
        except LXDInstallationError as error:
            raise CommandError(
                "Failed to install LXD. Please visit https://snapcraft.io/lxd for "
                "instructions on how to install the LXD snap for your distribution"
            ) from error
    else:
        raise CommandError(
            "LXD is required, but not installed. Please visit https://snapcraft.io/lxd for "
            "instructions on how to install the LXD snap for your distribution"
        )
Exemple #6
0
    def ensure_provider_is_available(cls) -> None:
        """Ensure provider is available, prompting the user to install it if required.

        :raises CraftError: if provider is not available.
        """
        if not lxd.is_installed():
            if confirm_with_user(
                "LXD is required, but not installed. Do you wish to install LXD "
                "and configure it with the defaults?",
                default=False,
            ):
                try:
                    lxd.install()
                except lxd.LXDInstallationError as error:
                    raise CraftError(
                        "Failed to install LXD. Visit https://snapcraft.io/lxd for "
                        "instructions on how to install the LXD snap for your distribution"
                    ) from error
            else:
                raise CraftError(
                    "LXD is required, but not installed. Visit https://snapcraft.io/lxd for "
                    "instructions on how to install the LXD snap for your distribution"
                )

        try:
            lxd.ensure_lxd_is_ready()
        except lxd.LXDError as error:
            raise CraftError(str(error)) from error
Exemple #7
0
def test_confirm_with_user_errors_in_managed_mode(
    mock_is_charmcraft_running_in_managed_mode, ):
    mock_is_charmcraft_running_in_managed_mode.return_value = True

    with pytest.raises(RuntimeError):
        confirm_with_user("prompt")
Exemple #8
0
def test_confirm_with_user(user_input, expected, mock_input, mock_isatty):
    mock_input.return_value = user_input

    assert confirm_with_user("prompt") == expected
    assert mock_input.mock_calls == [call("prompt [y/N]: ")]