Skip to content

Commit f22f357

Browse files
committed
Metadata API: Remove 3 'update' methods + tests
Remove ambiguous, unspecific, opinionated and trivial 'update' methods, which can be replaced by feasible one-liners that assign values directly to the object attribute to be *updated*. (see #1627 for details). Reasons to have these methods would be increased usability in terms of - reduced work - immediate feedback on invalid assignments However, given above described issues, the reasons against the methods as they are now seem to outweigh the reasons for them. Furthermore, it seems easier to re-add similar methods, which addressed these issues, after the upcoming 1.0.0 release than to remove or modify them. This patch also removes the corresponding tests as they become irrelevant (there is no need to test object assignment). In the case of the timestamp test, the removal also includes redundant test logic, which is already tested in `test_metadata_base`. Signed-off-by: Lukas Puehringer <[email protected]>
1 parent 4f8d494 commit f22f357

File tree

2 files changed

+0
-91
lines changed

2 files changed

+0
-91
lines changed

tests/test_api.py

-75
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
Delegations,
3333
Key,
3434
Metadata,
35-
MetaFile,
3635
Root,
3736
Snapshot,
3837
TargetFile,
@@ -297,56 +296,6 @@ def test_metadata_base(self) -> None:
297296
with self.assertRaises(ValueError):
298297
Metadata.from_dict(data)
299298

300-
def test_metadata_snapshot(self) -> None:
301-
snapshot_path = os.path.join(self.repo_dir, "metadata", "snapshot.json")
302-
snapshot = Metadata[Snapshot].from_file(snapshot_path)
303-
304-
# Create a MetaFile instance representing what we expect
305-
# the updated data to be.
306-
hashes = {
307-
"sha256": "c2986576f5fdfd43944e2b19e775453b96748ec4fe2638a6d2f32f1310967095"
308-
}
309-
fileinfo = MetaFile(2, 123, hashes)
310-
311-
self.assertNotEqual(
312-
snapshot.signed.meta["role1.json"].to_dict(), fileinfo.to_dict()
313-
)
314-
snapshot.signed.update("role1", fileinfo)
315-
self.assertEqual(
316-
snapshot.signed.meta["role1.json"].to_dict(), fileinfo.to_dict()
317-
)
318-
319-
def test_metadata_timestamp(self) -> None:
320-
timestamp_path = os.path.join(
321-
self.repo_dir, "metadata", "timestamp.json"
322-
)
323-
timestamp = Metadata[Timestamp].from_file(timestamp_path)
324-
325-
self.assertEqual(timestamp.signed.version, 1)
326-
timestamp.signed.bump_version()
327-
self.assertEqual(timestamp.signed.version, 2)
328-
329-
self.assertEqual(timestamp.signed.expires, datetime(2030, 1, 1, 0, 0))
330-
timestamp.signed.bump_expiration()
331-
self.assertEqual(timestamp.signed.expires, datetime(2030, 1, 2, 0, 0))
332-
timestamp.signed.bump_expiration(timedelta(days=365))
333-
self.assertEqual(timestamp.signed.expires, datetime(2031, 1, 2, 0, 0))
334-
335-
# Create a MetaFile instance representing what we expect
336-
# the updated data to be.
337-
hashes = {
338-
"sha256": "0ae9664468150a9aa1e7f11feecb32341658eb84292851367fea2da88e8a58dc"
339-
}
340-
fileinfo = MetaFile(2, 520, hashes)
341-
342-
self.assertNotEqual(
343-
timestamp.signed.snapshot_meta.to_dict(), fileinfo.to_dict()
344-
)
345-
timestamp.signed.update(fileinfo)
346-
self.assertEqual(
347-
timestamp.signed.snapshot_meta.to_dict(), fileinfo.to_dict()
348-
)
349-
350299
def test_metadata_verify_delegate(self) -> None:
351300
root_path = os.path.join(self.repo_dir, "metadata", "root.json")
352301
root = Metadata[Root].from_file(root_path)
@@ -505,30 +454,6 @@ def test_is_target_in_pathpattern(self) -> None:
505454
DelegatedRole._is_target_in_pathpattern(targetpath, pathpattern)
506455
)
507456

508-
def test_metadata_targets(self) -> None:
509-
targets_path = os.path.join(self.repo_dir, "metadata", "targets.json")
510-
targets = Metadata[Targets].from_file(targets_path)
511-
512-
# Create a fileinfo dict representing the expected updated data.
513-
filename = "file2.txt"
514-
hashes = {
515-
"sha256": "141f740f53781d1ca54b8a50af22cbf74e44c21a998fa2a8a05aaac2c002886b",
516-
"sha512": "ef5beafa16041bcdd2937140afebd485296cd54f7348ecd5a4d035c09759608de467a7ac0eb58753d0242df873c305e8bffad2454aa48f44480f15efae1cacd0",
517-
}
518-
519-
fileinfo = TargetFile(length=28, hashes=hashes, path=filename)
520-
521-
# Assert that data is not aleady equal
522-
self.assertNotEqual(
523-
targets.signed.targets[filename].to_dict(), fileinfo.to_dict()
524-
)
525-
# Update an already existing fileinfo
526-
targets.signed.update(fileinfo)
527-
# Verify that data is updated
528-
self.assertEqual(
529-
targets.signed.targets[filename].to_dict(), fileinfo.to_dict()
530-
)
531-
532457
def test_targets_key_api(self) -> None:
533458
targets_path = os.path.join(self.repo_dir, "metadata", "targets.json")
534459
targets: Targets = Metadata[Targets].from_file(targets_path).signed

tuf/api/metadata.py

-16
Original file line numberDiff line numberDiff line change
@@ -993,11 +993,6 @@ def to_dict(self) -> Dict[str, Any]:
993993
res_dict["meta"] = {"snapshot.json": self.snapshot_meta.to_dict()}
994994
return res_dict
995995

996-
# Modification.
997-
def update(self, snapshot_meta: MetaFile) -> None:
998-
"""Assigns passed info about snapshot metadata."""
999-
self.snapshot_meta = snapshot_meta
1000-
1001996

1002997
class Snapshot(Signed):
1003998
"""A container for the signed part of snapshot metadata.
@@ -1049,12 +1044,6 @@ def to_dict(self) -> Dict[str, Any]:
10491044
snapshot_dict["meta"] = meta_dict
10501045
return snapshot_dict
10511046

1052-
# Modification.
1053-
def update(self, rolename: str, role_info: MetaFile) -> None:
1054-
"""Assigns passed (delegated) targets role info to meta dict."""
1055-
metadata_fn = f"{rolename}.json"
1056-
self.meta[metadata_fn] = role_info
1057-
10581047

10591048
class DelegatedRole(Role):
10601049
"""A container with information about a delegated role.
@@ -1463,11 +1452,6 @@ def to_dict(self) -> Dict[str, Any]:
14631452
targets_dict["delegations"] = self.delegations.to_dict()
14641453
return targets_dict
14651454

1466-
# Modification.
1467-
def update(self, fileinfo: TargetFile) -> None:
1468-
"""Assigns passed target file info to meta dict."""
1469-
self.targets[fileinfo.path] = fileinfo
1470-
14711455
def add_key(self, role: str, key: Key) -> None:
14721456
"""Adds new signing key for delegated role 'role'.
14731457

0 commit comments

Comments
 (0)