Skip to content

Commit 911d81b

Browse files
committed
WIP: Add env chain test distros
1 parent 1573afa commit 911d81b

File tree

11 files changed

+251
-1
lines changed

11 files changed

+251
-1
lines changed

hab/resolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from . import utils
1414
from .errors import HabError, InvalidRequirementError, _IgnoredVersionError
15-
from .parsers import Config, HabBase, StubDistroVersion, Placeholder
15+
from .parsers import Config, HabBase, Placeholder, StubDistroVersion
1616
from .site import Site
1717
from .solvers import Solver
1818
from .user_prefs import UserPrefs
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "var-chain-a",
3+
"environment": {
4+
"os_specific": true,
5+
"*": {
6+
"set": {
7+
"OS_AGNOSTIC": "agnostic_value",
8+
"FROM_VAR_CHAIN_B": "--{VAR_CHAIN_B!e}--"
9+
}
10+
}
11+
}
12+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "var-chain-b",
3+
"variables": {
4+
"alias_specific_linux": "Linux: /{OS_AGNOSTIC!e}/test to {OS_SPECIFIC!e}",
5+
"alias_specific_windows": "Windows: c:/{OS_AGNOSTIC!e}/test to {OS_SPECIFIC!e}"
6+
},
7+
"aliases": {
8+
"linux": [
9+
["list_vars", ["python", "{relative_root}/list_vars.py"]],
10+
[
11+
"list_vars_env", {
12+
"cmd": ["python", "{relative_root}/list_vars.py"],
13+
"environment": {
14+
"set": {
15+
"ALIAS_SPECIFIC": "{alias_specific_linux}"
16+
}
17+
}
18+
}
19+
]
20+
],
21+
"windows": [
22+
["list_vars", ["python", "{relative_root}/list_vars.py"]],
23+
[
24+
"list_vars_env", {
25+
"cmd": ["python", "{relative_root}/list_vars.py"],
26+
"environment": {
27+
"set": {
28+
"ALIAS_SPECIFIC": "{alias_specific_windows}"
29+
}
30+
}
31+
}
32+
]
33+
]
34+
},
35+
"environment": {
36+
"os_specific": true,
37+
"*": {
38+
"VAR_CHAIN_B": "var-chain-b-wild"
39+
},
40+
"linux": {
41+
"set": {
42+
"OS_SPECIFIC": "/{OS_AGNOSTIC!e}",
43+
"VAR_CHAIN_B": "var-chain-b-linux"
44+
}
45+
},
46+
"windows": {
47+
"set": {
48+
"OS_SPECIFIC": "c:/{OS_AGNOSTIC!e}",
49+
"VAR_CHAIN_B": "var-chain-b-windows"
50+
}
51+
}
52+
}
53+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json
2+
import os
3+
4+
var_names = [
5+
"OS_AGNOSTIC",
6+
"OS_SPECIFIC",
7+
"ALIAS_SPECIFIC",
8+
"VAR_CHAIN_B",
9+
"FROM_VAR_CHAIN_B",
10+
]
11+
12+
result = {}
13+
for var_name in var_names:
14+
result[var_name] = os.getenv(var_name, "<UNSET>")
15+
16+
print(json.dumps(result, indent=4))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"OS_AGNOSTIC": "agnostic_value",
3+
"OS_SPECIFIC": "/agnostic_value",
4+
"ALIAS_SPECIFIC": "<UNSET>",
5+
"VAR_CHAIN_B": "var-chain-b-linux",
6+
"FROM_VAR_CHAIN_B": "----"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"OS_AGNOSTIC": "agnostic_value",
3+
"OS_SPECIFIC": "/agnostic_value",
4+
"ALIAS_SPECIFIC": "Linux: /agnostic_value/test to /agnostic_value",
5+
"VAR_CHAIN_B": "var-chain-b-linux",
6+
"FROM_VAR_CHAIN_B": "----"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"OS_AGNOSTIC": "agnostic_value",
3+
"OS_SPECIFIC": "c:/agnostic_value",
4+
"ALIAS_SPECIFIC": "<UNSET>",
5+
"VAR_CHAIN_B": "var-chain-b-windows",
6+
"FROM_VAR_CHAIN_B": "----"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"OS_AGNOSTIC": "agnostic_value",
3+
"OS_SPECIFIC": "c:/agnostic_value",
4+
"ALIAS_SPECIFIC": "Windows: c:/agnostic_value/test to c:/agnostic_value",
5+
"VAR_CHAIN_B": "var-chain-b-windows",
6+
"FROM_VAR_CHAIN_B": "----"
7+
}

tests/site_main_check.habcache

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,93 @@
17941794
},
17951795
"name": "the_dcc_plugin_e",
17961796
"version": "1.1"
1797+
},
1798+
"{config-root}/distros/var-chain-a/1.0/.hab.json": {
1799+
"environment": {
1800+
"*": {
1801+
"set": {
1802+
"FROM_VAR_CHAIN_B": "--{VAR_CHAIN_B!e}--",
1803+
"OS_AGNOSTIC": "agnostic_value"
1804+
}
1805+
},
1806+
"os_specific": true
1807+
},
1808+
"name": "var-chain-a",
1809+
"version": "1.0"
1810+
},
1811+
"{config-root}/distros/var-chain-b/1.0/.hab.json": {
1812+
"aliases": {
1813+
"linux": [
1814+
[
1815+
"list_vars",
1816+
[
1817+
"python",
1818+
"{relative_root}/list_vars.py"
1819+
]
1820+
],
1821+
[
1822+
"list_vars_env",
1823+
{
1824+
"cmd": [
1825+
"python",
1826+
"{relative_root}/list_vars.py"
1827+
],
1828+
"environment": {
1829+
"set": {
1830+
"ALIAS_SPECIFIC": "{alias_specific_linux}"
1831+
}
1832+
}
1833+
}
1834+
]
1835+
],
1836+
"windows": [
1837+
[
1838+
"list_vars",
1839+
[
1840+
"python",
1841+
"{relative_root}/list_vars.py"
1842+
]
1843+
],
1844+
[
1845+
"list_vars_env",
1846+
{
1847+
"cmd": [
1848+
"python",
1849+
"{relative_root}/list_vars.py"
1850+
],
1851+
"environment": {
1852+
"set": {
1853+
"ALIAS_SPECIFIC": "{alias_specific_windows}"
1854+
}
1855+
}
1856+
}
1857+
]
1858+
]
1859+
},
1860+
"environment": {
1861+
"*": {
1862+
"VAR_CHAIN_B": "var-chain-b-wild"
1863+
},
1864+
"linux": {
1865+
"set": {
1866+
"OS_SPECIFIC": "/{OS_AGNOSTIC!e}",
1867+
"VAR_CHAIN_B": "var-chain-b-linux"
1868+
}
1869+
},
1870+
"os_specific": true,
1871+
"windows": {
1872+
"set": {
1873+
"OS_SPECIFIC": "c:/{OS_AGNOSTIC!e}",
1874+
"VAR_CHAIN_B": "var-chain-b-windows"
1875+
}
1876+
}
1877+
},
1878+
"name": "var-chain-b",
1879+
"variables": {
1880+
"alias_specific_linux": "Linux: /{OS_AGNOSTIC!e}/test to {OS_SPECIFIC!e}",
1881+
"alias_specific_windows": "Windows: c:/{OS_AGNOSTIC!e}/test to {OS_SPECIFIC!e}"
1882+
},
1883+
"version": "1.0"
17971884
}
17981885
}
17991886
},

tests/test_env_var_chaining.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import json
2+
import re
3+
import subprocess
4+
from pathlib import Path
5+
6+
import pytest
7+
8+
# For each test .json file, create a test for each supported shell type.
9+
reference_dir = Path(__file__).parent / "env_resolution"
10+
references = []
11+
ids = []
12+
for _r in list(reference_dir.glob("*.json")):
13+
_plat = _r.name.split("-")[0]
14+
if _plat == "windows":
15+
_shells = ["bat", "ps1", "bash_win"]
16+
else:
17+
_shells = ["bash_linux"]
18+
for _shell in _shells:
19+
references.append((_shell, _r))
20+
ids.append(f"{_shell},{_r.name}")
21+
22+
23+
@pytest.mark.parametrize("shell,reference", references, ids=ids)
24+
def test_chaining(shell, reference, config_root, tmp_path, run_hab):
25+
26+
match = re.match(
27+
r"(?P<platform>[^-]+)-(?P<distros>[^-]+)-(?P<alias>[^.]+).json", reference.name
28+
)
29+
kwargs = match.groupdict()
30+
31+
# Skip tests that will not run on the current platform
32+
run_hab.skip_wrong_platform(shell)
33+
34+
runner = run_hab(config_root, tmp_path, stderr=subprocess.PIPE)
35+
sub_cmd = []
36+
for d in kwargs["distros"].split(","):
37+
sub_cmd.extend(["-r", f"var-chain-{d}"])
38+
sub_cmd += ["launch", ",", kwargs["alias"]]
39+
proc = runner.run_in_shell(shell, sub_cmd)
40+
41+
# Check that the env vars were set as expected
42+
assert proc.returncode == 0
43+
44+
check = json.load(reference.open())
45+
result = json.loads(proc.stdout)
46+
assert result == check

0 commit comments

Comments
 (0)