|
30 | 30 | from craft_cli.errors import ArgumentParsingError |
31 | 31 | from tests.factory import create_command |
32 | 32 |
|
| 33 | +# --- Tests for global arguments |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + ("params", "expected"), |
| 38 | + [ |
| 39 | + pytest.param( |
| 40 | + {"type": "flag"}, |
| 41 | + GlobalArgument( |
| 42 | + name="my-arg", |
| 43 | + short_option=None, |
| 44 | + long_option="--long-option", |
| 45 | + help_message="A global argument for testing", |
| 46 | + type="flag", |
| 47 | + ), |
| 48 | + id="basic-flag", |
| 49 | + ), |
| 50 | + pytest.param( |
| 51 | + {"type": "option"}, |
| 52 | + GlobalArgument( |
| 53 | + name="my-arg", |
| 54 | + short_option=None, |
| 55 | + long_option="--long-option", |
| 56 | + help_message="A global argument for testing", |
| 57 | + type="option", |
| 58 | + ), |
| 59 | + id="basic-option", |
| 60 | + ), |
| 61 | + pytest.param( |
| 62 | + {"type": "option", "choices": ["a", "b", "c"]}, |
| 63 | + GlobalArgument( |
| 64 | + name="my-arg", |
| 65 | + short_option=None, |
| 66 | + long_option="--long-option", |
| 67 | + help_message="A global argument for testing", |
| 68 | + type="option", |
| 69 | + choices=["a", "b", "c"], |
| 70 | + ), |
| 71 | + id="choices", |
| 72 | + ), |
| 73 | + pytest.param( |
| 74 | + {"type": "option", "choices": "ABC", "case_sensitive": False}, |
| 75 | + GlobalArgument( |
| 76 | + name="my-arg", |
| 77 | + short_option=None, |
| 78 | + long_option="--long-option", |
| 79 | + help_message="A global argument for testing", |
| 80 | + type="option", |
| 81 | + choices=["a", "b", "c"], |
| 82 | + case_sensitive=False |
| 83 | + ), |
| 84 | + id="case-insensitive", |
| 85 | + ), |
| 86 | + pytest.param( |
| 87 | + {"type": "option", "validator": int}, |
| 88 | + GlobalArgument( |
| 89 | + name="my-arg", |
| 90 | + short_option=None, |
| 91 | + long_option="--long-option", |
| 92 | + help_message="A global argument for testing", |
| 93 | + type="option", |
| 94 | + validator=int |
| 95 | + ), |
| 96 | + id="int-validator", |
| 97 | + ), |
| 98 | + pytest.param( |
| 99 | + {"type": "option", "choices": "123", "validator": int}, |
| 100 | + GlobalArgument( |
| 101 | + name="my-arg", |
| 102 | + short_option=None, |
| 103 | + long_option="--long-option", |
| 104 | + help_message="A global argument for testing", |
| 105 | + type="option", |
| 106 | + choices="123", |
| 107 | + validator=int, |
| 108 | + ), |
| 109 | + id="limited-ints", |
| 110 | + ), |
| 111 | + ] |
| 112 | +) |
| 113 | +def test_global_argument_init_success(params, expected): |
| 114 | + my_params = { |
| 115 | + "name": "my-arg", |
| 116 | + "short_option": None, |
| 117 | + "long_option": "--long-option", |
| 118 | + "help_message": "A global argument for testing", |
| 119 | + } |
| 120 | + my_params.update(params) |
| 121 | + assert GlobalArgument(**my_params) == expected |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.parametrize( |
| 125 | + ("params", "match"), |
| 126 | + [ |
| 127 | + ( |
| 128 | + {"type": "flag", "choices": "something"}, |
| 129 | + "A flag argument cannot have choices or a validator." |
| 130 | + ) |
| 131 | + ] |
| 132 | +) |
| 133 | +def test_global_argument_init_error(params, match): |
| 134 | + my_params = { |
| 135 | + "name": "my-arg", |
| 136 | + "short_option": None, |
| 137 | + "long_option": "--long-option", |
| 138 | + "help_message": "A global argument for testing", |
| 139 | + } |
| 140 | + my_params.update(params) |
| 141 | + with pytest.raises(TypeError, match=match): |
| 142 | + GlobalArgument(**my_params) |
| 143 | + |
| 144 | + |
33 | 145 | # --- Tests for the Dispatcher |
34 | 146 |
|
35 | 147 |
|
|
0 commit comments