Skip to content

Commit f5e94f8

Browse files
committed
Review comments
1 parent 8686f14 commit f5e94f8

6 files changed

Lines changed: 41 additions & 10 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +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)
16+
* Fix unhelpful error message when a metadata file is malformed (#1145)
1717

1818
Release 0.13.0 (released 2026-03-30)
1919
====================================

dfetch/commands/report.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from dfetch.log import get_logger
5151
from dfetch.manifest.project import ProjectEntry
5252
from dfetch.project import create_super_project
53-
from dfetch.project.metadata import Metadata
53+
from dfetch.project.metadata import InvalidMetadataError, Metadata
5454
from dfetch.reporting import REPORTERS, ReportTypes
5555
from dfetch.util.license import (
5656
LicenseScanResult,
@@ -171,6 +171,6 @@ def _determine_version(project: ProjectEntry) -> str:
171171
or project.hash
172172
or ""
173173
)
174-
except FileNotFoundError:
174+
except (FileNotFoundError, InvalidMetadataError):
175175
version = project.tag or project.revision or project.hash or ""
176176
return version

dfetch/project/metadata.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
"""
1717

1818

19+
class InvalidMetadataError(Exception):
20+
"""Raised when a metadata file exists but cannot be parsed."""
21+
22+
1923
class Dependency(TypedDict):
2024
"""Argument types for dependency class construction."""
2125

@@ -91,7 +95,9 @@ def from_file(cls, path: str) -> "Metadata":
9195
try:
9296
data: Options = yaml.safe_load(metadata_file)["dfetch"]
9397
except yaml.YAMLError as exc:
94-
raise ValueError(str(exc)) from exc
98+
raise InvalidMetadataError(str(exc)) from exc
99+
except (KeyError, TypeError) as exc:
100+
raise InvalidMetadataError(str(exc)) from exc
95101

96102
return cls(data)
97103

dfetch/project/subproject.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from dfetch.manifest.project import ProjectEntry
1010
from dfetch.manifest.version import Version
1111
from dfetch.project.abstract_check_reporter import AbstractCheckReporter
12-
from dfetch.project.metadata import Dependency, Metadata
12+
from dfetch.project.metadata import Dependency, InvalidMetadataError, Metadata
1313
from dfetch.util.util import hash_directory, safe_rm
1414
from dfetch.util.versions import latest_tag_from_list
1515
from dfetch.vcs.patch import Patch
@@ -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, ValueError):
351+
except InvalidMetadataError:
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, ValueError):
370+
except InvalidMetadataError:
371371
logger.print_warning_line(
372372
self.__project.name,
373373
f"{pathlib.Path(self.__metadata.path).relative_to(os.getcwd()).as_posix()}"

dfetch/reporting/stdout_reporter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070

7171
from dfetch.log import get_logger
7272
from dfetch.manifest.project import ProjectEntry
73-
from dfetch.project.metadata import Metadata
73+
from dfetch.project.metadata import InvalidMetadataError, Metadata
7474
from dfetch.reporting.reporter import Reporter
7575
from dfetch.util.license import LicenseScanResult
7676

@@ -119,7 +119,7 @@ def add_project(
119119
)
120120
logger.info("")
121121

122-
except FileNotFoundError:
122+
except (FileNotFoundError, InvalidMetadataError):
123123
logger.print_info_field(" last fetch", "never")
124124

125125
def dump_to_file(self, outfile: str) -> bool:

features/handle-invalid-metadata.feature

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
@update
21
Feature: Handle invalid metadata files
32

43
*Dfetch* will keep metadata about the fetched project locally to prevent re-fetching unchanged projects
54
or replacing locally changed projects. Sometimes the metadata file will become incorrect and this should not lead
65
to unpredictable behavior in *DFetch*.
76
For instance, the metadata may be invalid
87

8+
@update
99
Scenario: Metadata with invalid YAML syntax is treated as invalid
1010
Given the manifest 'dfetch.yaml'
1111
"""
@@ -30,6 +30,31 @@ Feature: Handle invalid metadata files
3030
> Fetched v1
3131
"""
3232

33+
@check
34+
Scenario: Metadata with invalid YAML syntax is treated as invalid during check
35+
Given the manifest 'dfetch.yaml'
36+
"""
37+
manifest:
38+
version: '0.0'
39+
40+
projects:
41+
- name: ext/test-repo-tag
42+
url: https://github.com/dfetch-org/test-repo
43+
tag: v1
44+
45+
"""
46+
And all projects are updated
47+
And the metadata file ".dfetch_data.yaml" of "ext/test-repo-tag" has invalid yaml
48+
When I run "dfetch check"
49+
Then the output shows
50+
"""
51+
Dfetch (0.13.0)
52+
ext/test-repo-tag:
53+
> ext/test-repo-tag/.dfetch_data.yaml is an invalid metadata file, not checking on disk version!
54+
> wanted (v1), available (v2.0)
55+
"""
56+
57+
@update
3358
Scenario: Invalid metadata is ignored
3459
Given the manifest 'dfetch.yaml'
3560
"""

0 commit comments

Comments
 (0)