File tree Expand file tree Collapse file tree 3 files changed +21
-11
lines changed Expand file tree Collapse file tree 3 files changed +21
-11
lines changed Original file line number Diff line number Diff line change 1
1
import argparse
2
2
import functools
3
3
import os
4
+ import pathlib
4
5
import re
5
6
import sys
6
7
import textwrap
@@ -298,12 +299,17 @@ def _parse_value(v: Any) -> Any:
298
299
return v [1 :]
299
300
if isinstance (v , str ) and v .startswith ("@" ):
300
301
# 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 ()
302
304
try :
303
- with open (v [ 1 :] , encoding = "utf-8" ) as f :
305
+ with open (filepath , encoding = "utf-8" ) as f :
304
306
return f .read ()
305
- except Exception as e :
306
- sys .stderr .write (f"{ e } \n " )
307
+ except UnicodeDecodeError :
308
+ with open (filepath , "rb" ) as f :
309
+ return f .read ()
310
+ except OSError as exc :
311
+ exc_name = type (exc ).__name__
312
+ sys .stderr .write (f"{ exc_name } : { exc } \n " )
307
313
sys .exit (1 )
308
314
309
315
return v
Original file line number Diff line number Diff line change @@ -540,12 +540,15 @@ def test_update_application_settings(gitlab_cli):
540
540
assert ret .success
541
541
542
542
543
- def test_create_project_with_values_from_file (gitlab_cli , tmpdir ):
543
+ def test_create_project_with_values_from_file (gitlab_cli , fixture_dir , tmpdir ):
544
544
name = "gitlab-project-from-file"
545
545
description = "Multiline\n \n Data\n "
546
546
from_file = tmpdir .join (name )
547
547
from_file .write (description )
548
548
from_file_path = f"@{ str (from_file )} "
549
+ avatar_file = fixture_dir / "avatar.png"
550
+ assert avatar_file .exists ()
551
+ avatar_file_path = f"@{ avatar_file } "
549
552
550
553
cmd = [
551
554
"-v" ,
@@ -555,6 +558,8 @@ def test_create_project_with_values_from_file(gitlab_cli, tmpdir):
555
558
name ,
556
559
"--description" ,
557
560
from_file_path ,
561
+ "--avatar" ,
562
+ avatar_file_path ,
558
563
]
559
564
ret = gitlab_cli (cmd )
560
565
Original file line number Diff line number Diff line change 1
1
import argparse
2
+ import contextlib
2
3
import io
3
4
import os
4
5
import sys
5
6
import tempfile
6
- from contextlib import redirect_stderr # noqa: H302
7
7
from unittest import mock
8
8
9
9
import pytest
@@ -62,7 +62,7 @@ def test_cls_to_gitlab_resource(class_name, expected_gitlab_resource):
62
62
)
63
63
def test_die (message , error , expected ):
64
64
fl = io .StringIO ()
65
- with redirect_stderr (fl ):
65
+ with contextlib . redirect_stderr (fl ):
66
66
with pytest .raises (SystemExit ) as test :
67
67
cli .die (message , error )
68
68
assert fl .getvalue () == expected
@@ -90,12 +90,11 @@ def test_parse_value():
90
90
os .unlink (temp_path )
91
91
92
92
fl = io .StringIO ()
93
- with redirect_stderr (fl ):
93
+ with contextlib . redirect_stderr (fl ):
94
94
with pytest .raises (SystemExit ) as exc :
95
95
cli ._parse_value ("@/thisfileprobablydoesntexist" )
96
- assert (
97
- fl .getvalue () == "[Errno 2] No such file or directory:"
98
- " '/thisfileprobablydoesntexist'\n "
96
+ assert fl .getvalue ().startswith (
97
+ "FileNotFoundError: [Errno 2] No such file or directory:"
99
98
)
100
99
assert exc .value .code == 1
101
100
You can’t perform that action at this time.
0 commit comments