|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import copy |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from dataclasses import dataclass, field |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from hab.formatter import Formatter |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class FileRef: |
| 17 | + path: Path |
| 18 | + name: str = field(init=False) |
| 19 | + shell: str | None = field(default=None) |
| 20 | + platform: str = field(init=False) |
| 21 | + distros: list = field(init=False) |
| 22 | + alias: str = field(init=False) |
| 23 | + _parser = re.compile( |
| 24 | + r"(?P<platform>[^-]+)-(?P<distros>[^-]+)-(?P<alias>[^.]+).json" |
| 25 | + ) |
| 26 | + |
| 27 | + def __post_init__(self) -> None: |
| 28 | + if not hasattr(self, "name"): |
| 29 | + self.name = self.path.stem |
| 30 | + match = self._parser.match(self.path.name) |
| 31 | + if not match: |
| 32 | + raise ValueError(f"Invalid filename: {self.path}") |
| 33 | + kwargs = match.groupdict() |
| 34 | + self.platform = kwargs["platform"] |
| 35 | + self.distros = kwargs["distros"].split(",") |
| 36 | + self.alias = kwargs["alias"] |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def from_glob(cls, path, glob_str="*.json"): |
| 40 | + ret = [] |
| 41 | + for filename in path.glob(glob_str): |
| 42 | + ret.append(cls(filename)) |
| 43 | + return ret |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def shell_matrix(cls, file_refs, shells: dict[str, list]) -> list: |
| 47 | + ret = [] |
| 48 | + for file_ref in file_refs: |
| 49 | + plat_shells = shells.get(file_ref.platform, []) |
| 50 | + for shell in plat_shells: |
| 51 | + ref = copy.copy(file_ref) |
| 52 | + ref.shell = shell |
| 53 | + ret.append(ref) |
| 54 | + ret.sort(key=lambda i: repr(i)) |
| 55 | + return ret |
| 56 | + |
| 57 | + def __repr__(self) -> str: |
| 58 | + if self.shell: |
| 59 | + return f"{self.shell},{self.name}" |
| 60 | + return self.name |
| 61 | + |
| 62 | + |
| 63 | +references = FileRef.from_glob(Path(__file__).parent / "var_expanding") |
| 64 | +shell_references = FileRef.shell_matrix( |
| 65 | + references, {"windows": ["bash_win", "bat", "ps1"], "linux": ["bash_linux"]} |
| 66 | +) |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize("reference", shell_references, ids=lambda f: repr(f)) |
| 70 | +def test_expanding(reference, config_root, tmp_path, run_hab): |
| 71 | + # Skip tests that will not run on the current platform |
| 72 | + run_hab.skip_wrong_platform(reference.shell) |
| 73 | + |
| 74 | + runner = run_hab(config_root, tmp_path, stderr=subprocess.PIPE) |
| 75 | + sub_cmd = [] |
| 76 | + for d in reference.distros: |
| 77 | + sub_cmd.extend(["-r", f"var-expand-{d}"]) |
| 78 | + sub_cmd += ["launch", ",", reference.alias] |
| 79 | + proc = runner.run_in_shell(reference.shell, sub_cmd) |
| 80 | + |
| 81 | + with runner.std_on_failure(proc): |
| 82 | + # Check that the env vars were set as expected |
| 83 | + assert proc.returncode == 0 |
| 84 | + |
| 85 | + # Replace `{;;}` with the correct pathsep for the test. This is required because |
| 86 | + # bash on windows uses the linux pathsep |
| 87 | + languages = { |
| 88 | + "bat": "batch", |
| 89 | + "ps1": "ps", |
| 90 | + "bash_win": "shwin", |
| 91 | + "bash_linux": "sh", |
| 92 | + } |
| 93 | + pathsep = Formatter.shell_formats[languages[reference.shell]][";"] |
| 94 | + check = reference.path.open().read() |
| 95 | + check = check.replace("{;;}", pathsep) |
| 96 | + |
| 97 | + assert proc.stdout == check |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.parametrize("reference", references, ids=lambda f: repr(f)) |
| 101 | +def test_launch(habcached_resolver, reference): |
| 102 | + pathsep = ":" |
| 103 | + if sys.platform.startswith("linux"): |
| 104 | + if reference.platform != "linux": |
| 105 | + raise pytest.skip("test doesn't apply on linux") |
| 106 | + elif sys.platform == "win32": |
| 107 | + pathsep = ";" |
| 108 | + if reference.platform != "windows": |
| 109 | + raise pytest.skip("test doesn't apply on windows") |
| 110 | + |
| 111 | + forced_requirements = [] |
| 112 | + for d in reference.distros: |
| 113 | + forced_requirements.extend([f"var-expand-{d}"]) |
| 114 | + cfg = habcached_resolver.resolve(",", forced_requirements=forced_requirements) |
| 115 | + |
| 116 | + proc = cfg.launch(reference.alias, args=None, blocking=True, stderr=subprocess.PIPE) |
| 117 | + assert proc.returncode == 0 |
| 118 | + |
| 119 | + # Replace `{;;}` with the correct pathsep for the test. This is required because |
| 120 | + # bash on windows uses the linux pathsep |
| 121 | + check = reference.path.open().read() |
| 122 | + check = check.replace("{;;}", pathsep) |
| 123 | + |
| 124 | + assert check == proc.output_stdout |
0 commit comments