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