Skip to content

Commit

Permalink
Fix exception when deprecated settings appear in a user config (#1595)
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Christophe Morin <[email protected]>
  • Loading branch information
JeanChristopheMorinPerso authored Nov 29, 2023
1 parent 77ce06c commit 94b6a05
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/rez/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,10 @@ def _load_config_from_filepaths(filepaths):
for key in data_:
if key in _deprecated_settings:
rez.deprecations.warn(
_deprecated_settings[key].get_message(),
_deprecated_settings[key].get_message(
key,
env_var=False,
),
rez.deprecations.RezDeprecationWarning,
pre_formatted=True,
filename=filepath_with_ext,
Expand Down
95 changes: 93 additions & 2 deletions src/rez/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
test configuration settings
"""
import unittest
from rez.tests.util import TestBase
from rez.tests.util import TestBase, TempdirMixin, restore_os_environ
from rez.exceptions import ConfigurationError
from rez.config import Config, get_module_root_config, _replace_config
from rez.config import Config, get_module_root_config, _replace_config, _Deprecation
from rez.system import system
from rez.utils.data_utils import RO_AttrDictWrapper
from rez.packages import get_developer_package
from rez.vendor.six import six
from rez.deprecations import RezDeprecationWarning
import os
import sys
import os.path
import subprocess
import functools
import shutil
if sys.version_info[:3] >= (3, 3):
import unittest.mock


class TestConfig(TestBase):
Expand Down Expand Up @@ -293,5 +299,90 @@ def test_8(self):
raise


@unittest.skipIf(sys.version_info[0] < 3, "Skip on python 2")
class TestDeprecations(TestBase, TempdirMixin):
@classmethod
def setUpClass(cls):
cls.settings = {}
TempdirMixin.setUpClass()

@classmethod
def tearDownClass(cls):
TempdirMixin.tearDownClass()

def test_deprecation_from_user_config(self):
user_home = os.path.join(self.root, "user_home")
self.addCleanup(functools.partial(shutil.rmtree, user_home))

os.makedirs(user_home)

with open(os.path.join(user_home, ".rezconfig.py"), "w") as fd:
fd.write("packages_path = ['/tmp/asd']")

fake_deprecated_settings = {
"packages_path": _Deprecation("0.0.0"),
}

with unittest.mock.patch(
"rez.config._deprecated_settings",
fake_deprecated_settings
):
with restore_os_environ():
os.environ['HOME'] = user_home
config = Config._create_main_config()
with self.assertWarns(RezDeprecationWarning) as warn:
_ = config.data
# Assert just to ensure the test was set up properly.
self.assertEqual(config.data["packages_path"], ["/tmp/asd"])

self.assertEqual(
str(warn.warning),
"config setting named 'packages_path' is deprecated and will be removed in 0.0.0.",
)

def test_deprecation_from_env_var(self):
fake_deprecated_settings = {
"packages_path": _Deprecation("0.0.0"),
}

with unittest.mock.patch(
"rez.config._deprecated_settings",
fake_deprecated_settings
):
with restore_os_environ():
# Test with non-json env var
os.environ["REZ_PACKAGES_PATH"] = "/tmp/asd2"
os.environ["REZ_DISABLE_HOME_CONFIG"] = "1"
config = Config._create_main_config()
with self.assertWarns(RezDeprecationWarning) as warn:
_ = config.data
# Assert just to ensure the test was set up properly.
self.assertEqual(config.data["packages_path"], ["/tmp/asd2"])

self.assertEqual(
str(warn.warning),
"config setting named 'packages_path' (configured through the "
"REZ_PACKAGES_PATH environment variable) is deprecated and will "
"be removed in 0.0.0.",
)

with restore_os_environ():
# Test with json env var
os.environ["REZ_PACKAGES_PATH_JSON"] = '["/tmp/asd2"]'
os.environ["REZ_DISABLE_HOME_CONFIG"] = "1"
config = Config._create_main_config()
with self.assertWarns(RezDeprecationWarning) as warn:
_ = config.data
# Assert just to ensure the test was set up properly.
self.assertEqual(config.data["packages_path"], ["/tmp/asd2"])

self.assertEqual(
str(warn.warning),
"config setting named 'packages_path' (configured through the "
"REZ_PACKAGES_PATH_JSON environment variable) is deprecated and will "
"be removed in 0.0.0.",
)


if __name__ == "__main__":
unittest.main()
7 changes: 7 additions & 0 deletions src/rez/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import functools
import sys
import json
import copy
from contextlib import contextmanager

# https://pypi.org/project/parameterized
Expand All @@ -39,6 +40,11 @@ def setUpClass(cls):
cls.settings = {}

def setUp(self):
# We have some tests that unfortunately don't clean themselves up
# after they are done. Store the origianl environment to be
# restored in tearDown
self.__environ = copy.deepcopy(os.environ)

self.maxDiff = None
os.environ["REZ_QUIET"] = "true"

Expand All @@ -56,6 +62,7 @@ def setup_once(self):

def tearDown(self):
self.teardown_config()
os.environ = self.__environ

@classmethod
def data_path(cls, *dirs):
Expand Down

0 comments on commit 94b6a05

Please sign in to comment.