| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW 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 |
"""Test the "cylc list" command.""" |
| 18 |
|
| 19 |
import pytest |
| 20 |
|
| 21 |
from cylc.flow.exceptions import InputError |
| 22 |
from cylc.flow.option_parsers import Options |
| 23 |
from cylc.flow.scripts.list import ( |
| 24 |
get_option_parser, |
| 25 |
_main, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
ListOptions = Options(get_option_parser()) |
| 30 |
|
| 31 |
|
| 32 |
@pytest.fixture(scope='module') |
| 33 |
async def cylc_list(mod_flow, mod_scheduler, mod_start): |
| 34 |
id_ = mod_flow( |
| 35 |
{ |
| 36 |
'scheduling': { |
| 37 |
# NOTE: all "a*" tasks are in the graph, but not "b*" tasks |
| 38 |
'initial cycle point': 1, |
| 39 |
'cycling mode': 'integer', |
| 40 |
'graph': {'P1': 'a12 & a111 & a112 & a121 & a2'}, |
| 41 |
}, |
| 42 |
'runtime': { |
| 43 |
'A': {'meta': {'title': 'Title For A'}}, |
| 44 |
'A1': {'inherit': 'A'}, |
| 45 |
'A11': {'inherit': 'A1', 'meta': {'title': 'Title For A11'}}, |
| 46 |
'A12': {'inherit': 'A1'}, |
| 47 |
'a2': {'inherit': 'A'}, |
| 48 |
'a12': {'inherit': 'A1'}, |
| 49 |
'a111': {'inherit': 'A11'}, |
| 50 |
'a112': {'inherit': 'A11'}, |
| 51 |
'a121': {'inherit': 'A12'}, |
| 52 |
'B': {}, |
| 53 |
'b1': {'inherit': 'B'}, |
| 54 |
}, |
| 55 |
} |
| 56 |
) |
| 57 |
schd = mod_scheduler(id_) |
| 58 |
|
| 59 |
async def _list(capsys, **kwargs): |
| 60 |
capsys.readouterr() |
| 61 |
await _main(ListOptions(**kwargs), schd.workflow) |
| 62 |
out, err = capsys.readouterr() |
| 63 |
return out.splitlines() |
| 64 |
|
| 65 |
async with mod_start(schd): |
| 66 |
yield _list |
| 67 |
|
| 68 |
|
| 69 |
@pytest.fixture |
| 70 |
def supports_utf8(monkeypatch): |
| 71 |
monkeypatch.setenv('LANG', 'C.utf-8') |
| 72 |
|
| 73 |
|
| 74 |
@pytest.fixture |
| 75 |
def does_not_support_utf8(monkeypatch): |
| 76 |
monkeypatch.setenv('LANG', 'C') |
| 77 |
|
| 78 |
|
| 79 |
async def test_plain(cylc_list, supports_utf8, capsys): |
| 80 |
"""Test the default output format.""" |
| 81 |
assert await cylc_list(capsys) == [ |
| 82 |
'a111', |
| 83 |
'a112', |
| 84 |
'a12', |
| 85 |
'a121', |
| 86 |
'a2', |
| 87 |
] |
| 88 |
|
| 89 |
assert await cylc_list(capsys, all_tasks=True) == [ |
| 90 |
'a111', |
| 91 |
'a112', |
| 92 |
'a12', |
| 93 |
'a121', |
| 94 |
'a2', |
| 95 |
'b1', # <= in the runtime but not in the graph |
| 96 |
] |
| 97 |
|
| 98 |
assert await cylc_list(capsys, all_namespaces=True) == [ |
| 99 |
'A', |
| 100 |
'A1', |
| 101 |
'A11', |
| 102 |
'A12', |
| 103 |
'B', |
| 104 |
'a111', |
| 105 |
'a112', |
| 106 |
'a12', |
| 107 |
'a121', |
| 108 |
'a2', |
| 109 |
'b1', |
| 110 |
'root', |
| 111 |
] |
| 112 |
|
| 113 |
with pytest.raises(InputError): |
| 114 |
await cylc_list(capsys, all_tasks=True, all_namespaces=True) |
| 115 |
|
| 116 |
|
| 117 |
async def test_mro(cylc_list, supports_utf8, capsys): |
| 118 |
"""Test the --mro (method resolution order) option.""" |
| 119 |
assert await cylc_list(capsys, mro=True) == [ |
| 120 |
'a111 a111 A11 A1 A root', |
| 121 |
'a112 a112 A11 A1 A root', |
| 122 |
'a12 a12 A1 A root', |
| 123 |
'a121 a121 A12 A1 A root', |
| 124 |
'a2 a2 A root', |
| 125 |
] |
| 126 |
|
| 127 |
assert await cylc_list(capsys, mro=True, all_tasks=True) == [ |
| 128 |
'a111 a111 A11 A1 A root', |
| 129 |
'a112 a112 A11 A1 A root', |
| 130 |
'a12 a12 A1 A root', |
| 131 |
'a121 a121 A12 A1 A root', |
| 132 |
'a2 a2 A root', |
| 133 |
'b1 b1 B root', |
| 134 |
] |
| 135 |
|
| 136 |
assert await cylc_list(capsys, mro=True, all_namespaces=True) == [ |
| 137 |
'A A root', |
| 138 |
'A1 A1 A root', |
| 139 |
'A11 A11 A1 A root', |
| 140 |
'A12 A12 A1 A root', |
| 141 |
'B B root', |
| 142 |
'a111 a111 A11 A1 A root', |
| 143 |
'a112 a112 A11 A1 A root', |
| 144 |
'a12 a12 A1 A root', |
| 145 |
'a121 a121 A12 A1 A root', |
| 146 |
'a2 a2 A root', |
| 147 |
'b1 b1 B root', |
| 148 |
'root root', |
| 149 |
] |
| 150 |
|
| 151 |
with pytest.raises(InputError): |
| 152 |
await cylc_list(capsys, titles=True, mro=True) |
| 153 |
|
| 154 |
|
| 155 |
async def test_tree(cylc_list, supports_utf8, capsys): |
| 156 |
"""Test the --tree option.""" |
| 157 |
assert ( |
| 158 |
await cylc_list(capsys, tree=True) |
| 159 |
# NOTE: the --all-tasks and --all-namespaces opts should be ignored |
| 160 |
== await cylc_list(capsys, tree=True, all_tasks=True) |
| 161 |
== await cylc_list(capsys, tree=True, all_namespaces=True) |
| 162 |
== [ |
| 163 |
'root ', |
| 164 |
' `-A ', |
| 165 |
' |-A1 ', |
| 166 |
' | |-A11 ', |
| 167 |
' | | |-a111 ', |
| 168 |
' | | `-a112 ', |
| 169 |
' | |-A12 ', |
| 170 |
' | | `-a121 ', |
| 171 |
' | `-a12 ', |
| 172 |
' `-a2 ', |
| 173 |
] |
| 174 |
) |
| 175 |
|
| 176 |
assert ( |
| 177 |
await cylc_list(capsys, tree=True, box=True) |
| 178 |
== await cylc_list(capsys, box=True) # --tree implicit with --box |
| 179 |
== [ |
| 180 |
'root ', |
| 181 |
' └─A ', |
| 182 |
' ├─A1 ', |
| 183 |
' │ ├─A11 ', |
| 184 |
' │ │ ├─a111 ', |
| 185 |
' │ │ └─a112 ', |
| 186 |
' │ ├─A12 ', |
| 187 |
' │ │ └─a121 ', |
| 188 |
' │ └─a12 ', |
| 189 |
' └─a2 ', |
| 190 |
] |
| 191 |
) |
| 192 |
|
| 193 |
assert await cylc_list(capsys, tree=True, titles=True) == [ |
| 194 |
'root ', |
| 195 |
' `-A ', |
| 196 |
' |-A1 ', |
| 197 |
' | |-A11 ', |
| 198 |
' | | |-a111 Title For A11', |
| 199 |
' | | `-a112 Title For A11', |
| 200 |
' | |-A12 ', |
| 201 |
' | | `-a121 Title For A', |
| 202 |
' | `-a12 Title For A', |
| 203 |
' `-a2 Title For A', |
| 204 |
] |
| 205 |
|
| 206 |
assert await cylc_list(capsys, tree=True, box=True, titles=True) == [ |
| 207 |
'root ', |
| 208 |
' └─A ', |
| 209 |
' ├─A1 ', |
| 210 |
' │ ├─A11 ', |
| 211 |
' │ │ ├─a111 Title For A11', |
| 212 |
' │ │ └─a112 Title For A11', |
| 213 |
' │ ├─A12 ', |
| 214 |
' │ │ └─a121 Title For A', |
| 215 |
' │ └─a12 Title For A', |
| 216 |
' └─a2 Title For A', |
| 217 |
] |
| 218 |
|
| 219 |
|
| 220 |
async def test_box_with_lang_c(cylc_list, does_not_support_utf8, capsys): |
| 221 |
"""It falls back to plain output if unicode support isn't there.""" |
| 222 |
assert await cylc_list(capsys, tree=True, box=True) == [ |
| 223 |
'a111', |
| 224 |
'a112', |
| 225 |
'a12', |
| 226 |
'a121', |
| 227 |
'a2', |
| 228 |
] |
| 229 |
|
| 230 |
|
| 231 |
async def test_titles(cylc_list, supports_utf8, capsys): |
| 232 |
"""Test the --titles option.""" |
| 233 |
assert await cylc_list(capsys, titles=True) == [ |
| 234 |
'a111 Title For A11', |
| 235 |
'a112 Title For A11', |
| 236 |
'a12 Title For A', |
| 237 |
'a121 Title For A', |
| 238 |
'a2 Title For A', |
| 239 |
] |
| 240 |
|
| 241 |
assert await cylc_list(capsys, titles=True, all_tasks=True) == [ |
| 242 |
'a111 Title For A11', |
| 243 |
'a112 Title For A11', |
| 244 |
'a12 Title For A', |
| 245 |
'a121 Title For A', |
| 246 |
'a2 Title For A', |
| 247 |
'b1 ', |
| 248 |
] |
| 249 |
|
| 250 |
assert await cylc_list(capsys, titles=True, all_namespaces=True) == [ |
| 251 |
'A Title For A', |
| 252 |
'A1 Title For A', |
| 253 |
'A11 Title For A11', |
| 254 |
'A12 Title For A', |
| 255 |
'B ', |
| 256 |
'a111 Title For A11', |
| 257 |
'a112 Title For A11', |
| 258 |
'a12 Title For A', |
| 259 |
'a121 Title For A', |
| 260 |
'a2 Title For A', |
| 261 |
'b1 ', |
| 262 |
'root ', |
| 263 |
] |
| 264 |
|
| 265 |
|
| 266 |
async def test_points(cylc_list, supports_utf8, capsys): |
| 267 |
"""Test the --points option.""" |
| 268 |
# specify start and stop points |
| 269 |
assert await cylc_list(capsys, prange='1,2') == [ |
| 270 |
'1/a111', |
| 271 |
'1/a112', |
| 272 |
'1/a12', |
| 273 |
'1/a121', |
| 274 |
'1/a2', |
| 275 |
'2/a111', |
| 276 |
'2/a112', |
| 277 |
'2/a12', |
| 278 |
'2/a121', |
| 279 |
'2/a2', |
| 280 |
] |
| 281 |
|
| 282 |
# leave start and stop points implicit |
| 283 |
assert await cylc_list(capsys, prange=',') == [ |
| 284 |
'1/a111', |
| 285 |
'1/a112', |
| 286 |
'1/a12', |
| 287 |
'1/a121', |
| 288 |
'1/a2', |
| 289 |
'2/a111', |
| 290 |
'2/a112', |
| 291 |
'2/a12', |
| 292 |
'2/a121', |
| 293 |
'2/a2', |
| 294 |
'3/a111', |
| 295 |
'3/a112', |
| 296 |
'3/a12', |
| 297 |
'3/a121', |
| 298 |
'3/a2', |
| 299 |
] |
| 300 |
|
| 301 |
# leave start point implicit |
| 302 |
assert await cylc_list(capsys, prange=',2') == [ |
| 303 |
'1/a111', |
| 304 |
'1/a112', |
| 305 |
'1/a12', |
| 306 |
'1/a121', |
| 307 |
'1/a2', |
| 308 |
'2/a111', |
| 309 |
'2/a112', |
| 310 |
'2/a12', |
| 311 |
'2/a121', |
| 312 |
'2/a2', |
| 313 |
] |
| 314 |
|
| 315 |
# leave stop point implicit |
| 316 |
assert await cylc_list(capsys, prange='4,') == [ |
| 317 |
'4/a111', |
| 318 |
'4/a112', |
| 319 |
'4/a12', |
| 320 |
'4/a121', |
| 321 |
'4/a2', |
| 322 |
'5/a111', |
| 323 |
'5/a112', |
| 324 |
'5/a12', |
| 325 |
'5/a121', |
| 326 |
'5/a2', |
| 327 |
'6/a111', |
| 328 |
'6/a112', |
| 329 |
'6/a12', |
| 330 |
'6/a121', |
| 331 |
'6/a2', |
| 332 |
] |
| 333 |
|
| 334 |
with pytest.raises(InputError): |
| 335 |
await cylc_list(capsys, prange='1,2', all_tasks=True) |
| 336 |
|
| 337 |
with pytest.raises(InputError): |
| 338 |
await cylc_list(capsys, prange='1,2', all_namespaces=True) |