Skip to content

Commit

Permalink
Add unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjoliver committed Mar 28, 2022
1 parent 489f4b9 commit 33d2670
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 11 additions & 3 deletions cylc/flow/scheduler_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from functools import lru_cache
from shlex import quote
import sys
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List

from cylc.flow import LOG, RSYNC_LOG
from cylc.flow.exceptions import ServiceFileError
Expand Down Expand Up @@ -360,14 +360,22 @@ def scheduler_cli(options: 'Values', workflow_id: str) -> None:
sys.exit(ret)


def _protect_remote_cmd_args(cmd: List[str]) -> List[str]:
"""Protect command args from second shell interpretation.
Escape quoting for --set="FOO='foo'" args.
(Separate function for unit testing.)
"""
return [quote(c) for c in cmd]


def _distribute(host):
"""Re-invoke this command on a different host if requested."""
# Check whether a run host is explicitly specified, else select one.
if not host:
host = select_workflow_host()[0]
if is_remote_host(host):
# quote here for (e.g.) --set='TEST="test"'
cmd = [quote(c) for c in sys.argv[1:]]
cmd = _protect_remote_cmd_args(sys.argv[1:])
# Prevent recursive host selection
cmd.append("--host=localhost")
_remote_cylc_cmd(cmd, host=host)
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_scheduler_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Tests for Cylc scheduler CLI."""

from cylc.flow.scheduler_cli import _protect_remote_cmd_args


def test__protect_remote_cmd_args():
cmd = ['cylc', 'play', '-n', '--set=FOO="foo"', 'wf']
exp = ['cylc', 'play', '-n', '\'--set=FOO="foo"\'', 'wf']
assert _protect_remote_cmd_args(cmd) == exp

0 comments on commit 33d2670

Please sign in to comment.