diff --git a/snapcraft/const.py b/snapcraft/const.py index fcf090978a..6543ea5519 100644 --- a/snapcraft/const.py +++ b/snapcraft/const.py @@ -47,3 +47,13 @@ def __str__(self) -> str: CURRENT_BASES = frozenset(BASES - ESM_BASES - LEGACY_BASES) """Bases handled by the current snapcraft codebase.""" + + +SNAPCRAFT_ENVIRONMENT_VARIABLES = frozenset( + { + "SNAPCRAFT_BUILD_INFO", + "SNAPCRAFT_IMAGE_INFO", + "SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS", + } +) +"""Snapcraft-specific environment variables.""" diff --git a/snapcraft/services/provider.py b/snapcraft/services/provider.py index 2ed2b59083..def7267d69 100644 --- a/snapcraft/services/provider.py +++ b/snapcraft/services/provider.py @@ -20,19 +20,16 @@ from craft_application import ProviderService from overrides import overrides +from snapcraft.const import SNAPCRAFT_ENVIRONMENT_VARIABLES + class Provider(ProviderService): """Snapcraft specialization of the Lifecycle Service.""" @overrides def setup(self) -> None: - if build_info := os.getenv("SNAPCRAFT_BUILD_INFO"): - self.environment["SNAPCRAFT_BUILD_INFO"] = build_info - if image_info := os.getenv("SNAPCRAFT_IMAGE_INFO"): - self.environment["SNAPCRAFT_IMAGE_INFO"] = image_info - if experimental_extensions := os.getenv( - "SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS" - ): - self.environment["SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS"] = ( - experimental_extensions - ) + """Add snapcraft environment variables to the build environment.""" + super().setup() + for variable in SNAPCRAFT_ENVIRONMENT_VARIABLES: + if variable in os.environ: + self.environment[variable] = os.environ[variable] diff --git a/tests/unit/services/test_provider.py b/tests/unit/services/test_provider.py index 8735e4d763..68c008ecc3 100644 --- a/tests/unit/services/test_provider.py +++ b/tests/unit/services/test_provider.py @@ -16,15 +16,19 @@ """Tests for the Snapcraft provider service.""" +from craft_application.services.provider import DEFAULT_FORWARD_ENVIRONMENT_VARIABLES + +from snapcraft.const import SNAPCRAFT_ENVIRONMENT_VARIABLES + def test_provider(provider_service, monkeypatch): - monkeypatch.setenv("SNAPCRAFT_BUILD_INFO", "foo") - monkeypatch.setenv("SNAPCRAFT_IMAGE_INFO", "bar") - monkeypatch.setenv("SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS", "baz") - provider_service.setup() - assert provider_service.environment["SNAPCRAFT_BUILD_INFO"] == "foo" - assert provider_service.environment["SNAPCRAFT_IMAGE_INFO"] == "bar" - assert ( - provider_service.environment["SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS"] - == "baz" + variables = SNAPCRAFT_ENVIRONMENT_VARIABLES | set( + DEFAULT_FORWARD_ENVIRONMENT_VARIABLES ) + for variable in variables: + monkeypatch.setenv(variable, f"{variable}-test-value") + + provider_service.setup() + + for variable in variables: + assert provider_service.environment[variable] == f"{variable}-test-value"