Skip to content

Commit

Permalink
Add optional_distros support
Browse files Browse the repository at this point in the history
This provides a way to suggest blessed `--requirement`'s. This includes
a description and default value.
  • Loading branch information
MHendricks committed Jul 19, 2024
1 parent 6cb5342 commit e59b263
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 12 deletions.
16 changes: 16 additions & 0 deletions hab/parsers/hab_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ def load(self, filename, data=None):
self.environment_config = data.get("environment", NotSet)
if self.min_verbosity is NotSet:
self.min_verbosity = data.get("min_verbosity", NotSet)
if self.optional_distros is NotSet:
self.optional_distros = data.get("optional_distros", NotSet)

if self.context is NotSet:
# TODO: make these use override methods
Expand Down Expand Up @@ -635,6 +637,20 @@ def name(self):
def name(self, name):
self.frozen_data["name"] = name

@hab_property(verbosity=1)
def optional_distros(self):
"""Information about distros chosen to be optionally enabled for a config.
The key is the distro including version specifier text. The value is a 2
item list containing a description to shown next to the key and a bool
indicating that it should be enabled by default when used.
"""
return self.frozen_data.get("optional_distros", NotSet)

@optional_distros.setter
def optional_distros(self, value):
self.frozen_data["optional_distros"] = value

def reduced(self, resolver=None, uri=None):
"""Returns a new instance with the final settings applied respecting inheritance"""
from . import FlatConfig
Expand Down
16 changes: 16 additions & 0 deletions tests/configs/optional/optional.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "optional",
"context": [],
"inherits": false,
"distros": [
"the_dcc"
],
"optional_distros": {
"maya2024": ["Adds new aliases"],
"the_dcc==1.0": ["Specific dcc version. Only choose one at a time."],
"the_dcc==1.2": ["Specific dcc version. Only choose one at a time."],
"the_dcc_plugin_a": ["Load an optional plugin by default", true],
"the_dcc_plugin_a==0.9": ["Force a specific version of this optinal plugin"],
"the_dcc_plugin_b": ["Only have a few licenses for this plugin, so opt into loading it"]
}
}
11 changes: 11 additions & 0 deletions tests/configs/optional/optional_child.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "child",
"context": ["optional"],
"inherits": false,
"distros": [
"the_dcc"
],
"optional_distros": {
"the_dcc_plugin_e": ["Different optional dependencies for a child URI.", true]
}
}
3 changes: 2 additions & 1 deletion tests/distros/the_dcc/1.0/.hab.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
],
"aliases": {
"windows": [
["dcc", "{relative_root}\\the_dcc.exe"]
["dcc", "{relative_root}\\the_dcc.exe"],
["dcc1.0", "{relative_root}\\the_dcc.exe"]
]
}
}
3 changes: 2 additions & 1 deletion tests/distros/the_dcc/1.1/.hab.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
],
"aliases": {
"windows": [
["dcc", "{relative_root}\\the_dcc.exe"]
["dcc", "{relative_root}\\the_dcc.exe"],
["dcc1.1", "{relative_root}\\the_dcc.exe"]
]
}
}
6 changes: 4 additions & 2 deletions tests/distros/the_dcc/1.2/.hab.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
],
"aliases": {
"linux": [
["dcc", "{relative_root}//the_dcc"]
["dcc", "{relative_root}//the_dcc"],
["dcc1.2", "{relative_root}//the_dcc"]
],
"windows": [
["dcc", "{relative_root}\\the_dcc.exe"]
["dcc", "{relative_root}\\the_dcc.exe"],
["dcc1.2", "{relative_root}\\the_dcc.exe"]
]
}
}
18 changes: 16 additions & 2 deletions tests/frozen.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@
"name": "distros",
"aliases": {
"windows": {
"dcc": "TEST_DIR_NAME\\the_dcc.exe"
"dcc": {
"cmd": "TEST_DIR_NAME\\the_dcc.exe",
"distro": ["the_dcc", "1.2"]
},
"dcc1.2": {
"cmd": "TEST_DIR_NAME\\the_dcc.exe",
"distro": ["the_dcc", "1.2"]
}
},
"linux": {
"dcc": "TEST_DIR_NAME//the_dcc"
"dcc": {
"cmd": "TEST_DIR_NAME//the_dcc",
"distro": ["the_dcc", "1.2"]
},
"dcc1.2": {
"cmd": "TEST_DIR_NAME//the_dcc",
"distro": ["the_dcc", "1.2"]
}
}
},
"versions": [
Expand Down
61 changes: 61 additions & 0 deletions tests/site_main_check.habcache
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,51 @@
},
"inherits": false
},
"{config-root}/configs/optional/optional.json": {
"name": "optional",
"context": [],
"inherits": false,
"distros": [
"the_dcc"
],
"optional_distros": {
"maya2024": [
"Adds new aliases"
],
"the_dcc==1.0": [
"Specific dcc version. Only choose one at a time."
],
"the_dcc==1.2": [
"Specific dcc version. Only choose one at a time."
],
"the_dcc_plugin_a": [
"Load an optional plugin by default",
true
],
"the_dcc_plugin_a==0.9": [
"Force a specific version of this optinal plugin"
],
"the_dcc_plugin_b": [
"Only have a few licenses for this plugin, so opt into loading it"
]
}
},
"{config-root}/configs/optional/optional_child.json": {
"name": "child",
"context": [
"optional"
],
"inherits": false,
"distros": [
"the_dcc"
],
"optional_distros": {
"the_dcc_plugin_e": [
"Different optional dependencies for a child URI.",
true
]
}
},
"{config-root}/configs/place-holder/place-holder_child.json": {
"name": "child",
"context": [
Expand Down Expand Up @@ -1372,6 +1417,10 @@
[
"dcc",
"{relative_root}\\the_dcc.exe"
],
[
"dcc1.0",
"{relative_root}\\the_dcc.exe"
]
]
},
Expand All @@ -1388,6 +1437,10 @@
[
"dcc",
"{relative_root}\\the_dcc.exe"
],
[
"dcc1.1",
"{relative_root}\\the_dcc.exe"
]
]
},
Expand All @@ -1406,12 +1459,20 @@
[
"dcc",
"{relative_root}//the_dcc"
],
[
"dcc1.2",
"{relative_root}//the_dcc"
]
],
"windows": [
[
"dcc",
"{relative_root}\\the_dcc.exe"
],
[
"dcc1.2",
"{relative_root}\\the_dcc.exe"
]
]
},
Expand Down
18 changes: 14 additions & 4 deletions tests/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,20 @@ def test_freeze(monkeypatch, config_root, platform, pathsep):
# Resolve the URI for frozen testing
cfg = resolver.resolve("not_set/distros")

# Ensure consistent testing across platforms. cfg has the current os's
# file paths instead of what is stored in frozen.json
cfg.frozen_data["aliases"]["linux"]["dcc"] = "TEST_DIR_NAME//the_dcc"
cfg.frozen_data["aliases"]["windows"]["dcc"] = "TEST_DIR_NAME\\the_dcc.exe"
for alias in ("dcc", "dcc1.2"):
# Ensure consistent testing across platforms. cfg has the current os's
# file paths instead of what is stored in frozen.json
cfg.frozen_data["aliases"]["linux"][alias]["cmd"] = "TEST_DIR_NAME//the_dcc"
cfg.frozen_data["aliases"]["windows"][alias][
"cmd"
] = "TEST_DIR_NAME\\the_dcc.exe"

# For ease of testing we also need to convert the distro tuple to a list
# that way it matches the json data stored in frozen.json
as_list = list(cfg.frozen_data["aliases"]["linux"][alias]["distro"])
cfg.frozen_data["aliases"]["linux"][alias]["distro"] = as_list
as_list = list(cfg.frozen_data["aliases"]["windows"][alias]["distro"])
cfg.frozen_data["aliases"]["windows"][alias]["distro"] = as_list

# Ensure the HAB_URI environment variable is defined on the FlatConfig object
# When checking the return from `cfg.freeze()` below HAB_URI is removed to
Expand Down
15 changes: 13 additions & 2 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ def test_metaclass():
"filename",
"min_verbosity",
"name",
"optional_distros",
"variables",
"version",
]
Expand All @@ -308,6 +309,7 @@ def test_metaclass():
"min_verbosity",
"inherits",
"name",
"optional_distros",
"uri",
"variables",
]
Expand All @@ -326,7 +328,11 @@ def standardize(txt):
# Build the test data so we can generate the output to check
# Note: using `repr([u"` so this test passes in python 2 and 3
pre = ["name: child", "uri: not_set/child"]
post = ["inherits: True", "min_verbosity: NotSet"]
post = [
"inherits: True",
"min_verbosity: NotSet",
"optional_distros: NotSet",
]
env = [
"environment: FMT_FOR_OS: a{;}b;c:{PATH!e}{;}d",
" TEST: case",
Expand Down Expand Up @@ -423,7 +429,12 @@ def test_no_values(self, resolver, uri):
for verbosity in range(3):
result = cfg.dump(verbosity=verbosity, color=False)
assert "aliases" not in result
assert "distros:" not in result
assert " distros:" not in result
if not verbosity:
# This is shown for v1 or higher
assert "optional_distros:" not in result
else:
assert "optional_distros:" in result


def test_environment(resolver):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class TestDumpForest:
" not_set/no_distros",
" not_set/no_env",
" not_set/os",
"optional",
" optional/child",
"place-holder",
" place-holder/child",
" place-holder/inherits",
Expand Down

0 comments on commit e59b263

Please sign in to comment.