Skip to content

Commit

Permalink
Ensure the environment is populated before processing per-alias env vars
Browse files Browse the repository at this point in the history
Fix a bad readme link
  • Loading branch information
MHendricks committed May 17, 2023
1 parent 3f9f9ed commit ad77a53
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ environment variable so you can simply run the `hab` command.
pip3 install hab
```

If you want to make use of (json5)[https://pypi.org/project/pyjson5/] formatting
If you want to make use of [json5](https://pypi.org/project/pyjson5/) formatting
when creating the various json files that drives hab, you should use the optional
json5 dependency. This lets you add comments and allows for trailing commas.

Expand Down
5 changes: 5 additions & 0 deletions hab/parsers/flat_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def _collect_values(self, node, default=False):
def _finalize_values(self):
"""Post processing done after `_collect_values` is run."""

# `_process_version` needs the global environment variables populated
# so populating per-alias env var's properly inherit the global variables.
# This call ensures that `self.frozen_data["environment"]` is populated.
self.environment

# Process version aliases, merging global env vars.
platform_aliases = {}
self.frozen_data["aliases"] = platform_aliases
Expand Down
7 changes: 6 additions & 1 deletion tests/configs/app/app_aliased_mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"distros": [
"aliased",
"aliased_mod"
]
],
"environment": {
"set" : {
"CONFIG_DEFINED": "config_variable"
}
}
}
1 change: 1 addition & 0 deletions tests/distros/aliased/2.0/list_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def print_var(var):
print_var("ALIASED_GLOBAL_D")
print_var("ALIASED_GLOBAL_E")
print_var("ALIASED_LOCAL")
print_var("CONFIG_DEFINED")
print("")

print(' PATH env var '.center(80, '-'))
Expand Down
7 changes: 3 additions & 4 deletions tests/test_freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def test_freeze(monkeypatch, config_root, platform, pathsep):
monkeypatch.setattr(os, 'pathsep', pathsep)
site = Site([config_root / "site_main.json"])
resolver = Resolver(site=site)

cfg_root = utils.path_forward_slash(config_root)
cfg = resolver.resolve("not_set/distros")

# Add a platform_path_maps mapping to convert the current hab checkout path
# to a generic know path on the other platform for uniform testing.
Expand All @@ -66,13 +64,14 @@ def test_freeze(monkeypatch, config_root, platform, pathsep):
else:
mappings['local-hab'][site.platform] = PurePosixPath(cfg_root)

# 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"

# Force the lazily loaded `cfg.frozen_data["environment"]` value to be loaded
cfg.environment
# 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
# simplify the output json data.
Expand Down
7 changes: 7 additions & 0 deletions tests/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,13 @@ def test_alias_mods_global(resolver):
assert cfg.environment['ALIASED_GLOBAL_E'] is None
assert cfg.environment['ALIASED_GLOBAL_F'] == ['Global F']
assert cfg.environment['ALIASED_MOD_GLOBAL_A'] == ['Global Mod A']
# This global env var was defined by the config json file.
# Ensure that it did not get lost at some point in the process.
assert cfg.environment['CONFIG_DEFINED'] == ['config_variable']
# This variable is always added by hab automatically
assert cfg.environment['HAB_URI'] == ['app/aliased/mod']
# Ensure no extra env vars were defined
assert len(cfg.environment) == 9

# Check cmd is expected
assert alias["cmd"][0] == "python"
Expand Down

0 comments on commit ad77a53

Please sign in to comment.