Skip to content

Commit

Permalink
feat: add 'max_log_args' option that allows to limit the number of ar…
Browse files Browse the repository at this point in the history
…guments that are shown in the logging output
  • Loading branch information
FredStober committed May 4, 2024
1 parent 4fec3b4 commit a49f076
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
10 changes: 10 additions & 0 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ def _tag_completer(
help="Adds a timestamp to logged output.",
noxfile=True,
),
_option_set.Option(
"max_log_args",
"-ma",
"--max-log-args",
group=options.groups["reporting"],
default=None,
type=int,
help="Set a limit on the maximum number of arguments that are logged.",
noxfile=True,
),
_option_set.Option(
"default_venv_backend",
"-db",
Expand Down
20 changes: 17 additions & 3 deletions nox/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,21 @@ def _clean_env(env: Mapping[str, str | None] | None = None) -> dict[str, str] |
return clean_env


def _shlex_join(args: Sequence[str | os.PathLike[str]]) -> str:
return " ".join(shlex.quote(os.fspath(arg)) for arg in args)
def _get_cmd_for_log(
max_log_args: int | None,
cmd: str | os.PathLike[str],
args: Sequence[str | os.PathLike[str]],
) -> str:
num_more_args = len(args) - (max_log_args or 0)
args = args[:max_log_args]
output_list = [str(cmd)]
if args:
output_list.extend(shlex.quote(os.fspath(arg)) for arg in args)
if (max_log_args is not None) and num_more_args > 0:
more_note = "more " if max_log_args > 0 else ""
args_note = "arguments" if num_more_args > 1 else "argument"
output_list.append(f"... ({num_more_args} {more_note}{args_note})")
return " ".join(output_list)


def run(
Expand All @@ -85,6 +98,7 @@ def run(
paths: Sequence[str] | None = None,
success_codes: Iterable[int] | None = None,
log: bool = True,
max_log_args: int | None = None,
external: ExternalType = False,
stdout: int | IO[str] | None = None,
stderr: int | IO[str] = subprocess.STDOUT,
Expand All @@ -97,7 +111,7 @@ def run(
success_codes = [0]

cmd, args = args[0], args[1:]
full_cmd = f"{cmd} {_shlex_join(args)}"
full_cmd = _get_cmd_for_log(max_log_args, cmd, args)

cmd_path = which(os.fspath(cmd), paths)
str_args = [os.fspath(arg) for arg in args]
Expand Down
1 change: 1 addition & 0 deletions nox/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ def _run(
silent=silent,
success_codes=success_codes,
log=log,
max_log_args=self._runner.global_config.max_log_args,
external=external,
stdout=stdout,
stderr=stderr,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ def test_run_verbosity_failed_command(capsys, caplog):
assert not logs


def test__get_cmd_for_log():
get_cmd_for_log = nox.command._get_cmd_for_log
assert get_cmd_for_log(None, "test", []) == "test"
assert get_cmd_for_log(1, "test", []) == "test"
arg_list = ["a", "b", "c"]
assert get_cmd_for_log(None, "test", arg_list) == "test a b c"
assert get_cmd_for_log(4, "test", arg_list) == "test a b c"
assert get_cmd_for_log(3, "test", arg_list) == "test a b c"
assert get_cmd_for_log(2, "test", arg_list) == "test a b ... (1 more argument)"
assert get_cmd_for_log(1, "test", arg_list) == "test a ... (2 more arguments)"
assert get_cmd_for_log(0, "test", arg_list) == "test ... (3 arguments)"


@pytest.mark.skipif(
platform.system() == "Windows",
reason="See https://github.com/python/cpython/issues/85815",
Expand Down
1 change: 1 addition & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def run_with_defaults(**kwargs):
"paths": None,
"success_codes": None,
"log": True,
"max_log_args": None,
"external": False,
"stdout": None,
"stderr": subprocess.STDOUT,
Expand Down

0 comments on commit a49f076

Please sign in to comment.