| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 742243f commit 6ad9da0
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,7 @@ | |||
| 17 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 18 | 18 | ||
| 19 | 19 | from __future__ import print_function | |
| 20 | + | ||
| 20 | 21 | import argparse | |
| 21 | 22 | import functools | |
| 22 | 23 | import importlib | |
@@ -143,9 +144,13 @@ def main(): | |||
| 143 | 144 | # load the propermodule (v3 or v4) accordingly. At that point we don't have | |
| 144 | 145 | # any subparser setup | |
| 145 | 146 | (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) | ||
| 149 | 154 | cli_module = importlib.import_module('gitlab.v%s.cli' % config.api_version) | |
| 150 | 155 | ||
| 151 | 156 | # Now we build the entire set of subcommands and do the complete parsing | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,10 +37,27 @@ class GitlabDataError(ConfigError): | |||
| 37 | 37 | pass | |
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | + class GitlabConfigMissingError(ConfigError): | ||
| 41 | + pass | ||
| 42 | + | ||
| 43 | + | ||
| 40 | 44 | class GitlabConfigParser(object): | |
| 41 | 45 | def __init__(self, gitlab_id=None, config_files=None): | |
| 42 | 46 | self.gitlab_id = gitlab_id | |
| 43 | 47 | _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 | + | ||
| 44 | 61 | self._config = configparser.ConfigParser() | |
| 45 | 62 | self._config.read(_files) | |
| 46 | 63 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -76,11 +76,20 @@ | |||
| 76 | 76 | ||
| 77 | 77 | ||
| 78 | 78 | 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') | ||
| 79 | 86 | @mock.patch('six.moves.builtins.open') | |
| 80 | - def test_invalid_id(self, m_open): | ||
| 87 | + def test_invalid_id(self, m_open, path_exists): | ||
| 81 | 88 | fd = six.StringIO(no_default_config) | |
| 82 | 89 | fd.close = mock.Mock(return_value=None) | |
| 83 | 90 | m_open.return_value = fd | |
| 91 | + path_exists.return_value = True | ||
| 92 | + config.GitlabConfigParser('there') | ||
| 84 | 93 | self.assertRaises(config.GitlabIDError, config.GitlabConfigParser) | |
| 85 | 94 | ||
| 86 | 95 | fd = six.StringIO(valid_config) | |
@@ -90,12 +99,15 @@ def test_invalid_id(self, m_open): | |||
| 90 | 99 | config.GitlabConfigParser, | |
| 91 | 100 | gitlab_id='not_there') | |
| 92 | 101 | ||
| 102 | + @mock.patch('os.path.exists') | ||
| 93 | 103 | @mock.patch('six.moves.builtins.open') | |
| 94 | - def test_invalid_data(self, m_open): | ||
| 104 | + def test_invalid_data(self, m_open, path_exists): | ||
| 95 | 105 | fd = six.StringIO(missing_attr_config) | |
| 96 | 106 | fd.close = mock.Mock(return_value=None, | |
| 97 | 107 | side_effect=lambda: fd.seek(0)) | |
| 98 | 108 | m_open.return_value = fd | |
| 109 | + path_exists.return_value = True | ||
| 110 | + | ||
| 99 | 111 | config.GitlabConfigParser('one') | |
| 100 | 112 | config.GitlabConfigParser('one') | |
| 101 | 113 | self.assertRaises(config.GitlabDataError, config.GitlabConfigParser, | |
@@ -107,11 +119,13 @@ def test_invalid_data(self, m_open): | |||
| 107 | 119 | self.assertEqual('Unsupported per_page number: 200', | |
| 108 | 120 | emgr.exception.args[0]) | |
| 109 | 121 | ||
| 122 | + @mock.patch('os.path.exists') | ||
| 110 | 123 | @mock.patch('six.moves.builtins.open') | |
| 111 | - def test_valid_data(self, m_open): | ||
| 124 | + def test_valid_data(self, m_open, path_exists): | ||
| 112 | 125 | fd = six.StringIO(valid_config) | |
| 113 | 126 | fd.close = mock.Mock(return_value=None) | |
| 114 | 127 | m_open.return_value = fd | |
| 128 | + path_exists.return_value = True | ||
| 115 | 129 | ||
| 116 | 130 | cp = config.GitlabConfigParser() | |
| 117 | 131 | self.assertEqual("one", cp.gitlab_id) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments