Skip to content

Commit 7a93a7e

Browse files
committed
feat: allow an environment variable to specify config location
It can be useful (especially in scripts) to specify a configuration location via an environment variable. If the "PYTHON_GITLAB_CFG" environment variable is defined, treat its value as the path to a configuration file and include it in the set of default configuration locations.
1 parent c7c431a commit 7a93a7e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

gitlab/config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818
import os
1919
import configparser
2020

21-
_DEFAULT_FILES = ["/etc/python-gitlab.cfg", os.path.expanduser("~/.python-gitlab.cfg")]
21+
22+
def _env_config():
23+
if "PYTHON_GITLAB_CFG" in os.environ:
24+
return [os.environ["PYTHON_GITLAB_CFG"]]
25+
return []
26+
27+
28+
_DEFAULT_FILES = [
29+
"/etc/python-gitlab.cfg",
30+
os.path.expanduser("~/.python-gitlab.cfg"),
31+
] + _env_config()
2232

2333

2434
class ConfigError(Exception):

gitlab/tests/test_config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
import os
1819
import unittest
1920

2021
import mock
@@ -72,6 +73,16 @@
7273
"""
7374

7475

76+
class TestEnvConfig(unittest.TestCase):
77+
def test_env_present(self):
78+
with mock.patch.dict(os.environ, {"PYTHON_GITLAB_CFG": "/some/path"}):
79+
self.assertEqual(["/some/path"], config._env_config())
80+
81+
def test_env_missing(self):
82+
with mock.patch.dict(os.environ, {}, clear=True):
83+
self.assertEqual([], config._env_config())
84+
85+
7586
class TestConfigParser(unittest.TestCase):
7687
@mock.patch("os.path.exists")
7788
def test_missing_config(self, path_exists):

0 commit comments

Comments
 (0)