diff --git a/changelog.d/feature.98fc1e59.entry.yaml b/changelog.d/feature.98fc1e59.entry.yaml new file mode 100644 index 0000000..01f6ce9 --- /dev/null +++ b/changelog.d/feature.98fc1e59.entry.yaml @@ -0,0 +1,5 @@ +message: Improve matching commands that contain quoted arguments. +pr_ids: +- 103 +timestamp: 1666456344 +type: feature diff --git a/pytest_subprocess/utils.py b/pytest_subprocess/utils.py index eece249..00edd49 100644 --- a/pytest_subprocess/utils.py +++ b/pytest_subprocess/utils.py @@ -1,4 +1,5 @@ import os +import shlex import sys import threading from pathlib import Path @@ -38,8 +39,10 @@ def __init__( command: "COMMAND", ): if isinstance(command, str): - command = tuple(command.split(" ")) - if not isinstance(command, (list, tuple)): + command = tuple(shlex.split(command)) + if isinstance(command, list): + command = tuple(command) + elif not isinstance(command, tuple): raise TypeError("Command can be only of type string, list or tuple.") self.command: Tuple[ARGUMENT, ...] = tuple( @@ -54,7 +57,9 @@ def __init__( def __eq__(self, other: AnyType) -> bool: if isinstance(other, str): - other = other.split(" ") + other = shlex.split(other) + elif isinstance(other, tuple): + other = list(other) norm_command = [ os.fspath(c) if isinstance(c, os.PathLike) else c for c in self.command diff --git a/tests/test_utils.py b/tests/test_utils.py index c21fc15..f1515bb 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -31,6 +31,11 @@ def test_more_complex_command(command): assert check_not_match(command, ["other", "command"]) +def test_command_with_quoted_string(): + command = Command(["something", "with", "an argument that contains spaces"]) + assert command == 'something with "an argument that contains spaces"' + + def test_simple_wildcards(): command = Command([Any()]) assert check_match(command, ["test"])