Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

play: preserve colour formatting when detaching #5461

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ ones in. -->

### Enhancements

[#5461](https://github.com/cylc/cylc-flow/pull/5461) - Preserve colour
formatting when starting workflows in distributed mode using `run hosts`.

[#5291](https://github.com/cylc/cylc-flow/pull/5291) - re-implement old-style
clock triggers as wall_clock xtriggers.

Expand Down
13 changes: 11 additions & 2 deletions cylc/flow/scheduler_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def scheduler_cli(options: 'Values', workflow_id_raw: str) -> None:
_upgrade_database(db_file)

# re-execute on another host if required
_distribute(options.host, workflow_id_raw, workflow_id)
_distribute(options.host, workflow_id_raw, workflow_id, options.color)

# print the start message
_print_startup_message(options)
Expand Down Expand Up @@ -557,7 +557,7 @@ def _print_startup_message(options):
LOG.warning(SUITERC_DEPR_MSG)


def _distribute(host, workflow_id_raw, workflow_id):
def _distribute(host, workflow_id_raw, workflow_id, color):
"""Re-invoke this command on a different host if requested.

Args:
Expand Down Expand Up @@ -589,6 +589,15 @@ def _distribute(host, workflow_id_raw, workflow_id):
# Prevent recursive host selection
cmd.append("--host=localhost")

# Preserve CLI colour
if is_terminal() and color != 'never':
# the detached process doesn't pass the is_terminal test
# so we have to explicitly tell Cylc to use color
cmd.append('--color=always')
else:
# otherwise set --color=never to make testing easier
cmd.append('--color=never')

# Re-invoke the command
# NOTE: has the potential to raise NoHostsError, however, this will
# most likely have been raised during host-selection
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_scheduler_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from cylc.flow.exceptions import ServiceFileError
from cylc.flow.scheduler_cli import (
_distribute,
_version_check,
)

Expand Down Expand Up @@ -222,3 +223,39 @@ def test_version_check_no_db(tmp_path):
"""It should pass if there is no DB file (e.g. on workflow first start)."""
db_file = tmp_path / 'db' # non-existent file
assert _version_check(db_file, False, False)


@pytest.mark.parametrize(
'cli_colour, is_terminal, distribute_colour',
[
('never', True, '--color=never'),
('auto', True, '--color=always'),
('always', True, '--color=always'),
('never', False, '--color=never'),
('auto', False, '--color=never'),
('always', False, '--color=never'),
]
)
def test_distribute_colour(
monkeymock,
cli_colour,
is_terminal,
distribute_colour,
):
"""It should start detached workflows with the correct --colour option.

The is_terminal test will fail for detached scheduler processes which means
that the colour formatting will be stripped for startup. This includes
the Cylc header logo and any warnings/errors raised during config parsing.

In order to preserver colour formatting we must set the `--colour` arg to
`always` when we want the detached process to start in colour mode.

See https://github.com/cylc/cylc-flow/issues/5159
"""
monkeymock('cylc.flow.scheduler_cli.sys.exit')
_is_terminal = monkeymock('cylc.flow.scheduler_cli.is_terminal')
_is_terminal.return_value = is_terminal
_cylc_server_cmd = monkeymock('cylc.flow.scheduler_cli.cylc_server_cmd')
_distribute('myhost', 'foo', 'foo/run1', cli_colour)
assert distribute_colour in _cylc_server_cmd.call_args[0][0]