diff --git a/README.md b/README.md index 98aa66e..aec1ee7 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/hab/parsers/flat_config.py b/hab/parsers/flat_config.py index 70c3c30..0bc5c56 100644 --- a/hab/parsers/flat_config.py +++ b/hab/parsers/flat_config.py @@ -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 diff --git a/tests/configs/app/app_aliased_mod.json b/tests/configs/app/app_aliased_mod.json index 9c71607..925f40a 100644 --- a/tests/configs/app/app_aliased_mod.json +++ b/tests/configs/app/app_aliased_mod.json @@ -5,5 +5,10 @@ "distros": [ "aliased", "aliased_mod" - ] + ], + "environment": { + "set" : { + "CONFIG_DEFINED": "config_variable" + } + } } diff --git a/tests/distros/aliased/2.0/list_vars.py b/tests/distros/aliased/2.0/list_vars.py index face471..5182452 100644 --- a/tests/distros/aliased/2.0/list_vars.py +++ b/tests/distros/aliased/2.0/list_vars.py @@ -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, '-')) diff --git a/tests/test_freeze.py b/tests/test_freeze.py index 31f3818..b20622d 100644 --- a/tests/test_freeze.py +++ b/tests/test_freeze.py @@ -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. @@ -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. diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 75b2a6d..a8086f0 100644 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -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"