Skip to content

Commit

Permalink
Add tests for os_specific MergeDict modifier
Browse files Browse the repository at this point in the history
Move platform lookup to utils and hab_base passes it to MergeDict
  • Loading branch information
MHendricks committed Aug 23, 2022
1 parent abec96c commit 0677dad
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 6 deletions.
6 changes: 4 additions & 2 deletions hab/merge_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions hab/parsers/hab_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import subprocess
import sys
from pathlib import Path

import anytree
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions hab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
36 changes: 36 additions & 0 deletions tests/configs/not_set/os_env.json
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 23 additions & 0 deletions tests/site_os_specific.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
26 changes: 26 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 2 additions & 1 deletion tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')",
Expand Down
28 changes: 28 additions & 0 deletions tests/test_site.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

import colorama
import pytest

Expand Down Expand Up @@ -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"]

0 comments on commit 0677dad

Please sign in to comment.