summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNejc Habjan <hab.nejc@gmail.com>2020-04-17 01:38:40 +0200
committerGitHub <noreply@github.com>2020-04-17 01:38:40 +0200
commit0c3b717f9376668696ad13b6b481f28ab3c03abf (patch)
tree14d1be0049be163a1253c864b090dad161c3b86a
parent70cefe4d5b7f29db6c8c1deef524076510fd350a (diff)
parent401e702a9ff14bf4cc33b3ed3acf16f3c60c6945 (diff)
downloadgitlab-0c3b717f9376668696ad13b6b481f28ab3c03abf.tar.gz
Merge pull request #1074 from jeremycline/environment-variable
feat: Allow an environment variable to specify config location
-rw-r--r--docs/cli.rst5
-rw-r--r--gitlab/config.py12
-rw-r--r--gitlab/tests/test_config.py11
3 files changed, 26 insertions, 2 deletions
diff --git a/docs/cli.rst b/docs/cli.rst
index b5c8e52..aeff276 100644
--- a/docs/cli.rst
+++ b/docs/cli.rst
@@ -14,7 +14,10 @@ Configuration
Files
-----
-``gitlab`` looks up 2 configuration files by default:
+``gitlab`` looks up 3 configuration files by default:
+
+``PYTHON_GITLAB_CFG`` environment variable
+ An environment variable that contains the path to a configuration file
``/etc/python-gitlab.cfg``
System-wide configuration file
diff --git a/gitlab/config.py b/gitlab/config.py
index 1b665ed..fa2593b 100644
--- a/gitlab/config.py
+++ b/gitlab/config.py
@@ -18,7 +18,17 @@
import os
import configparser
-_DEFAULT_FILES = ["/etc/python-gitlab.cfg", os.path.expanduser("~/.python-gitlab.cfg")]
+
+def _env_config():
+ if "PYTHON_GITLAB_CFG" in os.environ:
+ return [os.environ["PYTHON_GITLAB_CFG"]]
+ return []
+
+
+_DEFAULT_FILES = _env_config() + [
+ "/etc/python-gitlab.cfg",
+ os.path.expanduser("~/.python-gitlab.cfg"),
+]
class ConfigError(Exception):
diff --git a/gitlab/tests/test_config.py b/gitlab/tests/test_config.py
index 65bd300..681b3d1 100644
--- a/gitlab/tests/test_config.py
+++ b/gitlab/tests/test_config.py
@@ -15,6 +15,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import os
import unittest
import mock
@@ -72,6 +73,16 @@ per_page = 200
"""
+class TestEnvConfig(unittest.TestCase):
+ def test_env_present(self):
+ with mock.patch.dict(os.environ, {"PYTHON_GITLAB_CFG": "/some/path"}):
+ self.assertEqual(["/some/path"], config._env_config())
+
+ def test_env_missing(self):
+ with mock.patch.dict(os.environ, {}, clear=True):
+ self.assertEqual([], config._env_config())
+
+
class TestConfigParser(unittest.TestCase):
@mock.patch("os.path.exists")
def test_missing_config(self, path_exists):