diff options
author | Gauvain Pocentek <gauvainpocentek@gmail.com> | 2019-03-07 20:14:33 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-07 20:14:33 +0100 |
commit | 6bd19027f2cd1cc20d59182d8856f5955e0702e5 (patch) | |
tree | 4d7042ef7f2a65105ebd02af3fbe8d8d775f9c35 /gitlab | |
parent | a6e10f957aeccd7a1fd4e769f7e3acf6e4683308 (diff) | |
parent | 0b70da335690456a556afb9ff7a56dfca693b019 (diff) | |
download | gitlab-6bd19027f2cd1cc20d59182d8856f5955e0702e5.tar.gz |
Merge pull request #738 from jeroendecroos/Gitlab_from_config_inheritance
Make gitlab.Gitlab.from_config a classmethod
Diffstat (limited to 'gitlab')
-rw-r--r-- | gitlab/__init__.py | 18 | ||||
-rw-r--r-- | gitlab/tests/test_gitlab.py | 32 |
2 files changed, 41 insertions, 9 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 18f9d16..819096d 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -165,8 +165,8 @@ class Gitlab(object): """The API version used (4 only).""" return self._api_version - @staticmethod - def from_config(gitlab_id=None, config_files=None): + @classmethod + def from_config(cls, gitlab_id=None, config_files=None): """Create a Gitlab connection from configuration files. Args: @@ -181,13 +181,13 @@ class Gitlab(object): """ config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id, config_files=config_files) - return Gitlab(config.url, private_token=config.private_token, - oauth_token=config.oauth_token, - ssl_verify=config.ssl_verify, timeout=config.timeout, - http_username=config.http_username, - http_password=config.http_password, - api_version=config.api_version, - per_page=config.per_page) + return cls(config.url, private_token=config.private_token, + oauth_token=config.oauth_token, + ssl_verify=config.ssl_verify, timeout=config.timeout, + http_username=config.http_username, + http_password=config.http_password, + api_version=config.api_version, + per_page=config.per_page) def auth(self): """Performs an authentication. diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py index 5174bd2..fddd5ed 100644 --- a/gitlab/tests/test_gitlab.py +++ b/gitlab/tests/test_gitlab.py @@ -18,7 +18,9 @@ from __future__ import print_function +import os import pickle +import tempfile try: import unittest except ImportError: @@ -34,6 +36,17 @@ from gitlab import * # noqa from gitlab.v4.objects import * # noqa +valid_config = b"""[global] +default = one +ssl_verify = true +timeout = 2 + +[one] +url = http://one.url +private_token = ABCDEF +""" + + class TestSanitize(unittest.TestCase): def test_do_nothing(self): self.assertEqual(1, gitlab._sanitize(1)) @@ -536,3 +549,22 @@ class TestGitlab(unittest.TestCase): self.assertEqual(type(user), User) self.assertEqual(user.name, "name") self.assertEqual(user.id, 1) + + def _default_config(self): + fd, temp_path = tempfile.mkstemp() + os.write(fd, valid_config) + os.close(fd) + return temp_path + + def test_from_config(self): + config_path = self._default_config() + gitlab.Gitlab.from_config('one', [config_path]) + os.unlink(config_path) + + def test_subclass_from_config(self): + class MyGitlab(gitlab.Gitlab): + pass + config_path = self._default_config() + gl = MyGitlab.from_config('one', [config_path]) + self.assertEqual(type(gl).__name__, 'MyGitlab') + os.unlink(config_path) |