| 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 |
from types import SimpleNamespace |
| 18 |
|
| 19 |
from cylc.flow.async_util import pipe |
| 20 |
from cylc.flow.id import Tokens |
| 21 |
from cylc.flow.network.scan import scan |
| 22 |
from cylc.flow.scripts.completion_server import ( |
| 23 |
_list_prereqs_and_outputs, |
| 24 |
server, |
| 25 |
complete_cylc, |
| 26 |
complete_command, |
| 27 |
complete_option, |
| 28 |
complete_option_value, |
| 29 |
complete_argument, |
| 30 |
list_cylc_id, |
| 31 |
list_options, |
| 32 |
list_option_values, |
| 33 |
list_workflows, |
| 34 |
list_src_workflows, |
| 35 |
list_in_workflow, |
| 36 |
list_resources, |
| 37 |
list_dir, |
| 38 |
list_flows, |
| 39 |
list_colours, |
| 40 |
cli_detokenise, |
| 41 |
get_completion_script_file, |
| 42 |
get_current_completion_script_version, |
| 43 |
check_completion_script_compatibility, |
| 44 |
COMMANDS, |
| 45 |
) |
| 46 |
from cylc.flow.scripts.trigger import get_option_parser |
| 47 |
|
| 48 |
import pytest |
| 49 |
|
| 50 |
|
| 51 |
def setify(coro): |
| 52 |
"""Cast returned lists to sets for coroutines. |
| 53 |
|
| 54 |
Convenience function to use when you want to test output not order. |
| 55 |
""" |
| 56 |
async def _coro(*args, **kwargs): |
| 57 |
ret = await coro(*args, **kwargs) |
| 58 |
if isinstance(ret, list): |
| 59 |
return set(ret) |
| 60 |
return ret |
| 61 |
return _coro |
| 62 |
|
| 63 |
|
| 64 |
@pytest.fixture |
| 65 |
def dummy_workflow(tmp_path, monkeypatch, mock_glbl_cfg): |
| 66 |
"""A simple workflow run dir with some job directories to inspect. |
| 67 |
|
| 68 |
This patches the relevant interfaces so that this workflow will show up |
| 69 |
e.g. in the scan interface. |
| 70 |
""" |
| 71 |
install_dir = tmp_path / 'foo' |
| 72 |
install_dir.mkdir() |
| 73 |
(install_dir / 'run1').mkdir() |
| 74 |
run_dir = install_dir / 'run2' |
| 75 |
run_dir.mkdir() |
| 76 |
(install_dir / 'runN').symlink_to('run2', target_is_directory=True) |
| 77 |
(run_dir / 'flow.cylc').touch() |
| 78 |
job_log_dir = (run_dir / 'log') / 'job' |
| 79 |
job_log_dir.mkdir(parents=True) |
| 80 |
for cycle in ('1', '2', '3'): |
| 81 |
for task in ('foo', 'bar', 'baz'): |
| 82 |
for job in ('01', 'NN'): |
| 83 |
((job_log_dir / cycle) / task / job).mkdir(parents=True) |
| 84 |
|
| 85 |
# patch scan for list_workflows |
| 86 |
@pipe |
| 87 |
async def _scan(*args, **kwargs): |
| 88 |
kwargs['run_dir'] = tmp_path |
| 89 |
async for flow in scan(*args, **kwargs): |
| 90 |
yield flow |
| 91 |
|
| 92 |
monkeypatch.setattr( |
| 93 |
'cylc.flow.scripts.scan.scan', |
| 94 |
_scan, |
| 95 |
) |
| 96 |
|
| 97 |
# patch scan for list_src_workflows |
| 98 |
mock_glbl_cfg( |
| 99 |
'cylc.flow.scripts.completion_server.glbl_cfg', |
| 100 |
f''' |
| 101 |
[install] |
| 102 |
source dirs = {tmp_path} |
| 103 |
''' |
| 104 |
) |
| 105 |
|
| 106 |
# patch get_workflow_run_job_dir for list_in_workflow |
| 107 |
monkeypatch.setattr( |
| 108 |
'cylc.flow.pathutil._CYLC_RUN_DIR', |
| 109 |
tmp_path, |
| 110 |
) |
| 111 |
|
| 112 |
|
| 113 |
async def test_server(): |
| 114 |
"""Test the request/response server.""" |
| 115 |
def _listener(timeout=None): |
| 116 |
"""The listener yields requests.""" |
| 117 |
yield 'cylc|trigger|' |
| 118 |
yield 'cylc|trigger|one|' |
| 119 |
|
| 120 |
async def _responder(*parts): |
| 121 |
"""The responder computes responses.""" |
| 122 |
return [f'x{part}' for part in parts] |
| 123 |
|
| 124 |
# in "once" mode the server shuts down after returning the first response |
| 125 |
ret = [] |
| 126 |
await server(_listener, _responder, once=True, write=ret.append) |
| 127 |
assert ret == [ |
| 128 |
'xcylc xtrigger' |
| 129 |
] |
| 130 |
|
| 131 |
# otherwise the server stays up until the listener returns |
| 132 |
ret = [] |
| 133 |
await server(_listener, _responder, once=False, write=ret.append) |
| 134 |
assert ret == [ |
| 135 |
'xcylc xtrigger', |
| 136 |
'xcylc xone xtrigger', # the server sorts the responses |
| 137 |
] |
| 138 |
|
| 139 |
# if *anything* goes wrong the server should send an empty response |
| 140 |
# rather than crashing |
| 141 |
async def _responder(*parts): |
| 142 |
raise Exception() |
| 143 |
ret = [] |
| 144 |
await server(_listener, _responder, once=True, write=ret.append) |
| 145 |
assert ret == [''] |
| 146 |
|
| 147 |
|
| 148 |
async def test_complete_cylc(dummy_workflow): |
| 149 |
"""Test the completion for everything Cylc. |
| 150 |
|
| 151 |
Each individual completion function is tested individually too. This test |
| 152 |
is to ensure they work together correctly. |
| 153 |
""" |
| 154 |
_complete_cylc = setify(complete_cylc) # results are un-ordered |
| 155 |
|
| 156 |
# $ cylc<tab><tab> |
| 157 |
assert 'trigger' in await _complete_cylc('cylc') |
| 158 |
assert 'help' in await _complete_cylc('cylc') |
| 159 |
|
| 160 |
# $ cylc tri<tab><tab> |
| 161 |
assert 'trigger' in await _complete_cylc('cylc', 'tri') |
| 162 |
|
| 163 |
# $ cylc trigger<tab><tab> |
| 164 |
assert await _complete_cylc('cylc', 'trigger') == { |
| 165 |
'trigger', |
| 166 |
} |
| 167 |
|
| 168 |
# $ cylc trigger <tab><tab> |
| 169 |
assert await _complete_cylc('cylc', 'trigger', '') == { |
| 170 |
'foo/run2//', |
| 171 |
} |
| 172 |
|
| 173 |
# $ cylc trigger f<tab><tab> |
| 174 |
assert await _complete_cylc('cylc', 'trigger', 'f') == { |
| 175 |
'foo/run2//', |
| 176 |
} |
| 177 |
|
| 178 |
# $ cylc trigger foo/run2//<tab><tab> |
| 179 |
assert await _complete_cylc('cylc', 'trigger', 'foo/run2//') == { |
| 180 |
'foo/run2//1/', |
| 181 |
'foo/run2//2/', |
| 182 |
'foo/run2//3/', |
| 183 |
} |
| 184 |
|
| 185 |
# $ cylc trigger foo/run2//1<tab><tab> |
| 186 |
assert await _complete_cylc('cylc', 'trigger', 'foo/run2//1') == { |
| 187 |
'foo/run2//1/', |
| 188 |
} |
| 189 |
|
| 190 |
# $ cylc trigger foo/run2//1/<tab><tab> |
| 191 |
assert set(await _complete_cylc('cylc', 'trigger', 'foo/run2//1/')) == { |
| 192 |
'foo/run2//1/foo/', |
| 193 |
'foo/run2//1/bar/', |
| 194 |
'foo/run2//1/baz/', |
| 195 |
} |
| 196 |
|
| 197 |
# $ cylc trigger foo/run2//1/f<tab><tab> |
| 198 |
assert await _complete_cylc('cylc', 'trigger', 'foo/run2//1/f') == { |
| 199 |
'foo/run2//1/foo/', |
| 200 |
} |
| 201 |
|
| 202 |
# $ cylc trigger foo/run2//1/foo/<tab><tab> |
| 203 |
assert await _complete_cylc('cylc', 'trigger', 'foo/run2//1/foo/') == { |
| 204 |
'foo/run2//1/foo/01/', |
| 205 |
'foo/run2//1/foo/NN/', |
| 206 |
} |
| 207 |
|
| 208 |
# $ cylc trigger foo/run2//1/foo/N<tab><tab> |
| 209 |
assert await _complete_cylc('cylc', 'trigger', 'foo/run2//1/foo/N') == { |
| 210 |
'foo/run2//1/foo/NN/', |
| 211 |
} |
| 212 |
|
| 213 |
# $ cylc trigger -<tab><tab> |
| 214 |
assert '--flow' in await _complete_cylc('cylc', 'trigger', '-') |
| 215 |
|
| 216 |
# $ cylc trigger --flow <tab><tab> |
| 217 |
assert await _complete_cylc('cylc', 'trigger', '--flow', '') == { |
| 218 |
'none', |
| 219 |
'new', |
| 220 |
} |
| 221 |
|
| 222 |
# $ cylc trigger --flow <tab><tab> |
| 223 |
assert await _complete_cylc('cylc', 'trigger', '--flow', '', '') == { |
| 224 |
'foo/run2//' |
| 225 |
} |
| 226 |
|
| 227 |
# $ cylc trigger --flow=none <tab><tab> |
| 228 |
assert await _complete_cylc('cylc', 'trigger', '--flow=none', '') == { |
| 229 |
'foo/run2//' |
| 230 |
} |
| 231 |
|
| 232 |
# $ cylc trigger --flow=<tab><tab> |
| 233 |
assert await _complete_cylc('cylc', 'trigger', '--flow=') == { |
| 234 |
'--flow=none', |
| 235 |
'--flow=new', |
| 236 |
} |
| 237 |
|
| 238 |
# $ cylc trigger --flow=1 <tab><tab> |
| 239 |
assert await _complete_cylc('cylc', 'trigger', '--flow=1', '') == { |
| 240 |
'foo/run2//' |
| 241 |
} |
| 242 |
|
| 243 |
# $ cylc trigger --62656566<tab><tab> |
| 244 |
assert await _complete_cylc('cylc', 'trigger', '--62656566') == set() |
| 245 |
|
| 246 |
# $ cylc trigger 62656566 --77656C6C696E67746F6E<tab><tab> |
| 247 |
assert await _complete_cylc( |
| 248 |
'cylc', '62656566', '--77656C6C696E67746F6E=' |
| 249 |
) == set() |
| 250 |
|
| 251 |
# $ cylc cat-log f<tab><tab> |
| 252 |
assert await _complete_cylc('cylc', 'cat-log', 'f') == {'foo/run2//'} |
| 253 |
|
| 254 |
# $ cylc log f<tab><tab> # NOTE: "log" is an alias for "cat-log" |
| 255 |
assert await _complete_cylc('cylc', 'log', 'f') == {'foo/run2//'} |
| 256 |
|
| 257 |
# $ cylc help <tab><tab> |
| 258 |
assert 'all' in await _complete_cylc('cylc', 'help', '') |
| 259 |
|
| 260 |
# $ cylc version <tab><tab> |
| 261 |
assert '--long' in await _complete_cylc('cylc', 'version', '') |
| 262 |
|
| 263 |
|
| 264 |
async def test_complete_command(): |
| 265 |
"""Test completion for Cylc commands.""" |
| 266 |
ret = await complete_command('t') |
| 267 |
assert 'tui' in ret |
| 268 |
assert 'trigger' in ret |
| 269 |
|
| 270 |
ret = await complete_command('tri') |
| 271 |
assert 'tui' not in ret |
| 272 |
assert 'trigger' in ret |
| 273 |
|
| 274 |
# we should get an empty list for a non-existent command |
| 275 |
ret = await complete_command('626565662077656C6C696E67746F6E') |
| 276 |
assert ret == [] |
| 277 |
|
| 278 |
|
| 279 |
async def test_complete_option(): |
| 280 |
"""Test completion for --options of Cylc commands.""" |
| 281 |
ret = await complete_option('trigger') |
| 282 |
assert all( |
| 283 |
item.startswith('-') |
| 284 |
for item in ret |
| 285 |
) |
| 286 |
assert '--flow' in ret |
| 287 |
|
| 288 |
# we should get an empty list for a non-existent options |
| 289 |
ret = await complete_option('626565662077656C6C696E67746F6E') |
| 290 |
assert ret == [] |
| 291 |
|
| 292 |
assert await complete_option('trigger', '--flow=no') == ['--flow=none'] |
| 293 |
assert await complete_option('trigger', '--flow=ne') == ['--flow=new'] |
| 294 |
|
| 295 |
# we should get None for no existent options, this enables use to fail |
| 296 |
# over to other completion methods |
| 297 |
assert await complete_option('trigger', '--float=a') is None |
| 298 |
|
| 299 |
|
| 300 |
async def test_option_value(): |
| 301 |
"""Test completion for values of --options of Cylc commands.""" |
| 302 |
ret = await complete_option_value('trigger', '--flow') |
| 303 |
assert 'none' in ret |
| 304 |
|
| 305 |
ret = await complete_option_value('trigger', '--flow', 'a') |
| 306 |
assert 'none' not in ret |
| 307 |
|
| 308 |
# we should get None for no existent values, this enables use to fail |
| 309 |
# over to other completion methods |
| 310 |
ret = await complete_option_value('626565662077656C6C696E67746F6E', 'x') |
| 311 |
assert ret is None |
| 312 |
|
| 313 |
ret = await complete_option_value('trigger', '626565662077656C6C696E6774') |
| 314 |
assert ret is None |
| 315 |
|
| 316 |
|
| 317 |
async def test_complete_argument(monkeypatch): |
| 318 |
"""Test completions for positional arguments of Cylc commands.""" |
| 319 |
# register two fake commands with their own special completions |
| 320 |
def _complete_arg(x): |
| 321 |
async def __complete_arg(*args): |
| 322 |
return x |
| 323 |
return __complete_arg |
| 324 |
|
| 325 |
monkeypatch.setattr( |
| 326 |
'cylc.flow.scripts.completion_server.COMMAND_MAP', |
| 327 |
{ |
| 328 |
'foo': _complete_arg(['aaa', 'bbb']), |
| 329 |
'bar': _complete_arg(['ccc', 'ddd']), |
| 330 |
'baz': None, |
| 331 |
} |
| 332 |
) |
| 333 |
|
| 334 |
# patch the default ID listing completion |
| 335 |
async def _list_cylc_id(*args): |
| 336 |
return ['eee', 'fff'] |
| 337 |
|
| 338 |
monkeypatch.setattr( |
| 339 |
'cylc.flow.scripts.completion_server.list_cylc_id', |
| 340 |
_list_cylc_id |
| 341 |
) |
| 342 |
|
| 343 |
# the foo command should provide foo-specific completions |
| 344 |
ret = await complete_argument('foo') |
| 345 |
assert ret == ['aaa', 'bbb'] |
| 346 |
|
| 347 |
ret = await complete_argument('foo', 'a') |
| 348 |
assert ret == ['aaa'] |
| 349 |
|
| 350 |
# the bar command should provide bar-specific completions |
| 351 |
ret = await complete_argument('bar') |
| 352 |
assert ret == ['ccc', 'ddd'] |
| 353 |
|
| 354 |
# the bax command should not provide argument completions |
| 355 |
ret = await complete_argument('baz') |
| 356 |
assert ret == [] |
| 357 |
|
| 358 |
# all other commands should fallback to the default completions |
| 359 |
ret = await complete_argument('pub') |
| 360 |
assert ret == ['eee', 'fff'] |
| 361 |
|
| 362 |
|
| 363 |
async def test_list_cylc_id(monkeypatch): |
| 364 |
"""Test listing Cylc IDs. |
| 365 |
|
| 366 |
This test ensures that list_cylc_ids is calling the right interfaces. |
| 367 |
""" |
| 368 |
_list_cylc_id = setify(list_cylc_id) |
| 369 |
|
| 370 |
async def _list_workflows(): |
| 371 |
return ['abc', 'bcd', 'cde'] |
| 372 |
|
| 373 |
async def _list_in_workflow(tokens): |
| 374 |
if tokens.workflow_id == 'abc': |
| 375 |
return ['1', '2'] |
| 376 |
return ['2', '3'] |
| 377 |
|
| 378 |
monkeypatch.setattr( |
| 379 |
'cylc.flow.scripts.completion_server.list_workflows', |
| 380 |
_list_workflows, |
| 381 |
) |
| 382 |
monkeypatch.setattr( |
| 383 |
'cylc.flow.scripts.completion_server.list_in_workflow', |
| 384 |
_list_in_workflow, |
| 385 |
) |
| 386 |
|
| 387 |
assert await _list_cylc_id(None) == {'abc', 'bcd', 'cde'} |
| 388 |
assert await _list_cylc_id('a') == {'abc', 'bcd', 'cde'} |
| 389 |
assert await _list_cylc_id('abc//') == {'1', '2'} |
| 390 |
assert await _list_cylc_id('bcd//') == {'2', '3'} |
| 391 |
|
| 392 |
|
| 393 |
def test_list_options(monkeypatch): |
| 394 |
"""Test listing of command --options.""" |
| 395 |
assert '--flow' in list_options('trigger') |
| 396 |
assert '--color' in list_options('trigger') |
| 397 |
# we should get an empty list if anything goes wrong |
| 398 |
assert list_options('zz9+za') == [] |
| 399 |
|
| 400 |
# patch the logic to turn off the auto_add behaviour of CylcOptionParser |
| 401 |
class EntryPoint: |
| 402 |
def load(self): |
| 403 |
def _parser_function(): |
| 404 |
parser = get_option_parser() |
| 405 |
del parser.auto_add |
| 406 |
return parser |
| 407 |
return SimpleNamespace(parser_function=_parser_function) |
| 408 |
monkeypatch.setitem( |
| 409 |
COMMANDS, |
| 410 |
'trigger', |
| 411 |
EntryPoint(), |
| 412 |
) |
| 413 |
|
| 414 |
# with auto_add turned off the --color option should be absent |
| 415 |
assert '--color' not in list_options('trigger') |
| 416 |
|
| 417 |
|
| 418 |
async def test_list_option_values(monkeypatch): |
| 419 |
"""Test listing of --option values.""" |
| 420 |
_list_option_values = setify(list_option_values) |
| 421 |
|
| 422 |
async def _list_a_options(*args): |
| 423 |
return ['foo', 'bar', 'baz'] |
| 424 |
|
| 425 |
# register two options |
| 426 |
monkeypatch.setattr( |
| 427 |
'cylc.flow.scripts.completion_server.OPTION_MAP', |
| 428 |
{ |
| 429 |
# --a has a registered completion |
| 430 |
'--a': _list_a_options, |
| 431 |
# --b has completions explicitly turned off |
| 432 |
'--b': None, |
| 433 |
# --c is not registered |
| 434 |
}, |
| 435 |
) |
| 436 |
|
| 437 |
assert await _list_option_values(None, '--a', None) == { |
| 438 |
'foo', |
| 439 |
'bar', |
| 440 |
'baz' |
| 441 |
} |
| 442 |
assert await _list_option_values(None, '--b', None) == set() |
| 443 |
assert await _list_option_values(None, '--c', None) is None |
| 444 |
|
| 445 |
|
| 446 |
async def test_list_workflows(dummy_workflow): |
| 447 |
"""Test listing workflows (via "scan").""" |
| 448 |
# test list_workflows |
| 449 |
assert await list_workflows() == ['foo/run2//'] |
| 450 |
assert await list_workflows(states={'running'}) == [] |
| 451 |
|
| 452 |
# test list_src_workflows |
| 453 |
assert await list_src_workflows(None) == ['foo/run2'] |
| 454 |
|
| 455 |
|
| 456 |
async def test_list_in_workflow(dummy_workflow): |
| 457 |
"""Test listing of "things" within workflows. |
| 458 |
|
| 459 |
Things i.e. cycles/tasks/jobs. |
| 460 |
""" |
| 461 |
_list_in_workflow = setify(list_in_workflow) |
| 462 |
|
| 463 |
# workflow => list cycles |
| 464 |
assert await _list_in_workflow(Tokens('foo/run2//')) == { |
| 465 |
'foo/run2//1/', |
| 466 |
'foo/run2//2/', |
| 467 |
'foo/run2//3/', |
| 468 |
} |
| 469 |
# cycle => list tasks |
| 470 |
assert await _list_in_workflow(Tokens('foo/run2//1')) == { |
| 471 |
'foo/run2//1/foo/', |
| 472 |
'foo/run2//1/bar/', |
| 473 |
'foo/run2//1/baz/', |
| 474 |
} |
| 475 |
# task => list jobs |
| 476 |
assert await _list_in_workflow(Tokens('foo/run2//1/foo/')) == { |
| 477 |
'foo/run2//1/foo/01/', |
| 478 |
'foo/run2//1/foo/NN/', |
| 479 |
} |
| 480 |
# jobs => nothing to do |
| 481 |
assert await _list_in_workflow( |
| 482 |
Tokens('foo/run2//1/foo/01'), |
| 483 |
) == set() |
| 484 |
|
| 485 |
# no tokens => nothing to list |
| 486 |
assert await _list_in_workflow(Tokens()) == set() |
| 487 |
# non-existant workflow => nothing to list |
| 488 |
assert await _list_in_workflow( |
| 489 |
Tokens('forty-two'), |
| 490 |
# set infer_run to false as this workflow does not exist so will raise |
| 491 |
# an exception |
| 492 |
# (note exceptions are fine, they get caught and ignored at the top |
| 493 |
# level) |
| 494 |
infer_run=False |
| 495 |
) == set() |
| 496 |
# non-existant cycle => nothing to list |
| 497 |
assert await _list_in_workflow(Tokens('foo/run2//4')) == set() |
| 498 |
# non-existant task => nothing to list |
| 499 |
assert await _list_in_workflow(Tokens('foo/run2//4/foo')) == set() |
| 500 |
# non-existant job => nothing to list |
| 501 |
assert await _list_in_workflow(Tokens('foo/run2//4/foo/02')) == set() |
| 502 |
|
| 503 |
|
| 504 |
async def test_list_in_workflow_inference(dummy_workflow): |
| 505 |
"""It should infer the latest run when appropriate.""" |
| 506 |
_list_in_workflow = setify(list_in_workflow) |
| 507 |
|
| 508 |
assert await _list_in_workflow(Tokens('foo/run2//')) == { |
| 509 |
'foo/run2//1/', |
| 510 |
'foo/run2//2/', |
| 511 |
'foo/run2//3/', |
| 512 |
} |
| 513 |
assert await _list_in_workflow(Tokens('foo//')) == { |
| 514 |
'foo//1/', |
| 515 |
'foo//2/', |
| 516 |
'foo//3/', |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
async def test_list_resources(): |
| 521 |
"""Test listing of "resources. |
| 522 |
|
| 523 |
Resources i.e. things provided by `cylc get-resources`. |
| 524 |
""" |
| 525 |
assert 'cylc-completion.bash' in await list_resources(None) |
| 526 |
|
| 527 |
|
| 528 |
async def test_list_dir(tmp_path, monkeypatch): |
| 529 |
"""Test directory listing.""" |
| 530 |
(tmp_path / 'x').mkdir() |
| 531 |
((tmp_path / 'x') / 'y').mkdir() |
| 532 |
((tmp_path / 'x') / 'z').touch() |
| 533 |
monkeypatch.chdir(tmp_path) |
| 534 |
|
| 535 |
_list_dir = setify(list_dir) |
| 536 |
|
| 537 |
# --- relative paths --- |
| 538 |
|
| 539 |
# no "partial" |
| 540 |
# => list $PWD |
| 541 |
assert { |
| 542 |
str(path) |
| 543 |
for path in await _list_dir(None) |
| 544 |
} == {'x/'} |
| 545 |
|
| 546 |
# no trailing `/` at the end of the path |
| 547 |
# (i.e. an incomplete path) |
| 548 |
# => list the parent |
| 549 |
assert { |
| 550 |
str(path) |
| 551 |
for path in await _list_dir('x') |
| 552 |
} == {'x/'} |
| 553 |
|
| 554 |
# # trailing `/` at the end of the path |
| 555 |
# # (i.e. complete path) |
| 556 |
# # => list dir path |
| 557 |
assert { |
| 558 |
str(path) |
| 559 |
for path in await _list_dir('x/') |
| 560 |
} == {'x/y/', 'x/z'} # "y" is a dir, "z" is a file |
| 561 |
|
| 562 |
# listing a file |
| 563 |
# => noting to list, just return the file |
| 564 |
assert { |
| 565 |
str(path) |
| 566 |
for path in await _list_dir('x/z/') |
| 567 |
} == {'x/z'} |
| 568 |
|
| 569 |
# --- absolute paths --- |
| 570 |
|
| 571 |
# no trailing `/` at the end of the path |
| 572 |
# (i.e. an incomplete path) |
| 573 |
# => list the parent |
| 574 |
assert { |
| 575 |
# '/'.join(path.rsplit('/', 2)[-2:]) |
| 576 |
path.replace(str(tmp_path), '') |
| 577 |
for path in await _list_dir(str(tmp_path / 'x')) |
| 578 |
} == {'/x/'} |
| 579 |
|
| 580 |
# trailing `/` at the end of the path |
| 581 |
# (i.e. complete path) |
| 582 |
# => list dir path |
| 583 |
assert { |
| 584 |
path.replace(str(tmp_path), '') |
| 585 |
for path in await _list_dir(str(tmp_path / 'x') + '/') |
| 586 |
} == {'/x/y/', '/x/z'} # "y" is a dir, "z" is a file |
| 587 |
|
| 588 |
# listing a file |
| 589 |
# => noting to list, just return the file |
| 590 |
assert { |
| 591 |
path.replace(str(tmp_path), '') |
| 592 |
for path in await _list_dir(str(tmp_path / 'x' / 'z') + '/') |
| 593 |
} == {'/x/z'} |
| 594 |
|
| 595 |
|
| 596 |
async def test_list_flows(): |
| 597 |
"""Test listing values for the --flow option. |
| 598 |
|
| 599 |
Currently this only provides the textual options i.e. it doesn't list |
| 600 |
"flows" running in a workflow, yet... |
| 601 |
""" |
| 602 |
assert 'none' in await list_flows(None) |
| 603 |
|
| 604 |
|
| 605 |
async def test_list_colours(): |
| 606 |
"""Test listing values for the --color option.""" |
| 607 |
assert 'always' in await list_colours(None) |
| 608 |
|
| 609 |
|
| 610 |
async def test_cli_detokenise(): |
| 611 |
"""Test that Cylc IDs are detokenised with a trailing slash. |
| 612 |
|
| 613 |
Cylc completion used the trailing slash to determine that the previous |
| 614 |
part of the ID has been completed the same way as regular directory |
| 615 |
completion does. |
| 616 |
""" |
| 617 |
|
| 618 |
assert cli_detokenise(Tokens()) == '' |
| 619 |
assert cli_detokenise(Tokens('~u/w')) == '~u/w//' |
| 620 |
assert cli_detokenise(Tokens('~u/w//c')) == '~u/w//c/' |
| 621 |
assert cli_detokenise(Tokens('~u/w//c/t')) == '~u/w//c/t/' |
| 622 |
assert cli_detokenise(Tokens('~u/w//c/t/01')) == '~u/w//c/t/01/' |
| 623 |
|
| 624 |
|
| 625 |
def test_get_completion_script_file(): |
| 626 |
"""Test retrieving the completion script file.""" |
| 627 |
assert get_completion_script_file('bash').exists() |
| 628 |
# if the requested language is not supported it should return None |
| 629 |
assert get_completion_script_file('ksh') is None |
| 630 |
|
| 631 |
|
| 632 |
def test_get_current_completion_script_version(tmp_path): |
| 633 |
"""Test extracting the completion script version from the script file. |
| 634 |
|
| 635 |
We do this to determine the version of the completion script bundled with |
| 636 |
this version of Cylc (in order to inform users of upgrades). |
| 637 |
""" |
| 638 |
completion_script = get_completion_script_file('bash') |
| 639 |
|
| 640 |
# it should extract the version for bash |
| 641 |
assert get_current_completion_script_version( |
| 642 |
completion_script, |
| 643 |
'bash', |
| 644 |
) is not None |
| 645 |
|
| 646 |
# it should return None for ksh |
| 647 |
assert get_current_completion_script_version( |
| 648 |
completion_script, |
| 649 |
'ksh', |
| 650 |
) is None |
| 651 |
|
| 652 |
# it should return None if it can't find the version |
| 653 |
completion_script = (tmp_path / 'foo') |
| 654 |
completion_script.touch() |
| 655 |
assert get_current_completion_script_version( |
| 656 |
completion_script, |
| 657 |
'bash', |
| 658 |
) is None |
| 659 |
|
| 660 |
|
| 661 |
def test_check_completion_script_compatibility(monkeypatch, capsys): |
| 662 |
"""Test whether a completion script is compatible with the server. |
| 663 |
|
| 664 |
Incase the server interface changes at a later date this will allow us to |
| 665 |
exit gracefully rather than crashing in a horrible way. |
| 666 |
""" |
| 667 |
# set the bash completion script version to 1.0.1 |
| 668 |
def _get_current_completion_script_version(_script, lang): |
| 669 |
if lang == 'bash': |
| 670 |
return '1.0.1' |
| 671 |
return None |
| 672 |
|
| 673 |
# set the completion script compatibility range to >=1.0.0, <2.0.0 |
| 674 |
monkeypatch.setattr( |
| 675 |
'cylc.flow.scripts.completion_server.REQUIRED_SCRIPT_VERSION', |
| 676 |
'>=1.0.0, <2.0.0', |
| 677 |
) |
| 678 |
monkeypatch.setattr( |
| 679 |
'cylc.flow.scripts.completion_server' |
| 680 |
'.get_current_completion_script_version', |
| 681 |
_get_current_completion_script_version |
| 682 |
) |
| 683 |
|
| 684 |
# versions which match ">=1.0.0, <2.0.0" should be valid |
| 685 |
assert check_completion_script_compatibility('bash', '0.9.9') is False |
| 686 |
assert check_completion_script_compatibility('bash', '1.0.0') is True |
| 687 |
assert check_completion_script_compatibility('bash', '1.0.1') is True |
| 688 |
assert check_completion_script_compatibility('bash', '1.0.2') is True |
| 689 |
assert check_completion_script_compatibility('bash', '2.0.0') is False |
| 690 |
|
| 691 |
# all versions should be invalid (because we don't offer ksh support) |
| 692 |
assert check_completion_script_compatibility('ksh', '0.9.9') is False |
| 693 |
assert check_completion_script_compatibility('ksh', '1.0.0') is False |
| 694 |
assert check_completion_script_compatibility('ksh', '1.0.1') is False |
| 695 |
assert check_completion_script_compatibility('ksh', '1.0.2') is False |
| 696 |
assert check_completion_script_compatibility('ksh', '2.0.0') is False |
| 697 |
|
| 698 |
# it should tell the user when a new version of the script is available |
| 699 |
capsys.readouterr() # clear |
| 700 |
assert check_completion_script_compatibility('bash', '1.0.0') is True |
| 701 |
out, err = capsys.readouterr() |
| 702 |
assert not out # never write to stdout |
| 703 |
assert 'A new version of the Cylc bash script is available' in err |
| 704 |
|
| 705 |
# it should tell the user if the script is incompatible |
| 706 |
capsys.readouterr() # clear |
| 707 |
assert check_completion_script_compatibility('bash', '0.9.9') is False |
| 708 |
out, err = capsys.readouterr() |
| 709 |
assert not out # never write to stdout |
| 710 |
assert 'The Cylc bash script needs to be updated' in err |
| 711 |
|
| 712 |
# it shouldn't say anything unless necessary |
| 713 |
capsys.readouterr() # clear |
| 714 |
assert check_completion_script_compatibility('bash', '1.0.1') is True |
| 715 |
out, err = capsys.readouterr() |
| 716 |
assert not out # never write to stdout |
| 717 |
assert not err |
| 718 |
|
| 719 |
|
| 720 |
async def test_prereqs_and_outputs(): |
| 721 |
"""Test the error cases for listing task prereqs/outputs. |
| 722 |
|
| 723 |
The success cases are tested in an integration test (requires a running |
| 724 |
scheduler). |
| 725 |
""" |
| 726 |
# if no tokens are provided, no prereqs or outputs are returned |
| 727 |
assert await _list_prereqs_and_outputs([]) == ([], []) |
| 728 |
|
| 729 |
# if an invalid workflow is provided, we can't list anything |
| 730 |
assert await _list_prereqs_and_outputs( |
| 731 |
[Tokens(workflow='no-such-workflow')] |
| 732 |
) == ([], []) |