Skip to content

Commit

Permalink
refactor(utils): allow quoting any type of argument
Browse files Browse the repository at this point in the history
  • Loading branch information
KSmanis committed Feb 6, 2022
1 parent 3732297 commit 1695bd3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pytest-cov = "^3.0.0"
[tool.poetry.scripts]
pip-autocompile = "pipautocompile.main:cli"

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"if TYPE_CHECKING:",
]

[tool.isort]
profile = "black"
add_imports = ["from __future__ import annotations"]
Expand Down
8 changes: 6 additions & 2 deletions src/pipautocompile/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import annotations

from shlex import quote
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any

def quote_args(*args: str) -> str:
return " ".join(map(quote, args))

def quote_args(*args: Any) -> str:
return " ".join(quote(str(arg)) for arg in args)
8 changes: 7 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
argnames=("args", "expected"),
argvalues=(
(tuple(), ""),
((None,), "None"),
((1, None, 1.5, True, "", " foo", "bar "), "1 None 1.5 True '' ' foo' 'bar '"),
(("",), "''"),
(("", ""), "'' ''"),
(("foo",), "foo"),
Expand All @@ -22,8 +24,12 @@
((" foo ", "bar "), "' foo ' 'bar '"),
((" foo ", " bar "), "' foo ' ' bar '"),
((" foo ", " bar ", " baz "), "' foo ' ' bar ' ' baz '"),
(
("pip-autocompile", "--pip-compile-args", "--allow-unsafe --upgrade"),
"pip-autocompile --pip-compile-args '--allow-unsafe --upgrade'",
),
),
ids=lambda argvalue: repr(argvalue),
)
def test_quote_args(args: tuple[str], expected: str) -> None:
def test_quote_args(args: tuple, expected: str) -> None:
assert quote_args(*args) == expected

0 comments on commit 1695bd3

Please sign in to comment.