From 87d2f9f2e6b3349c985ffab57d392fe597e2f209 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Mon, 14 Nov 2016 14:23:57 +0100 Subject: [PATCH] Can set mozdata.ini path in using MOZDATA_INI environment variable --- VERSION | 2 +- libmozdata/config.py | 4 +++- tests/test_config.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index baa98378..79062996 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.20 +0.1.21 diff --git a/libmozdata/config.py b/libmozdata/config.py index 1a071257..835f3bcd 100644 --- a/libmozdata/config.py +++ b/libmozdata/config.py @@ -26,7 +26,9 @@ def __init__(self, path=None): self.path = path def get_default_paths(self): - return [os.path.join(os.getcwd(), 'mozdata.ini'), os.path.expanduser('~/.mozdata.ini')] + paths = [os.path.join(os.getcwd(), 'mozdata.ini'), os.path.expanduser('~/.mozdata.ini')] + env = os.environ.get('MOZDATA_INI') + return [os.path.expanduser(env)] + paths if env else paths def get(self, section, option, default=None, type=str): if not self.config.has_option(section, option): diff --git a/tests/test_config.py b/tests/test_config.py index fd41c5ad..7b7c3ef0 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -5,6 +5,8 @@ import unittest import sys import os +import shutil +import tempfile try: from configparser import ConfigParser except ImportError: @@ -82,6 +84,40 @@ def test_config_exists_in_cwd(self): self.assertIsNone(config.get('Section', 'Option3')) self.assertEqual(config.get('Section', 'Option3', 'Default'), 'Default') + def test_config_exists_in_env(self): + try: + senv = os.environ.get('MOZDATA_INI') + tmp = tempfile.mkdtemp() + path = os.path.join(tmp, 'mozdata.ini') + with open(path, 'w') as f: + custom_conf = ConfigParser() + custom_conf.add_section('Section env') + custom_conf.set('Section env', 'Option', 'Value') + custom_conf.set('Section env', 'Option2', 'Value2') + custom_conf.add_section('Section2') + custom_conf.set('Section2', 'Option', 'Value3') + custom_conf.write(f) + + os.environ['MOZDATA_INI'] = path + + from libmozdata import config + + self.assertEqual(config.get('Section env', 'Option'), 'Value') + self.assertEqual(config.get('Section env', 'Option', 'Default'), 'Value') + self.assertEqual(config.get('Section env', 'Option2'), 'Value2') + self.assertEqual(config.get('Section env', 'Option2', 'Default'), 'Value2') + self.assertEqual(config.get('Section2', 'Option'), 'Value3') + self.assertEqual(config.get('Section2', 'Option', 'Default'), 'Value3') + self.assertIsNone(config.get('Section env', 'Option3')) + self.assertEqual(config.get('Section env', 'Option3', 'Default'), 'Default') + finally: + if senv is None: + os.environ.pop('MOZDATA_INI') + else: + os.environ['MOZDATA_INI'] = senv + + shutil.rmtree(tmp) + def test_config_get_with_type(self): with open('mozdata.ini', 'w') as f: custom_conf = ConfigParser()