Skip to content

Add default option to RegexSource (#1980) #1982

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

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
4 changes: 3 additions & 1 deletion backend/src/hatchling/version/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def __init__(self, root: str, relative_path: str) -> None:
self.__path = os.path.normpath(os.path.join(root, relative_path))
self.__cached_read_data: tuple | None = None

def read(self, *, pattern: str | bool) -> str:
def read(self, *, pattern: str | bool, default: str | None = None) -> str:
if not os.path.isfile(self.__path):
if default is not None:
return default
message = f'file does not exist: {self.__relative_path}'
raise OSError(message)

Expand Down
7 changes: 6 additions & 1 deletion backend/src/hatchling/version/source/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ def get_version_data(self) -> dict:
message = 'option `pattern` must be a string'
raise TypeError(message)

default = self.config.get('default')
if default is not None and not isinstance(default, str):
message = 'option `default` must be a string'
raise TypeError(message)

version_file = VersionFile(self.root, relative_path)
version = version_file.read(pattern=pattern)
version = version_file.read(pattern=pattern, default=default)

return {'version': version, 'version_file': version_file}

Expand Down
1 change: 1 addition & 0 deletions docs/plugins/version-source/regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ source = "regex"
| --- | --- |
| `path` (required) | A relative path to a file containing the project's version |
| `pattern` | A regular expression that has a named group called `version` that represents the version. The default pattern looks for a variable named `__version__` or `VERSION` that is set to a string containing the version, optionally prefixed with the lowercase letter `v`. |
| `default` | A value used as the version if the file at `path` does not exist. |
25 changes: 25 additions & 0 deletions tests/backend/version/source/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,28 @@ def foo():
with temp_dir.as_cwd():
source.set_version('foo', source.get_version_data())
assert source.get_version_data()['version'] == 'foo'


@pytest.mark.parametrize(
('file_contents', 'default', 'expected_version'),
[
('1.2.3', '0.0.0', '1.2.3'),
('1.2.3', '0.0.1', '1.2.3'),
(None, '0.dev', '0.dev'),
(None, '9.9.9', '9.9.9'),
],
)
def test_with_default_value(temp_dir, file_contents, default, expected_version):
source = RegexSource(
str(temp_dir),
{'path': 'x/y', 'pattern': '^(?P<version>.+)$', 'default': default},
)

file_path = temp_dir / 'x' / 'y'
file_path.ensure_parent_dir_exists()

if file_contents is not None:
file_path.write_text(file_contents)

with temp_dir.as_cwd():
assert source.get_version_data()['version'] == expected_version