Skip to content

Commit

Permalink
Refactor config default search path retrieval
Browse files Browse the repository at this point in the history
Co-authored-by: David Hoese <david.hoese@ssec.wisc.edu>
Co-authored-by: James Bourbeau <jrbourbeau@users.noreply.github.com>
  • Loading branch information
jrbourbeau and djhoese committed Feb 4, 2022
1 parent adbb8d1 commit 322c802
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
5 changes: 0 additions & 5 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ subprojects to manage configuration files separately, but have them merge
into the same global configuration (ex. ``dask``, ``dask-kubernetes``,
``dask-ml``).

.. note::

For historical reasons we also look in the ``~/.mypkg`` directory for
config files. This is deprecated and will soon be removed.*

Environment Variables
~~~~~~~~~~~~~~~~~~~~~

Expand Down
1 change: 0 additions & 1 deletion donfig/config_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ def __init__(self, name, defaults=None, paths=None, env=None, env_var=None, root
os.path.join(sys.prefix, 'etc', name),
*[os.path.join(prefix, "etc", name) for prefix in site.PREFIXES],
os.path.join(os.path.expanduser('~'), '.config', name),
os.path.join(os.path.expanduser('~'), '.{}'.format(name))
]

if env_prefix is None:
Expand Down
42 changes: 41 additions & 1 deletion donfig/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import yaml
import os
import site
import stat
import subprocess
import sys

import pytest
import yaml

from donfig.config_obj import (Config, update, merge, collect_yaml,
collect_env, expand_environment_variables,
Expand Down Expand Up @@ -494,3 +495,42 @@ def test_path_includes_site_prefix():
)

subprocess.check_call([sys.executable, "-c", command])


def test__get_paths(monkeypatch):
# These environment variables, if present, would interfere with these tests
# We temporarily remove them to avoid interference from the
# machine where tests are being run.
monkeypatch.delenv("MYPKG_CONFIG", raising=False)
monkeypatch.delenv("MYPKG_ROOT_CONFIG", raising=False)

expected = [
"/etc/mypkg",
os.path.join(sys.prefix, "etc", "mypkg"),
os.path.join(os.path.expanduser("~"), ".config", "mypkg"),
]
config = Config("mypkg")
assert config.paths == expected
assert len(config.paths) == len(set(config.paths)) # No duplicate paths

with monkeypatch.context() as m:
m.setenv("MYPKG_CONFIG", "foo-bar")
config = Config("mypkg")
paths = config.paths
assert paths == expected + ["foo-bar"]
assert len(paths) == len(set(paths))

with monkeypatch.context() as m:
m.setenv("MYPKG_ROOT_CONFIG", "foo-bar")
config = Config("mypkg")
paths = config.paths
assert paths == ["foo-bar"] + expected[1:]
assert len(paths) == len(set(paths))

with monkeypatch.context() as m:
prefix = os.path.join("include", "this", "path")
m.setattr(site, "PREFIXES", site.PREFIXES + [prefix])
config = Config("mypkg")
paths = config.paths
assert os.path.join(prefix, "etc", "mypkg") in paths
assert len(paths) == len(set(paths))

0 comments on commit 322c802

Please sign in to comment.