File: test_parse_options.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (106 lines) | stat: -rw-r--r-- 3,160 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import pytest

from briefcase.console import LogLevel


def test_parse_options_no_overrides(base_command):
    """Command line options are parsed if provided without overrides."""
    options, overrides = base_command.parse_options(
        extra=(
            "-x",
            "wibble",
            "-r",
            "important",
        )
    )

    assert options == {
        "extra": "wibble",
        "mystery": None,
        "required": "important",
    }
    assert overrides == {}
    assert base_command.console.input_enabled
    assert base_command.console.verbosity == LogLevel.INFO


def test_parse_options_with_overrides(base_command):
    """Command line options and overrides are parsed if provided."""
    options, overrides = base_command.parse_options(
        extra=(
            "-x",
            "wibble",
            "-r",
            "important",
            "-C",
            "width=10",
            "-C",
            "height=20",
        )
    )

    assert options == {
        "extra": "wibble",
        "mystery": None,
        "required": "important",
    }
    assert overrides == {
        "width": 10,
        "height": 20,
    }
    assert base_command.console.input_enabled
    assert base_command.console.verbosity == LogLevel.INFO


@pytest.mark.parametrize(
    "verbosity, log_level",
    [
        ("", LogLevel.INFO),
        ("-v", LogLevel.VERBOSE),
        ("-vv", LogLevel.DEBUG),
        ("-vvv", LogLevel.DEEP_DEBUG),
        ("-vvvv", LogLevel.DEEP_DEBUG),
        ("-vvvvv", LogLevel.DEEP_DEBUG),
    ],
)
def test_verbosity(base_command, verbosity, log_level):
    """The logging level is set correctly for the verbosity."""
    base_command.parse_options(extra=filter(None, ("-r", "default", verbosity)))

    assert base_command.console.verbosity == log_level


def test_missing_option(base_command, capsys):
    """If a required option isn't provided, an error is raised."""
    with pytest.raises(SystemExit) as excinfo:
        base_command.parse_options(extra=("-x", "wibble"))

    # Error code for a missing required option
    assert excinfo.value.code == 2
    # Error message about missing option is displayed
    err = capsys.readouterr().err
    assert "the following arguments are required: -r/--required" in err


def test_unknown_option(other_command, capsys):
    """If an unknown command is provided, it raises an error."""
    with pytest.raises(SystemExit) as excinfo:
        other_command.parse_options(extra=("-y", "because"))

    # Error code for an unknown option
    assert excinfo.value.code == 2
    # Error message about unknown option is displayed
    err = capsys.readouterr().err
    assert "unrecognized arguments: -y because" in err


def test_no_options(other_command, capsys):
    """If a command doesn't define options, any option is an error."""
    with pytest.raises(SystemExit) as excinfo:
        other_command.parse_options(extra=("-x", "wibble"))

    # Error code for an unknown option
    assert excinfo.value.code == 2
    # Error message about unknown option is displayed
    err = capsys.readouterr().err
    assert "unrecognized arguments: -x wibble" in err