From 058e0292d79749b61c337546a8dc41cf299b4b01 Mon Sep 17 00:00:00 2001 From: w3lld1 <42353747+w3lld1@users.noreply.github.com> Date: Thu, 9 Jul 2026 21:58:32 +0000 Subject: [PATCH] fix: strip trailing newlines from line output --- gto/utils.py | 8 ++++++-- tests/test_cli.py | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gto/utils.py b/gto/utils.py index 2ec01293..44542cef 100644 --- a/gto/utils.py +++ b/gto/utils.py @@ -47,6 +47,10 @@ def make_ready_to_serialize( ) +def _format_line_value(value): + return str(value).rstrip("\r\n") + + def format_echo(result, format, format_table=None, if_empty="", missing_value="-"): if format == "yaml": yaml.dump(make_ready_to_serialize(result), sys.stdout) @@ -69,9 +73,9 @@ def format_echo(result, format, format_table=None, if_empty="", missing_value="- elif format == "lines": if result: for line in result: - click.echo(line) + click.echo(_format_line_value(line)) elif format == "line": if result: - click.echo(result) + click.echo(_format_line_value(result)) else: raise NotImplementedError(f"Format {format} is not implemented") diff --git a/tests/test_cli.py b/tests/test_cli.py index 2932c5a9..34db7df5 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -502,6 +502,14 @@ def test_show_line_flag_empty_result(): _check_successful_cmd("show", ["-r", ".", "m5#missing", "--version"], "") +def test_show_line_flags_strip_value_newlines(): + output = ([{"ref": "rf@v1.2.4\n", "version": "v1.2.4\r\n"}], "keys") + with mock.patch("gto.api.show", return_value=output): + _check_successful_cmd("show", ["-r", ".", "rf@latest", "--ref"], "rf@v1.2.4\n") + with mock.patch("gto.api.show", return_value=output): + _check_successful_cmd("show", ["-r", ".", "rf@latest", "--version"], "v1.2.4\n") + + def test_history_json_empty(repo_with_commit: str): _check_successful_cmd("history", ["-r", repo_with_commit, "--json"], "[]\n")