From 07fb499d65dcbf30e01efd287d9bb3c162c60aa4 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Fri, 7 Oct 2022 17:47:40 +0100 Subject: [PATCH] fix: nox.session.run-ing commands with pathlib.Path arguments (#649) * fix: nox.session.run-ing commands with pathlib.Path arguments Previously this should have worked in theory, but the string which is logged for debugging calls shlex.quote, which was blowing up trying to quote the pathlib.Path. I'm slightly surprised shlex.quote doesn't support pathlib.Path objects (yet?) but for now here this just falls back to a cruder representation when quoting fails. * Use os.fspath instead. --- nox/command.py | 3 +-- tests/test_command.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/nox/command.py b/nox/command.py index 8a55c1fa..9b828b48 100644 --- a/nox/command.py +++ b/nox/command.py @@ -71,8 +71,7 @@ def _clean_env(env: dict[str, str] | None) -> dict[str, str] | None: def _shlex_join(args: Sequence[str]) -> str: - # shlex.join() was added in Python 3.8 - return " ".join(shlex.quote(arg) for arg in args) + return " ".join(shlex.quote(os.fspath(arg)) for arg in args) def run( diff --git a/tests/test_command.py b/tests/test_command.py index a05ab820..efda1608 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -22,6 +22,7 @@ import subprocess import sys import time +from pathlib import Path from textwrap import dedent from unittest import mock @@ -112,6 +113,19 @@ def test_run_verbosity_failed_command(capsys, caplog): assert not logs +@pytest.mark.skipif( + platform.system() == "Windows", + reason="See https://github.com/python/cpython/issues/85815", +) +def test_run_non_str(): + result = nox.command.run( + [Path(PYTHON), "-c", "import sys; print(sys.argv)", Path(PYTHON)], + silent=True, + ) + + assert PYTHON in result + + def test_run_env_unicode(): result = nox.command.run( [PYTHON, "-c", 'import os; print(os.environ["SIGIL"])'],