Skip to content

Commit 8686f14

Browse files
committed
Fix unhelpful message when a metadata file (fixes #1145)
1 parent 8a918ca commit 8686f14

5 files changed

Lines changed: 40 additions & 3 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Release 0.14.0 (unreleased)
1313
* Add new ``remove`` command to remove projects from manifest and disk (#26)
1414
* Fix "unsafe symlink target" error for archives containing relative ``..`` symlinks (#1122)
1515
* Fix ``dfetch add`` crashing with a ``ValueError`` when the remote URL has a trailing slash (#1137)
16+
* Fix unhelpful message when a metadata file (#1145)
1617

1718
Release 0.13.0 (released 2026-03-30)
1819
====================================

dfetch/project/metadata.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ def from_project_entry(cls, project: ProjectEntry) -> "Metadata":
8888
def from_file(cls, path: str) -> "Metadata":
8989
"""Load metadata file."""
9090
with open(path, encoding="utf-8") as metadata_file:
91-
data: Options = yaml.safe_load(metadata_file)["dfetch"]
91+
try:
92+
data: Options = yaml.safe_load(metadata_file)["dfetch"]
93+
except yaml.YAMLError as exc:
94+
raise ValueError(str(exc)) from exc
95+
9296
return cls(data)
9397

9498
def fetched(

dfetch/project/subproject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def on_disk_version(self) -> Version | None:
348348

349349
try:
350350
return Metadata.from_file(self.__metadata.path).version
351-
except TypeError:
351+
except (TypeError, ValueError):
352352
logger.print_warning_line(
353353
self.__project.name,
354354
f"{pathlib.Path(self.__metadata.path).relative_to(os.getcwd()).as_posix()}"
@@ -367,7 +367,7 @@ def _on_disk_hash(self) -> str | None:
367367

368368
try:
369369
return Metadata.from_file(self.__metadata.path).hash
370-
except TypeError:
370+
except (TypeError, ValueError):
371371
logger.print_warning_line(
372372
self.__project.name,
373373
f"{pathlib.Path(self.__metadata.path).relative_to(os.getcwd()).as_posix()}"

features/handle-invalid-metadata.feature

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ Feature: Handle invalid metadata files
66
to unpredictable behavior in *DFetch*.
77
For instance, the metadata may be invalid
88

9+
Scenario: Metadata with invalid YAML syntax is treated as invalid
10+
Given the manifest 'dfetch.yaml'
11+
"""
12+
manifest:
13+
version: '0.0'
14+
15+
projects:
16+
- name: ext/test-repo-tag
17+
url: https://github.com/dfetch-org/test-repo
18+
tag: v1
19+
20+
"""
21+
And all projects are updated
22+
And the metadata file ".dfetch_data.yaml" of "ext/test-repo-tag" has invalid yaml
23+
When I run "dfetch update"
24+
Then the output shows
25+
"""
26+
Dfetch (0.13.0)
27+
ext/test-repo-tag:
28+
> ext/test-repo-tag/.dfetch_data.yaml is an invalid metadata file, not checking on disk version!
29+
> ext/test-repo-tag/.dfetch_data.yaml is an invalid metadata file, not checking local hash!
30+
> Fetched v1
31+
"""
32+
933
Scenario: Invalid metadata is ignored
1034
Given the manifest 'dfetch.yaml'
1135
"""

features/steps/generic_steps.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ def step_impl(_, metadata_file, project_path):
282282
)
283283

284284

285+
@given('the metadata file "{metadata_file}" of "{project_path}" has invalid yaml')
286+
def step_impl(_, metadata_file, project_path):
287+
generate_file(
288+
os.path.join(os.getcwd(), project_path, metadata_file),
289+
"key: [unclosed bracket\n",
290+
)
291+
292+
285293
@given('the metadata file "{metadata_file}" of "{project_path}" is changed')
286294
def step_impl(_, metadata_file, project_path):
287295
extend_file(

0 commit comments

Comments
 (0)