[tests] Remote packages with substitutions (#12145)

This commit is contained in:
Javier Peletier
2025-11-28 19:15:55 +01:00
committed by GitHub
parent fb82362e9c
commit e15f3a08ae
7 changed files with 122 additions and 1 deletions

View File

@@ -0,0 +1,25 @@
substitutions:
x: 10
y: 20
z: 30
values_from_repo1_main:
- package_name: package1
x: 3
y: 4
z: 5
volume: 60
- package_name: package2
x: 6
y: 7
z: 8
volume: 336
- package_name: default
x: 10
y: 20
z: 5
volume: 1000
- package_name: package4_from_repo2
x: 9
y: 10
z: 11
volume: 990

View File

@@ -0,0 +1,37 @@
substitutions:
x: 10
y: 20
z: 30
packages:
package1:
url: https://github.com/esphome/repo1
files:
- path: file1.yaml
vars:
package_name: package1
x: 3
y: 4
ref: main
package2: !include # a package that just includes the given remote package
file: remote_package_proxy.yaml
vars:
url: https://github.com/esphome/repo1
ref: main
files:
- path: file1.yaml
vars:
package_name: package2
x: 6
y: 7
z: 8
package3: github://esphome/repo1/file1.yaml@main # a package that uses the shorthand syntax
package4: # include repo2, which itself includes repo1
url: https://github.com/esphome/repo2
files:
- path: file2.yaml
vars:
package_name: package4
a: 9
b: 10
c: 11
ref: main

View File

@@ -0,0 +1,6 @@
# acts as a proxy to be able to include a remote package
# in which the url/ref/files come from a substitution
packages:
- url: ${url}
ref: ${ref}
files: ${files}

View File

@@ -0,0 +1,3 @@
This folder contains fake repos for remote packages testing
These are used by `test_substitutions.py`.
To add repos, create a folder and add its path to the `REMOTES` constant in `test_substitutions.py`.

View File

@@ -0,0 +1,9 @@
defaults:
z: 5
package_name: default
values_from_repo1_main:
- package_name: ${package_name}
x: ${x}
y: ${y}
z: ${z}
volume: ${x*y*z}

View File

@@ -0,0 +1,10 @@
packages:
- url: https://github.com/esphome/repo1
ref: main
files:
- path: file1.yaml
vars:
package_name: ${package_name}_from_repo2
x: ${a}
y: ${b}
z: ${c}

View File

@@ -2,6 +2,7 @@ import glob
import logging import logging
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
from unittest.mock import patch
from esphome import config as config_module, yaml_util from esphome import config as config_module, yaml_util
from esphome.components import substitutions from esphome.components import substitutions
@@ -84,11 +85,41 @@ def verify_database(value: Any, path: str = "") -> str | None:
return None return None
def test_substitutions_fixtures(fixture_path): # Mapping of (url, ref) to local test repository path under fixtures/substitutions
REMOTES = {
("https://github.com/esphome/repo1", "main"): "remotes/repo1/main",
("https://github.com/esphome/repo2", "main"): "remotes/repo2/main",
}
@patch("esphome.git.clone_or_update")
def test_substitutions_fixtures(mock_clone_or_update, fixture_path):
base_dir = fixture_path / "substitutions" base_dir = fixture_path / "substitutions"
sources = sorted(glob.glob(str(base_dir / "*.input.yaml"))) sources = sorted(glob.glob(str(base_dir / "*.input.yaml")))
assert sources, f"No input YAML files found in {base_dir}" assert sources, f"No input YAML files found in {base_dir}"
def fake_clone_or_update(
*,
url: str,
ref: str | None = None,
refresh=None,
domain: str,
username: str | None = None,
password: str | None = None,
submodules: list[str] | None = None,
_recover_broken: bool = True,
) -> tuple[Path, None]:
path = REMOTES.get((url, ref))
if path is None:
path = REMOTES.get((url.rstrip(".git"), ref))
if path is None:
raise RuntimeError(
f"Cannot find test repository for {url} @ {ref}. Check the REMOTES mapping in test_substitutions.py"
)
return base_dir / path, None
mock_clone_or_update.side_effect = fake_clone_or_update
failures = [] failures = []
for source_path in sources: for source_path in sources:
source_path = Path(source_path) source_path = Path(source_path)