diff --git a/hab/merge_dict.py b/hab/merge_dict.py index 1c8f47a..69e57b7 100644 --- a/hab/merge_dict.py +++ b/hab/merge_dict.py @@ -8,10 +8,12 @@ def __init__(self, platform=None, **format_kwargs): self.format_kwargs = format_kwargs self.formatter = self.default_format self.pathsep = os.pathsep - # self.platform = platform - self.platform = 'windows' self.validator = None + if platform is None: + platform = utils.platform() + self.platform = platform + def default_format(self, value): if isinstance(value, list): # Format the individual items if a list of args is used. diff --git a/hab/parsers/hab_base.py b/hab/parsers/hab_base.py index fe2d3f7..ea71d94 100644 --- a/hab/parsers/hab_base.py +++ b/hab/parsers/hab_base.py @@ -3,7 +3,6 @@ import logging import os import subprocess -import sys from pathlib import Path import anytree @@ -81,7 +80,7 @@ def _platform(self): if self._platform_override: # Provide a method for testing to always test for running on a specific os return self._platform_override - return "windows" if sys.platform == "win32" else "linux" + return utils.platform() def check_environment(self, environment_config): """Check that the environment config only makes valid adjustments. @@ -514,7 +513,7 @@ def update_environment(self, environment_config, obj=None): # No environment_config dictionary was set, noting to do return - merger = MergeDict(relative_root=self.dirname) + merger = MergeDict(relative_root=self.dirname, platform=self._platform) merger.formatter = obj.format_environment_value merger.validator = self.check_environment merger.update(self._environment, environment_config) diff --git a/hab/utils.py b/hab/utils.py index 0981c6c..856b2da 100644 --- a/hab/utils.py +++ b/hab/utils.py @@ -172,3 +172,8 @@ def load_json_file(filename): def path_forward_slash(path): """Converts a Path object into a string with forward slashes""" return str(path).replace('\\', '/') + + +def platform(): + """Returns the current operating system as `windows` or `linux`.""" + return "windows" if sys.platform == "win32" else "linux" diff --git a/tests/configs/not_set/os_env.json b/tests/configs/not_set/os_env.json new file mode 100644 index 0000000..11b70e2 --- /dev/null +++ b/tests/configs/not_set/os_env.json @@ -0,0 +1,36 @@ +{ + "name": "os", + "context": ["not_set"], + "environment": { + "os_specific": true, + "linux": { + "unset": [ + "UNSET_VARIABLE_LIN" + ], + "set": { + "SET_VARIABLE_LIN": "set_value_lin" + }, + "append": { + "APPEND_VARIABLE_LIN": "append_value_lin" + }, + "prepend": { + "PREPEND_VARIABLE_LIN": "prepend_value_lin" + } + }, + "windows": { + "unset": [ + "UNSET_VARIABLE_WIN" + ], + "set": { + "SET_VARIABLE_WIN": "set_value_win" + }, + "append": { + "APPEND_VARIABLE_WIN": "append_value_win" + }, + "prepend": { + "PREPEND_VARIABLE_WIN": "prepend_value_win" + } + } + }, + "inherits": false +} diff --git a/tests/site_os_specific.json b/tests/site_os_specific.json new file mode 100644 index 0000000..94f1043 --- /dev/null +++ b/tests/site_os_specific.json @@ -0,0 +1,23 @@ +{ + "os_specific": true, + "linux": { + "append": { + "config_paths": [ + "config/path" + ], + "distro_paths": [ + "distro/path" + ] + } + }, + "windows": { + "append": { + "config_paths": [ + "config\\path" + ], + "distro_paths": [ + "distro\\path" + ] + } + } +} diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 86d4525..4a4cb35 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -702,3 +702,29 @@ def test_duplicated_distros(config_root, resolver): with pytest.raises(DuplicateJsonError): resolver.find_distro("the_dcc==1.2") + + +def test_os_specific_linux(monkeypatch, resolver): + """Check that if "os_specific" is set to true, only env vars for the current + os are resolved.""" + # Simulate running on a linux platform. + monkeypatch.setattr(sys, "platform", "linux") + cfg = resolver.resolve("not_set/os") + + assert cfg.environment["UNSET_VARIABLE_LIN"] is None + assert cfg.environment["SET_VARIABLE_LIN"] == ["set_value_lin"] + assert cfg.environment["APPEND_VARIABLE_LIN"] == ["append_value_lin"] + assert cfg.environment["PREPEND_VARIABLE_LIN"] == ["prepend_value_lin"] + + +def test_os_specific_win(monkeypatch, resolver): + """Check that if "os_specific" is set to true, only env vars for the current + os are resolved.""" + # Simulate running on a windows platform + monkeypatch.setattr(sys, "platform", "win32") + cfg = resolver.resolve("not_set/os") + + assert cfg.environment["UNSET_VARIABLE_WIN"] is None + assert cfg.environment["SET_VARIABLE_WIN"] == ["set_value_win"] + assert cfg.environment["APPEND_VARIABLE_WIN"] == ["append_value_win"] + assert cfg.environment["PREPEND_VARIABLE_WIN"] == ["prepend_value_win"] diff --git a/tests/test_resolver.py b/tests/test_resolver.py index e63a514..5029a96 100644 --- a/tests/test_resolver.py +++ b/tests/test_resolver.py @@ -113,7 +113,8 @@ def test_dump_forest(resolver): " |-- hab.parsers.config.Config('not_set/env_path_set')", " |-- hab.parsers.config.Config('not_set/env_path_unset')", " |-- hab.parsers.config.Config('not_set/distros')", - " +-- hab.parsers.config.Config('not_set/no_env')", + " |-- hab.parsers.config.Config('not_set/no_env')", + " +-- hab.parsers.config.Config('not_set/os')", "place-holder", " hab.parsers.placeholder.Placeholder('place-holder')", " |-- hab.parsers.config.Config('place-holder/child')", diff --git a/tests/test_site.py b/tests/test_site.py index a701b8d..37481b4 100644 --- a/tests/test_site.py +++ b/tests/test_site.py @@ -1,3 +1,5 @@ +import sys + import colorama import pytest @@ -78,3 +80,29 @@ def test_dump(config_root): result = site.dump() for check in checks: assert check.format(green="", reset="") in result + + +def test_os_specific_linux(monkeypatch, config_root): + """Check that if "os_specific" is set to true, only vars for the current + os are resolved.""" + # Simulate running on a linux platform. + monkeypatch.setattr(sys, "platform", "linux") + + paths = [config_root / "site_os_specific.json"] + site = Site(paths) + + assert site.get("config_paths") == ["config/path"] + assert site.get("distro_paths") == ["distro/path"] + + +def test_os_specific_win(monkeypatch, config_root): + """Check that if "os_specific" is set to true, only vars for the current + os are resolved.""" + # Simulate running on a windows platform + monkeypatch.setattr(sys, "platform", "win32") + + paths = [config_root / "site_os_specific.json"] + site = Site(paths) + + assert site.get("config_paths") == ["config\\path"] + assert site.get("distro_paths") == ["distro\\path"]