Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env version-source: add a fallback value #1710

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions backend/src/hatchling/version/source/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class EnvSource(VersionSourceInterface):

def get_version_data(self) -> dict:
variable = self.config.get('variable', '')
default_value = self.config.get('default-value', '')

if not variable:
message = 'option `variable` must be specified'
raise ValueError(message)
Expand All @@ -18,11 +20,15 @@ def get_version_data(self) -> dict:
message = 'option `variable` must be a string'
raise TypeError(message)

if variable not in os.environ:
if default_value and not isinstance(default_value, str):
message = 'option `default-value` must be a string'
raise TypeError(message)

if variable not in os.environ and not default_value:
message = f'environment variable `{variable}` is not set'
raise RuntimeError(message)

return {'version': os.environ[variable]}
return {'version': os.getenv(variable, default_value)}

def set_version(self, version: str, version_data: dict) -> None: # noqa: ARG002, PLR6301
message = 'Cannot set environment variables'
Expand Down
1 change: 1 addition & 0 deletions docs/plugins/version-source/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ source = "env"
| Option | Description |
| --- | --- |
| `variable` (required) | The name of the environment variable |
| `default-value` | The value to use if the environment variable is not set |
14 changes: 14 additions & 0 deletions tests/backend/version/source/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def test_variable_not_string(isolation):
source.get_version_data()


def test_default_value_not_string(isolation):
source = EnvSource(str(isolation), {'variable': 'ENV_VERSION', 'default-value': 1})

with pytest.raises(TypeError, match='option `default-value` must be a string'):
source.get_version_data()


def test_variable_not_available(isolation):
source = EnvSource(str(isolation), {'variable': 'ENV_VERSION'})

Expand All @@ -27,6 +34,13 @@ def test_variable_not_available(isolation):
source.get_version_data()


def test_variable_not_available_with_default_value(isolation):
source = EnvSource(str(isolation), {'variable': 'ENV_VERSION', 'default-value': '0.0.1'})

with EnvVars(exclude=['ENV_VERSION']):
assert source.get_version_data()['version'] == '0.0.1'


def test_variable_contains_version(isolation):
source = EnvSource(str(isolation), {'variable': 'ENV_VERSION'})

Expand Down