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

Appearance settings

Commit 6ad9da0

Browse filesBrowse files
max-wittiggpocentek
authored andcommitted
fix(cli): exit on config parse error, instead of crashing
* Exit and hint user about possible errors * test: adjust test cases to config missing error
1 parent 742243f commit 6ad9da0
Copy full SHA for 6ad9da0

File tree

Expand file treeCollapse file tree

3 files changed

+42
-6
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+42
-6
lines changed
Open diff view settings
Collapse file

‎gitlab/cli.py‎

Copy file name to clipboardExpand all lines: gitlab/cli.py
+8-3Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from __future__ import print_function
20+
2021
import argparse
2122
import functools
2223
import importlib
@@ -143,9 +144,13 @@ def main():
143144
# load the propermodule (v3 or v4) accordingly. At that point we don't have
144145
# any subparser setup
145146
(options, args) = parser.parse_known_args(sys.argv)
146-
147-
config = gitlab.config.GitlabConfigParser(options.gitlab,
148-
options.config_file)
147+
try:
148+
config = gitlab.config.GitlabConfigParser(
149+
options.gitlab,
150+
options.config_file
151+
)
152+
except gitlab.config.ConfigError as e:
153+
sys.exit(e)
149154
cli_module = importlib.import_module('gitlab.v%s.cli' % config.api_version)
150155

151156
# Now we build the entire set of subcommands and do the complete parsing
Collapse file

‎gitlab/config.py‎

Copy file name to clipboardExpand all lines: gitlab/config.py
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,27 @@ class GitlabDataError(ConfigError):
3737
pass
3838

3939

40+
class GitlabConfigMissingError(ConfigError):
41+
pass
42+
43+
4044
class GitlabConfigParser(object):
4145
def __init__(self, gitlab_id=None, config_files=None):
4246
self.gitlab_id = gitlab_id
4347
_files = config_files or _DEFAULT_FILES
48+
file_exist = False
49+
for file in _files:
50+
if os.path.exists(file):
51+
file_exist = True
52+
if not file_exist:
53+
raise GitlabConfigMissingError(
54+
"Config file not found. \nPlease create one in "
55+
"one of the following locations: {} \nor "
56+
"specify a config file using the '-c' parameter.".format(
57+
", ".join(_DEFAULT_FILES)
58+
)
59+
)
60+
4461
self._config = configparser.ConfigParser()
4562
self._config.read(_files)
4663

Collapse file

‎gitlab/tests/test_config.py‎

Copy file name to clipboardExpand all lines: gitlab/tests/test_config.py
+17-3Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,20 @@
7676

7777

7878
class TestConfigParser(unittest.TestCase):
79+
@mock.patch('os.path.exists')
80+
def test_missing_config(self, path_exists):
81+
path_exists.return_value = False
82+
with self.assertRaises(config.GitlabConfigMissingError):
83+
config.GitlabConfigParser('test')
84+
85+
@mock.patch('os.path.exists')
7986
@mock.patch('six.moves.builtins.open')
80-
def test_invalid_id(self, m_open):
87+
def test_invalid_id(self, m_open, path_exists):
8188
fd = six.StringIO(no_default_config)
8289
fd.close = mock.Mock(return_value=None)
8390
m_open.return_value = fd
91+
path_exists.return_value = True
92+
config.GitlabConfigParser('there')
8493
self.assertRaises(config.GitlabIDError, config.GitlabConfigParser)
8594

8695
fd = six.StringIO(valid_config)
@@ -90,12 +99,15 @@ def test_invalid_id(self, m_open):
9099
config.GitlabConfigParser,
91100
gitlab_id='not_there')
92101

102+
@mock.patch('os.path.exists')
93103
@mock.patch('six.moves.builtins.open')
94-
def test_invalid_data(self, m_open):
104+
def test_invalid_data(self, m_open, path_exists):
95105
fd = six.StringIO(missing_attr_config)
96106
fd.close = mock.Mock(return_value=None,
97107
side_effect=lambda: fd.seek(0))
98108
m_open.return_value = fd
109+
path_exists.return_value = True
110+
99111
config.GitlabConfigParser('one')
100112
config.GitlabConfigParser('one')
101113
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
@@ -107,11 +119,13 @@ def test_invalid_data(self, m_open):
107119
self.assertEqual('Unsupported per_page number: 200',
108120
emgr.exception.args[0])
109121

122+
@mock.patch('os.path.exists')
110123
@mock.patch('six.moves.builtins.open')
111-
def test_valid_data(self, m_open):
124+
def test_valid_data(self, m_open, path_exists):
112125
fd = six.StringIO(valid_config)
113126
fd.close = mock.Mock(return_value=None)
114127
m_open.return_value = fd
128+
path_exists.return_value = True
115129

116130
cp = config.GitlabConfigParser()
117131
self.assertEqual("one", cp.gitlab_id)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.