Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit57749d4

Browse files
JohnVillalovosnejch
authored andcommitted
fix(cli): support binary files with@ notation
Support binary files being used in the CLI with arguments using the`@` notation. For example `--avatar @/path/to/avatar.png`Also explicitly catch the common OSError exception, which is theparent exception for things like: FileNotFoundError, PermissionErrorand more exceptions.Remove the bare exception handling. We would rather have the fulltraceback of any exceptions that we don't know about and add themlater if needed.Closes:#2752
1 parentdae9e52 commit57749d4

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

‎gitlab/cli.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
importargparse
22
importfunctools
33
importos
4+
importpathlib
45
importre
56
importsys
67
importtextwrap
@@ -298,12 +299,17 @@ def _parse_value(v: Any) -> Any:
298299
returnv[1:]
299300
ifisinstance(v,str)andv.startswith("@"):
300301
# If the user-provided value starts with @, we try to read the file
301-
# path provided after @ as the real value. Exit on any error.
302+
# path provided after @ as the real value.
303+
filepath=pathlib.Path(v[1:]).expanduser().resolve()
302304
try:
303-
withopen(v[1:],encoding="utf-8")asf:
305+
withopen(filepath,encoding="utf-8")asf:
304306
returnf.read()
305-
exceptExceptionase:
306-
sys.stderr.write(f"{e}\n")
307+
exceptUnicodeDecodeError:
308+
withopen(filepath,"rb")asf:
309+
returnf.read()
310+
exceptOSErrorasexc:
311+
exc_name=type(exc).__name__
312+
sys.stderr.write(f"{exc_name}:{exc}\n")
307313
sys.exit(1)
308314

309315
returnv

‎tests/functional/cli/test_cli_v4.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,15 @@ def test_update_application_settings(gitlab_cli):
540540
assertret.success
541541

542542

543-
deftest_create_project_with_values_from_file(gitlab_cli,tmpdir):
543+
deftest_create_project_with_values_from_file(gitlab_cli,fixture_dir,tmpdir):
544544
name="gitlab-project-from-file"
545545
description="Multiline\n\nData\n"
546546
from_file=tmpdir.join(name)
547547
from_file.write(description)
548548
from_file_path=f"@{str(from_file)}"
549+
avatar_file=fixture_dir/"avatar.png"
550+
assertavatar_file.exists()
551+
avatar_file_path=f"@{avatar_file}"
549552

550553
cmd= [
551554
"-v",
@@ -555,6 +558,8 @@ def test_create_project_with_values_from_file(gitlab_cli, tmpdir):
555558
name,
556559
"--description",
557560
from_file_path,
561+
"--avatar",
562+
avatar_file_path,
558563
]
559564
ret=gitlab_cli(cmd)
560565

‎tests/unit/test_cli.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
importargparse
2+
importcontextlib
23
importio
34
importos
45
importsys
56
importtempfile
6-
fromcontextlibimportredirect_stderr# noqa: H302
77
fromunittestimportmock
88

99
importpytest
@@ -62,7 +62,7 @@ def test_cls_to_gitlab_resource(class_name, expected_gitlab_resource):
6262
)
6363
deftest_die(message,error,expected):
6464
fl=io.StringIO()
65-
withredirect_stderr(fl):
65+
withcontextlib.redirect_stderr(fl):
6666
withpytest.raises(SystemExit)astest:
6767
cli.die(message,error)
6868
assertfl.getvalue()==expected
@@ -90,12 +90,11 @@ def test_parse_value():
9090
os.unlink(temp_path)
9191

9292
fl=io.StringIO()
93-
withredirect_stderr(fl):
93+
withcontextlib.redirect_stderr(fl):
9494
withpytest.raises(SystemExit)asexc:
9595
cli._parse_value("@/thisfileprobablydoesntexist")
96-
assert (
97-
fl.getvalue()=="[Errno 2] No such file or directory:"
98-
" '/thisfileprobablydoesntexist'\n"
96+
assertfl.getvalue().startswith(
97+
"FileNotFoundError: [Errno 2] No such file or directory:"
9998
)
10099
assertexc.value.code==1
101100

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp