| 1 |
# THIS FILE IS PART OF THE CYLC SUITE ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from optparse import OptionParser, Values |
| 18 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
from cylc.flow.exceptions import CylcError |
| 22 |
from cylc.flow.parsec.exceptions import ParsecError |
| 23 |
from cylc.flow.terminal import ( |
| 24 |
cli_function, |
| 25 |
prompt, |
| 26 |
should_use_color, |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
# this puts Exception in globals() where we can easily find it later |
| 31 |
Exception = Exception |
| 32 |
SystemExit = SystemExit |
| 33 |
|
| 34 |
|
| 35 |
def get_option_parser(): |
| 36 |
"""An option parser with no options.""" |
| 37 |
return OptionParser() |
| 38 |
|
| 39 |
|
| 40 |
@cli_function(get_option_parser) |
| 41 |
def cli(parser, opts, exc_class): |
| 42 |
"""Dummy command line interface which raises an exception. |
| 43 |
|
| 44 |
Args: |
| 45 |
exc_class: The class of the exception to raise. |
| 46 |
|
| 47 |
""" |
| 48 |
if exc_class: |
| 49 |
raise globals()[exc_class]('message') |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize( |
| 53 |
'verbosity,exception_in,exception_out,return_code,stderr', |
| 54 |
[ |
| 55 |
# CylcError - "known" error |
| 56 |
pytest.param( # nicely formatted |
| 57 |
0, |
| 58 |
CylcError, |
| 59 |
SystemExit, |
| 60 |
1, |
| 61 |
'CylcError: message\n', |
| 62 |
id='CylcError' |
| 63 |
), |
| 64 |
pytest.param( # full traceback in debug mode |
| 65 |
2, |
| 66 |
CylcError, |
| 67 |
CylcError, |
| 68 |
None, |
| 69 |
None, |
| 70 |
id='CylcError-debug' |
| 71 |
), |
| 72 |
|
| 73 |
# ParsecError - "known" error |
| 74 |
pytest.param( # nicely formatted |
| 75 |
0, |
| 76 |
ParsecError, |
| 77 |
SystemExit, |
| 78 |
1, |
| 79 |
'ParsecError: message\n', |
| 80 |
id='ParsecError' |
| 81 |
), |
| 82 |
pytest.param( # full traceback in debug mode |
| 83 |
2, |
| 84 |
ParsecError, |
| 85 |
ParsecError, |
| 86 |
None, |
| 87 |
None, |
| 88 |
id='ParsecError-debug' |
| 89 |
), |
| 90 |
|
| 91 |
# Exception - "unknown" error |
| 92 |
pytest.param( # full traceback |
| 93 |
0, |
| 94 |
Exception, |
| 95 |
Exception, |
| 96 |
None, |
| 97 |
None, |
| 98 |
id='Exception' |
| 99 |
), |
| 100 |
pytest.param( # full traceback in debug mode |
| 101 |
2, |
| 102 |
Exception, |
| 103 |
Exception, |
| 104 |
None, |
| 105 |
None, |
| 106 |
id='Exception-debug' |
| 107 |
), |
| 108 |
|
| 109 |
# SystemExit - "unknown" error |
| 110 |
pytest.param( # full traceback |
| 111 |
0, |
| 112 |
SystemExit, |
| 113 |
SystemExit, |
| 114 |
1, |
| 115 |
'ERROR: message\n', |
| 116 |
id='SystemExit' |
| 117 |
), |
| 118 |
pytest.param( # full traceback in debug mode |
| 119 |
2, |
| 120 |
SystemExit, |
| 121 |
SystemExit, |
| 122 |
1, |
| 123 |
'ERROR: message\n', |
| 124 |
id='SystemExit-debug' |
| 125 |
), |
| 126 |
] |
| 127 |
) |
| 128 |
def test_cli( |
| 129 |
verbosity, |
| 130 |
exception_in, |
| 131 |
exception_out, |
| 132 |
return_code, |
| 133 |
stderr, |
| 134 |
monkeypatch, |
| 135 |
capsys |
| 136 |
): |
| 137 |
"""Test that the CLI formats exceptions appropriately. |
| 138 |
|
| 139 |
The idea here is that "known" errors (those which subclass CylcError or |
| 140 |
ParsecError) should be formatted nicely (as opposed to dumping the |
| 141 |
full traceback to stderr) in interactive mode. This behaviour can be |
| 142 |
overridden using --debug mode. In non-interactive mode we always print the |
| 143 |
full traceback for logging purposes. |
| 144 |
|
| 145 |
Other exceptions represent "unknown" errors which we would expect to occur. |
| 146 |
We should print the full traceback in these situations. |
| 147 |
""" |
| 148 |
monkeypatch.setattr('cylc.flow.flags.verbosity', verbosity) |
| 149 |
monkeypatch.setattr('cylc.flow.terminal.supports_color', lambda: False) |
| 150 |
|
| 151 |
with pytest.raises(exception_out) as exc_ctx: |
| 152 |
cli(exception_in.__name__) |
| 153 |
|
| 154 |
if return_code is not None: |
| 155 |
assert exc_ctx.value.args[0] == return_code |
| 156 |
|
| 157 |
if stderr is not None: |
| 158 |
assert capsys.readouterr()[1] == stderr |
| 159 |
|
| 160 |
|
| 161 |
@pytest.fixture |
| 162 |
def stdinput(monkeypatch): |
| 163 |
def _input(*lines): |
| 164 |
lines = list(lines) |
| 165 |
|
| 166 |
def __input(_message): |
| 167 |
try: |
| 168 |
return lines.pop(0) |
| 169 |
except IndexError: |
| 170 |
raise Exception('stdinput ran out of lines') |
| 171 |
|
| 172 |
monkeypatch.setattr( |
| 173 |
'cylc.flow.terminal.input', |
| 174 |
__input, |
| 175 |
) |
| 176 |
|
| 177 |
return _input |
| 178 |
|
| 179 |
|
| 180 |
def test_prompt(stdinput): |
| 181 |
"""Test the prompt function with some simulated input.""" |
| 182 |
# test a multiple choice prompt |
| 183 |
stdinput('y') |
| 184 |
assert prompt('yes or no', ['y', 'n']) == 'y' |
| 185 |
stdinput('n') |
| 186 |
assert prompt('yes or no', ['y', 'n']) == 'n' |
| 187 |
|
| 188 |
# test a prompt with mapped return values |
| 189 |
stdinput('42') |
| 190 |
assert prompt('what is the answer', {'41': False, '42': True}) is True |
| 191 |
stdinput('41') |
| 192 |
assert prompt('what is the answer', {'41': False, '42': True}) is False |
| 193 |
|
| 194 |
# test incorrect input (should re-prompt until it gets a valid response) |
| 195 |
stdinput('40', '41', '42') |
| 196 |
assert prompt('what is the answer', ['42']) == '42' |
| 197 |
|
| 198 |
# test a prompt with a default |
| 199 |
stdinput('') |
| 200 |
assert prompt('whatever', ['x'], default='x') == 'x' |
| 201 |
|
| 202 |
# test a prompt with an input pre-process method thinggy |
| 203 |
stdinput('YES') |
| 204 |
assert prompt('yes yes yes no', ['yes', 'no'], process=str.lower) == 'yes' |
| 205 |
|
| 206 |
|
| 207 |
@pytest.mark.parametrize('opts, supported, expected', [ |
| 208 |
({}, True, False), |
| 209 |
({'color': 'never'}, True, False), |
| 210 |
({'color': 'auto'}, True, True), |
| 211 |
({'color': 'auto'}, False, False), |
| 212 |
({'color': 'always'}, False, True), |
| 213 |
]) |
| 214 |
def test_should_use_color( |
| 215 |
opts: dict, expected: bool, supported: bool, |
| 216 |
monkeypatch: pytest.MonkeyPatch |
| 217 |
): |
| 218 |
monkeypatch.setattr('cylc.flow.terminal.supports_color', lambda: supported) |
| 219 |
options = Values(opts) |
| 220 |
assert should_use_color(options) == expected |