Skip to content

Commit

Permalink
cylc scan: add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-sanders committed Aug 7, 2020
1 parent 8bd7582 commit 48be6ca
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 194 deletions.
63 changes: 44 additions & 19 deletions cylc/flow/scripts/cylc_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@
graphql_query,
filter_name
)
from cylc.flow.option_parsers import CylcOptionParser as COP
from cylc.flow.option_parsers import (
CylcOptionParser as COP,
Options
)
from cylc.flow.print_tree import print_tree
from cylc.flow.suite_files import ContactFileFields as Cont
from cylc.flow.terminal import cli_function
Expand Down Expand Up @@ -250,14 +253,17 @@ def _format_rich(flow, opts):
**{
name: flow[key]
for name, key in (
('cylcVersion', 'cylcVersion'),
('version', 'cylcVersion'),
('host', Cont.HOST),
('port', Cont.PORT)
)
}
}
maxlen = max(len(key) for key in display)
for key, value in display.items():
# format multiline strings by whitespace padding the lines
value = ('\n' + (' ' * (maxlen + 7))).join(value.splitlines())
# write out the key: value pairs
ret.append(f' {key: <{maxlen}} {value}')
return '\n'.join(ret)

Expand All @@ -274,30 +280,30 @@ def sort_function(flow):
return (state, flow['name'])


async def _sorted(pipe, formatter, opts):
async def _sorted(pipe, formatter, opts, write):
"""List all flows, sort, then print them individually."""
ret = []
async for item in pipe:
ret.append(item)
for flow in sorted(ret, key=sort_function):
cprint(formatter(flow, opts))
write(formatter(flow, opts))


async def _serial(pipe, formatter, opts):
async def _serial(pipe, formatter, opts, write):
"""List all flows, then print them as one."""
ret = []
async for item in pipe:
ret.append(item)
cprint(formatter(ret, opts))
write(formatter(ret, opts))


async def _async(pipe, formatter, opts):
async def _async(pipe, formatter, opts, write):
"""List and print flows individually."""
async for flow in pipe:
cprint(formatter(flow, opts))
write(formatter(flow, opts))


async def _tree(pipe, formatter, opts):
async def _tree(pipe, formatter, opts, write):
"""List all flows, sort, then print them as a tree."""
# get list of flows
ret = []
Expand All @@ -321,12 +327,15 @@ async def _tree(pipe, formatter, opts):

# print tree
ret = print_tree(tree, '', sort=False, use_unicode=True)
cprint('\n'.join(ret))
write('\n'.join(ret))


def get_pipe(opts, formatter):
def get_pipe(opts, formatter, scan_dir=None):
"""Construct a pipe for listing flows."""
pipe = scan
if scan_dir:
pipe = scan(scan_dir=scan_dir)
else:
pipe = scan

show_running = 'running' in opts.states
show_held = 'held' in opts.states
Expand Down Expand Up @@ -398,19 +407,24 @@ def get_formatter(opts):
return formatter, method


async def scanner(opts):
async def scanner(opts, write, scan_dir=None):
"""Print workflows to stdout."""
formatter, method = get_formatter(opts)
pipe = get_pipe(opts, formatter)
pipe = get_pipe(opts, formatter, scan_dir)

LOG.debug(f'pipe: {repr(pipe)}')

await method(pipe, formatter, opts)
await method(pipe, formatter, opts, write)


@cli_function(get_option_parser)
def main(parser, opts, color):
"""Implement `cylc scan`."""
async def main(opts, color=False, scan_dir=None, write=cprint):
"""Open up a Python API for testing purposes.
Note:
Don't use this API for anything other than testing, there is a
proper Python API for these purposes.
"""
# validate / standardise the list of workflow states
opts.states = set(opts.states.split(','))
if 'all' in opts.states:
Expand All @@ -430,4 +444,15 @@ def main(parser, opts, color):
if opts.format == 'rich' and not opts.colour_blind:
cprint(state_totals_key() + '\n')

asyncio.run(scanner(opts))
await scanner(opts, write, scan_dir)


@cli_function(get_option_parser)
def cli(_, opts, color):
"""Implement `cylc scan`."""
asyncio.run(
main(opts, color)
)


ScanOptions = Options(get_option_parser())
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ console_scripts =
cylc-report-timings = cylc.flow.scripts.cylc_report_timings:main
cylc-restart = cylc.flow.scripts.cylc_restart:main
cylc-run = cylc.flow.scripts.cylc_run:main
cylc-scan = cylc.flow.scripts.cylc_scan:main
cylc-scan = cylc.flow.scripts.cylc_scan:cli
cylc-search = cylc.flow.scripts.cylc_search:main
cylc-set-verbosity = cylc.flow.scripts.cylc_set_verbosity:main
cylc-show = cylc.flow.scripts.cylc_show:main
Expand Down
62 changes: 0 additions & 62 deletions tests/functional/cylc-scan/01-scan.t

This file was deleted.

112 changes: 0 additions & 112 deletions tests/functional/cylc-scan/04-outputs.t

This file was deleted.

Loading

0 comments on commit 48be6ca

Please sign in to comment.