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

Add :parent path modifier #1160

Merged
merged 2 commits into from
Dec 18, 2023
Merged
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
13 changes: 13 additions & 0 deletions backend/src/hatchling/utils/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ def format_path(cls, path: str, modifier: str) -> str:
if not modifier:
return os.path.normpath(path)

modifiers = modifier.split(':')[::-1]
while modifiers and modifiers[-1] == 'parent':
path = os.path.dirname(path)
modifiers.pop()

if not modifiers:
return path

if len(modifiers) > 1:
message = f'Expected a single path modifier and instead got: {", ".join(reversed(modifiers))}'
raise ValueError(message)

modifier = modifiers[0]
if modifier == 'uri':
return path_to_uri(path)

Expand Down
11 changes: 11 additions & 0 deletions docs/config/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ All paths support the following modifiers:
| --- | --- |
| `uri` | The normalized absolute URI path prefixed by `file:` |
| `real` | The path with all symbolic links resolved |
| `parent` | The parent of the preceding path |

!!! tip
The `parent` modifier can be chained and may be combined with either the `uri` or `real` modifier, with the latter placed at the end. For example:

```toml config-example
[tool.hatch.envs.test]
dependencies = [
"example-project @ {root:parent:parent:uri}/example-project",
]
```

### System separators

Expand Down
1 change: 1 addition & 0 deletions docs/history/hatchling.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
***Added:***

- Add `bypass-selection` option to the `wheel` build target to allow for empty (metadata-only) wheels
- Add `parent` context modifier for path fields

***Fixed:***

Expand Down
39 changes: 38 additions & 1 deletion tests/backend/utils/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,58 @@ def test_default(self, isolation):
context = Context(isolation)
assert context.format('foo {root}') == f'foo {isolation}'

def test_parent(self, isolation):
context = Context(isolation)
path = os.path.dirname(str(isolation))
assert context.format('foo {root:parent}') == f'foo {path}'

def test_parent_parent(self, isolation):
context = Context(isolation)
path = os.path.dirname(os.path.dirname(str(isolation)))
assert context.format('foo {root:parent:parent}') == f'foo {path}'

def test_uri(self, isolation, uri_slash_prefix):
context = Context(isolation)
normalized_path = str(isolation).replace(os.sep, '/')
assert context.format('foo {root:uri}') == f'foo file:{uri_slash_prefix}{normalized_path}'

def test_uri_parent(self, isolation, uri_slash_prefix):
context = Context(isolation)
normalized_path = os.path.dirname(str(isolation)).replace(os.sep, '/')
assert context.format('foo {root:parent:uri}') == f'foo file:{uri_slash_prefix}{normalized_path}'

def test_uri_parent_parent(self, isolation, uri_slash_prefix):
context = Context(isolation)
normalized_path = os.path.dirname(os.path.dirname(str(isolation))).replace(os.sep, '/')
assert context.format('foo {root:parent:parent:uri}') == f'foo file:{uri_slash_prefix}{normalized_path}'

def test_real(self, isolation):
context = Context(isolation)
assert context.format('foo {root:real}') == f'foo {os.path.realpath(isolation)}'
real_path = os.path.realpath(isolation)
assert context.format('foo {root:real}') == f'foo {real_path}'

def test_real_parent(self, isolation):
context = Context(isolation)
real_path = os.path.dirname(os.path.realpath(isolation))
assert context.format('foo {root:parent:real}') == f'foo {real_path}'

def test_real_parent_parent(self, isolation):
context = Context(isolation)
real_path = os.path.dirname(os.path.dirname(os.path.realpath(isolation)))
assert context.format('foo {root:parent:parent:real}') == f'foo {real_path}'

def test_unknown_modifier(self, isolation):
context = Context(isolation)

with pytest.raises(ValueError, match='Unknown path modifier: bar'):
context.format('foo {root:bar}')

def test_too_many_modifiers_after_parent(self, isolation):
context = Context(isolation)

with pytest.raises(ValueError, match='Expected a single path modifier and instead got: foo, bar, baz'):
context.format('foo {root:parent:foo:bar:baz}')


class TestHome:
def test_default(self, isolation):
Expand Down