Skip to content

fix(cli): support binary files with @ notation #2753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions gitlab/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import functools
import os
import pathlib
import re
import sys
import textwrap
Expand Down Expand Up @@ -298,12 +299,17 @@ def _parse_value(v: Any) -> Any:
return v[1:]
if isinstance(v, str) and v.startswith("@"):
# If the user-provided value starts with @, we try to read the file
# path provided after @ as the real value. Exit on any error.
# path provided after @ as the real value.
filepath = pathlib.Path(v[1:]).expanduser().resolve()
try:
with open(v[1:], encoding="utf-8") as f:
with open(filepath, encoding="utf-8") as f:
return f.read()
except Exception as e:
sys.stderr.write(f"{e}\n")
except UnicodeDecodeError:
with open(filepath, "rb") as f:
return f.read()
except OSError as exc:
exc_name = type(exc).__name__
sys.stderr.write(f"{exc_name}: {exc}\n")
sys.exit(1)

return v
Expand Down
7 changes: 6 additions & 1 deletion tests/functional/cli/test_cli_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,15 @@ def test_update_application_settings(gitlab_cli):
assert ret.success


def test_create_project_with_values_from_file(gitlab_cli, tmpdir):
def test_create_project_with_values_from_file(gitlab_cli, fixture_dir, tmpdir):
name = "gitlab-project-from-file"
description = "Multiline\n\nData\n"
from_file = tmpdir.join(name)
from_file.write(description)
from_file_path = f"@{str(from_file)}"
avatar_file = fixture_dir / "avatar.png"
assert avatar_file.exists()
avatar_file_path = f"@{avatar_file}"

cmd = [
"-v",
Expand All @@ -555,6 +558,8 @@ def test_create_project_with_values_from_file(gitlab_cli, tmpdir):
name,
"--description",
from_file_path,
"--avatar",
avatar_file_path,
]
ret = gitlab_cli(cmd)

Expand Down
11 changes: 5 additions & 6 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import argparse
import contextlib
import io
import os
import sys
import tempfile
from contextlib import redirect_stderr # noqa: H302
from unittest import mock

import pytest
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_cls_to_gitlab_resource(class_name, expected_gitlab_resource):
)
def test_die(message, error, expected):
fl = io.StringIO()
with redirect_stderr(fl):
with contextlib.redirect_stderr(fl):
with pytest.raises(SystemExit) as test:
cli.die(message, error)
assert fl.getvalue() == expected
Expand Down Expand Up @@ -90,12 +90,11 @@ def test_parse_value():
os.unlink(temp_path)

fl = io.StringIO()
with redirect_stderr(fl):
with contextlib.redirect_stderr(fl):
with pytest.raises(SystemExit) as exc:
cli._parse_value("@/thisfileprobablydoesntexist")
assert (
fl.getvalue() == "[Errno 2] No such file or directory:"
" '/thisfileprobablydoesntexist'\n"
assert fl.getvalue().startswith(
"FileNotFoundError: [Errno 2] No such file or directory:"
)
assert exc.value.code == 1

Expand Down