Skip to content

feat: Allow an environment variable to specify config location #1074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion gitlab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions gitlab/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,6 +73,16 @@
"""


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):
Expand Down