From 2be608d5688a2062ade9c547cb97abfb50ef0375 Mon Sep 17 00:00:00 2001 From: Abraham Murciano Date: Sun, 15 Dec 2024 16:26:00 +0200 Subject: [PATCH 1/2] add tests for default on env version source Added two tests for the `env` version source: 1. the environment variable is not set, but a default is configured 2. the environment variable is set and a default is configured --- tests/backend/version/source/test_env.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/backend/version/source/test_env.py b/tests/backend/version/source/test_env.py index 3e8d74847..a840b634c 100644 --- a/tests/backend/version/source/test_env.py +++ b/tests/backend/version/source/test_env.py @@ -27,8 +27,22 @@ def test_variable_not_available(isolation): source.get_version_data() +def test_variable_not_available_with_default(isolation): + source = EnvSource(str(isolation), {'variable': 'ENV_VERSION', 'default': '0.0.0'}) + + with EnvVars(exclude=['ENV_VERSION']): + assert source.get_version_data()['version'] == '0.0.0' + + def test_variable_contains_version(isolation): source = EnvSource(str(isolation), {'variable': 'ENV_VERSION'}) with EnvVars({'ENV_VERSION': '0.0.1'}): assert source.get_version_data()['version'] == '0.0.1' + + +def test_variable_contains_version_with_default(isolation): + source = EnvSource(str(isolation), {'variable': 'ENV_VERSION', 'default': '0.0.0'}) + + with EnvVars({'ENV_VERSION': '0.0.1'}): + assert source.get_version_data()['version'] == '0.0.1' From f006cae63e9fdddd54ee7d731e6152ff7d530760 Mon Sep 17 00:00:00 2001 From: Abraham Murciano Date: Sun, 15 Dec 2024 16:32:23 +0200 Subject: [PATCH 2/2] use default version if env var is not set in env version source --- backend/src/hatchling/version/source/env.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backend/src/hatchling/version/source/env.py b/backend/src/hatchling/version/source/env.py index b46389437..4d80c6e13 100644 --- a/backend/src/hatchling/version/source/env.py +++ b/backend/src/hatchling/version/source/env.py @@ -19,6 +19,9 @@ def get_version_data(self) -> dict: raise TypeError(message) if variable not in os.environ: + default = self.config.get('default', '') + if default: + return {'version': default} message = f'environment variable `{variable}` is not set' raise RuntimeError(message)