Skip to content

Commit

Permalink
Use quotes to improve accuracy of logged commands #169
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-n committed Sep 30, 2023
1 parent ad92424 commit 21c523e
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 47 deletions.
2 changes: 1 addition & 1 deletion poethepoet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
try:
import tomllib as tomli
except ImportError:
import tomli
import tomli # type: ignore[no-redef]

from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union

Expand Down
3 changes: 2 additions & 1 deletion poethepoet/task/cmd.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shlex
from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Tuple, Type, Union

from ..exceptions import PoeException
Expand Down Expand Up @@ -41,7 +42,7 @@ def _handle_run(

cmd = (*self._resolve_args(context, env), *extra_args)

self._print_action(" ".join(cmd), context.dry)
self._print_action(shlex.join(cmd), context.dry)

return context.get_executor(self.invocation, env, self.options).execute(
cmd, use_exec=self.options.get("use_exec", False)
Expand Down
3 changes: 2 additions & 1 deletion poethepoet/task/script.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import shlex
from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Tuple, Type, Union

from ..exceptions import ExpressionParseError
Expand Down Expand Up @@ -65,7 +66,7 @@ def _handle_run(
# windows
cmd = ("python", "-c", "".join(script))

self._print_action(" ".join(argv), context.dry)
self._print_action(shlex.join(argv), context.dry)
return context.get_executor(self.invocation, env, self.options).execute(
cmd, use_exec=self.options.get("use_exec", False)
)
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/scripts_project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ default_array_item_task_type = "cmd"

[tool.poe.tasks]
# Interpret subtasks as cmd instead of ref
composite_task = ["poe_test_echo Hello", "poe_test_echo World!"]
composite_task = ["poe_test_echo Hello", "poe_test_echo 'World!'"]

# test_setting_default_task_type
echo-args = "pkg:echo_args"
Expand Down
30 changes: 21 additions & 9 deletions tests/test_cmd_tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
def test_call_echo_task(run_poe_subproc, projects, esc_prefix):
def test_call_echo_task(run_poe_subproc, projects, esc_prefix, is_windows):
result = run_poe_subproc("echo", "foo", "!", project="cmds")
assert result.capture == (
"Poe => poe_test_echo "
f"POE_ROOT:{projects['cmds']} Password1, task_args: foo !\n"
)
if is_windows:
assert result.capture == (
"Poe => poe_test_echo "
f"'POE_ROOT:{projects['cmds']}' Password1, task_args: foo '!'\n"
)
else:
assert result.capture == (
"Poe => poe_test_echo "
f"POE_ROOT:{projects['cmds']} Password1, task_args: foo '!'\n"
)

assert result.stdout == f"POE_ROOT:{projects['cmds']} Password1, task_args: foo !\n"
assert result.stderr == ""

Expand Down Expand Up @@ -34,7 +41,7 @@ def test_cmd_task_with_args_and_extra_args(run_poe_subproc):
"!",
project="cmds",
)
assert result.capture == "Poe => poe_test_echo hey you guy !\n"
assert result.capture == "Poe => poe_test_echo hey you guy '!'\n"
assert result.stdout == "hey you guy !\n"
assert result.stderr == ""

Expand All @@ -50,7 +57,7 @@ def test_cmd_alias_env_var(run_poe_subproc):

def test_cmd_task_with_multiple_value_arg(run_poe_subproc):
result = run_poe_subproc("multiple-value-arg", "hey", "1", "2", "3", project="cmds")
assert result.capture == "Poe => poe_test_echo first: hey second: 1 2 3\n"
assert result.capture == "Poe => poe_test_echo 'first: hey second: 1 2 3'\n"
assert result.stdout == "first: hey second: 1 2 3\n"
assert result.stderr == ""

Expand Down Expand Up @@ -114,7 +121,9 @@ def test_cmd_task_with_cwd_option_arg(run_poe_subproc, poe_project_path):
assert result.stderr == ""


def test_cmd_task_with_with_glob_arg_and_cwd(run_poe_subproc, poe_project_path):
def test_cmd_task_with_with_glob_arg_and_cwd(
run_poe_subproc, poe_project_path, is_windows
):
result = run_poe_subproc("ls", "--path-arg", "./subdir", project="cwd")
assert result.capture == "Poe => ls ./subdir\n"
assert result.stdout == "bar\nfoo\n"
Expand All @@ -129,6 +138,9 @@ def test_cmd_task_with_with_glob_arg_and_cwd(run_poe_subproc, poe_project_path):
"ls", "--path-arg", "./f*", "--cwd-arg", "subdir", project="cwd"
)
assert result.capture.startswith("Poe => ls ")
assert result.capture.endswith("foo\n")
if is_windows:
assert result.capture.endswith("foo'\n")
else:
assert result.capture.endswith("foo\n")
assert result.stdout == "bar.txt\n"
assert result.stderr == ""
7 changes: 5 additions & 2 deletions tests/test_default_value.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def test_global_envfile_and_default(run_poe_subproc):
result = run_poe_subproc("test", project="default_value")
assert (
"Poe => poe_test_echo !one! !two! !three! !four! !five! !six!\n"
"Poe => poe_test_echo '!one!' '!two!' '!three!' '!four!' '!five!' '!six!'\n"
in result.capture
)
assert result.stdout == "!one! !two! !three! !four! !five! !six!\n"
Expand All @@ -19,6 +19,9 @@ def test_global_envfile_and_default_with_presets(run_poe_subproc):
}

result = run_poe_subproc("test", project="default_value", env=env)
assert "Poe => poe_test_echo !one! !two! !three! 444 !five! 666\n" in result.capture
assert (
"Poe => poe_test_echo '!one!' '!two!' '!three!' 444 '!five!' 666\n"
in result.capture
)
assert result.stdout == "!one! !two! !three! 444 !five! 666\n"
assert result.stderr == ""
7 changes: 4 additions & 3 deletions tests/test_envfile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def test_global_envfile_and_default(run_poe_subproc):
result = run_poe_subproc("deploy-dev", project="envfile")
assert (
"Poe => poe_test_echo deploying to admin:12345@dev.example.com:8080\n"
"Poe => poe_test_echo 'deploying to admin:12345@dev.example.com:8080'\n"
in result.capture
)
assert result.stdout == "deploying to admin:12345@dev.example.com:8080\n"
Expand All @@ -11,7 +11,7 @@ def test_global_envfile_and_default(run_poe_subproc):
def test_task_envfile_and_default(run_poe_subproc):
result = run_poe_subproc("deploy-prod", project="envfile")
assert (
"Poe => poe_test_echo deploying to admin:12345@prod.example.com/app\n"
"Poe => poe_test_echo 'deploying to admin:12345@prod.example.com/app'\n"
in result.capture
)
assert result.stdout == "deploying to admin:12345@prod.example.com/app\n"
Expand All @@ -24,7 +24,8 @@ def test_multiple_envfiles(run_poe_subproc, projects):
)

assert (
"Poe => poe_test_echo VAL_A-VAL_B-VAL_C-VAL_D-VAL_E-VAL_F!!\n" in result.capture
"Poe => poe_test_echo 'VAL_A-VAL_B-VAL_C-VAL_D-VAL_E-VAL_F!!'\n"
in result.capture
)
assert result.stdout == "VAL_A-VAL_B-VAL_C-VAL_D-VAL_E-VAL_F!!\n"
assert result.stderr == ""
8 changes: 4 additions & 4 deletions tests/test_includes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ def test_docs_for_include_toml_file(run_poe_subproc):

def test_run_task_included_from_toml_file(run_poe_subproc):
result = run_poe_subproc("greet", "Whirl!", project="includes")
assert result.capture == "Poe => poe_test_echo Hello Whirl!\n"
assert result.capture == "Poe => poe_test_echo Hello 'Whirl!'\n"
assert result.stdout == "Hello Whirl!\n"
assert result.stderr == ""


def test_run_task_not_included_from_toml_file(run_poe_subproc):
result = run_poe_subproc("echo", "Whirl!", project="includes")
assert result.capture == "Poe => poe_test_echo Whirl!\n"
assert result.capture == "Poe => poe_test_echo 'Whirl!'\n"
assert result.stdout == "Whirl!\n"
assert result.stderr == ""

Expand Down Expand Up @@ -49,14 +49,14 @@ def test_running_from_multiple_includes(run_poe_subproc, projects):
"Whirl!",
project="includes",
)
assert result.capture == "Poe => poe_test_echo Whirl!\n"
assert result.capture == "Poe => poe_test_echo 'Whirl!'\n"
assert result.stdout == "Whirl!\n"
assert result.stderr == ""

result = run_poe_subproc(
f'--root={projects["includes/multiple_includes"]}', "greet", "Whirl!"
)
assert result.capture == "Poe => poe_test_echo Hello Whirl!\n"
assert result.capture == "Poe => poe_test_echo Hello 'Whirl!'\n"
assert result.stdout == "Hello Whirl!\n"
assert result.stderr == ""

Expand Down
16 changes: 11 additions & 5 deletions tests/test_poe_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@ def test_setting_default_task_type(run_poe_subproc, projects, esc_prefix):
project="scripts",
env=no_venv,
)
assert result.capture == f"Poe => echo-args nat, welcome to {projects['scripts']}\n"
assert (
result.capture == f"Poe => echo-args nat, 'welcome to {projects['scripts']}'\n"
)
assert result.stdout == f"hello nat, welcome to {projects['scripts']}\n"
assert result.stderr == ""


def test_setting_default_array_item_task_type(run_poe_subproc):
result = run_poe_subproc("composite_task", project="scripts", env=no_venv)
assert result.capture == "Poe => poe_test_echo Hello\nPoe => poe_test_echo World!\n"
assert (
result.capture == "Poe => poe_test_echo Hello\nPoe => poe_test_echo 'World!'\n"
)
assert result.stdout == "Hello\nWorld!\n"
assert result.stderr == ""


def test_setting_global_env_vars(run_poe_subproc):
result = run_poe_subproc("travel", env=no_venv)
assert result.capture == "Poe => poe_test_echo from EARTH to\nPoe => travel[1]\n"
assert (
result.capture == "Poe => poe_test_echo 'from EARTH to'\nPoe => 'travel[1]'\n"
)
assert result.stdout == "from EARTH to\nMARS\n"
assert result.stderr == ""

Expand All @@ -48,7 +54,7 @@ def test_override_default_verbosity(run_poe_subproc, low_verbosity_project_path)
"test",
cwd=low_verbosity_project_path,
)
assert result.capture == "Poe => poe_test_echo Hello there!\n"
assert result.capture == "Poe => poe_test_echo Hello 'there!'\n"
assert result.stdout == "Hello there!\n"
assert result.stderr == ""

Expand All @@ -59,7 +65,7 @@ def test_partially_decrease_verbosity(run_poe_subproc, high_verbosity_project_pa
"test",
cwd=high_verbosity_project_path,
)
assert result.capture == "Poe => poe_test_echo Hello there!\n"
assert result.capture == "Poe => poe_test_echo Hello 'there!'\n"
assert result.stdout == "Hello there!\n"
assert result.stderr == ""

Expand Down
4 changes: 2 additions & 2 deletions tests/test_poetry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def test_task_with_cli_dependency(run_poetry, projects, is_windows):
cwd=projects["poetry_plugin"],
)
if is_windows:
assert result.stdout.startswith("Poe => cowpy yo yo yo")
assert result.stdout.startswith("Poe => cowpy 'yo yo yo'")
assert "< yo yo yo >" in result.stdout
else:
# On POSIX cowpy expects notices its being called as a subprocess and tries
# unproductively to take input from stdin
assert result.stdout.startswith("Poe => cowpy yo yo yo")
assert result.stdout.startswith("Poe => cowpy 'yo yo yo'")
assert (
"< Cowacter, eyes:default, tongue:False, thoughts:False >" in result.stdout
)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ref_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ def test_ref_passes_named_args_in_definition(run_poe_subproc):

def test_ref_passes_extra_args_in_definition(run_poe_subproc):
result = run_poe_subproc("greet-funny", project="refs")
assert result.capture == "Poe => poe_test_echo hi lol!\n"
assert result.capture == "Poe => poe_test_echo hi 'lol!'\n"
assert result.stdout == "hi lol!\n"
assert result.stderr == ""
10 changes: 5 additions & 5 deletions tests/test_script_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ def test_script_task_with_args_optional(run_poe_subproc, projects, is_windows):
env=no_venv,
)
assert result.capture == (
f"Poe => greet-passed-args --greeting=hello --user=nat --optional=welcome "
f"to {named_args_project_path}\n"
f"Poe => greet-passed-args --greeting=hello --user=nat "
f"'--optional=welcome to {named_args_project_path}'\n"
)

if is_windows:
Expand Down Expand Up @@ -238,7 +238,7 @@ def test_script_with_positional_args(run_poe_subproc):
result = run_poe_subproc(
"greet-positional", "help!", "Santa", project="scripts", env=no_venv
)
assert result.capture == "Poe => greet-positional help! Santa\n"
assert result.capture == "Poe => greet-positional 'help!' Santa\n"
assert result.stdout == "help! Santa\n"
assert result.stderr == ""

Expand Down Expand Up @@ -275,14 +275,14 @@ def test_script_with_positional_args_and_options(run_poe_subproc):
result = run_poe_subproc(
"greet-positional", "help!", "Santa", "--upper", project="scripts", env=no_venv
)
assert result.capture == "Poe => greet-positional help! Santa --upper\n"
assert result.capture == "Poe => greet-positional 'help!' Santa --upper\n"
assert result.stdout == "HELP! SANTA\n"
assert result.stderr == ""

result = run_poe_subproc(
"greet-positional", "--upper", "help!", "Santa", project="scripts", env=no_venv
)
assert result.capture == "Poe => greet-positional --upper help! Santa\n"
assert result.capture == "Poe => greet-positional --upper 'help!' Santa\n"
assert result.stdout == "HELP! SANTA\n"
assert result.stderr == ""

Expand Down
14 changes: 7 additions & 7 deletions tests/test_sequence_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ def test_sequence_task(run_poe_subproc, esc_prefix):
result = run_poe_subproc("composite_task", project="sequences")
assert result.capture == (
"Poe => poe_test_echo Hello\n"
"Poe => poe_test_echo World!\n"
"Poe => poe_test_echo :)!\n"
"Poe => poe_test_echo 'World!'\n"
"Poe => poe_test_echo ':)!'\n"
)
assert result.stdout == "Hello\nWorld!\n:)!\n"
assert result.stderr == ""
Expand All @@ -14,8 +14,8 @@ def test_another_sequence_task(run_poe_subproc, esc_prefix):
result = run_poe_subproc("also_composite_task", project="sequences")
assert result.capture == (
"Poe => poe_test_echo Hello\n"
"Poe => poe_test_echo World!\n"
"Poe => poe_test_echo :)!\n"
"Poe => poe_test_echo 'World!'\n"
"Poe => poe_test_echo ':)!'\n"
)
assert result.stdout == "Hello\nWorld!\n:)!\n"
assert result.stderr == ""
Expand All @@ -25,8 +25,8 @@ def test_a_script_sequence_task_with_args(run_poe_subproc, esc_prefix):
# This should be exactly the same as calling the composite_task task directly
result = run_poe_subproc("greet-multiple", "--mouse=Jerry", project="sequences")
assert result.capture == (
"Poe => my_package:main(environ.get('cat'))\n"
"Poe => my_package:main(environ['mouse'])\n"
"""Poe => 'my_package:main(environ.get('"'"'cat'"'"'))'\n"""
"""Poe => 'my_package:main(environ['"'"'mouse'"'"'])'\n"""
)
assert result.stdout == "hello Tom\nhello Jerry\n"
assert result.stderr == ""
Expand All @@ -37,7 +37,7 @@ def test_sequence_task_with_multiple_value_arg(run_poe_subproc):
"multiple-value-arg", "hey", "1", "2", "3", project="sequences"
)
assert result.capture == (
"Poe => poe_test_echo first: hey\nPoe => poe_test_echo second: 1 2 3\n"
"Poe => poe_test_echo first: hey\nPoe => poe_test_echo second: '1 2 3'\n"
"Poe => poe_test_echo Done.\n"
)
assert result.stdout == "first: hey\nsecond: 1 2 3\nDone.\n"
Expand Down
16 changes: 11 additions & 5 deletions tests/test_task_running.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
def test_ref_task(run_poe_subproc, projects, esc_prefix):
def test_ref_task(run_poe_subproc, projects, esc_prefix, is_windows):
# This should be exactly the same as calling the echo task directly
result = run_poe_subproc(
"also_echo", "foo", "!", env={"POETRY_VIRTUALENVS_CREATE": "false"}
)
assert result.capture == (
"Poe => poe_test_echo "
f"POE_ROOT:{projects['example']} Password1, task_args: foo !\n"
)
if is_windows:
assert result.capture == ( # windows paths need quotes
"Poe => poe_test_echo "
f"'POE_ROOT:{projects['example']}' Password1, task_args: foo '!'\n"
)
else:
assert result.capture == (
"Poe => poe_test_echo "
f"POE_ROOT:{projects['example']} Password1, task_args: foo '!'\n"
)
assert (
result.stdout == f"POE_ROOT:{projects['example']} Password1, task_args: foo !\n"
)
Expand Down

0 comments on commit 21c523e

Please sign in to comment.