|
10 | 10 | import os
|
11 | 11 | import sys
|
12 | 12 | import tempfile
|
13 |
| -from typing import Optional |
| 13 | +from typing import Dict, Callable, Optional, Tuple |
14 | 14 | from tuf.exceptions import UnsignedMetadataError
|
15 | 15 | import unittest
|
16 | 16 |
|
|
19 | 19 | from tests import utils
|
20 | 20 | from tests.repository_simulator import RepositorySimulator
|
21 | 21 |
|
| 22 | +# DataSet is only here so type hints can be used: |
| 23 | +# It is a dict of name to test dict |
| 24 | +DataSet = Dict[str, str] |
| 25 | + |
| 26 | +# Test runner decorator: Runs the test as a set of N SubTests, |
| 27 | +# (where N is number of items in dataset), feeding the actual test |
| 28 | +# function one test case at a time |
| 29 | +def run_sub_tests_with_dataset(dataset: DataSet): |
| 30 | + def real_decorator(function: Callable[["TestUpdater", str], None]): |
| 31 | + def wrapper(test_cls: "TestUpdater"): |
| 32 | + for case, data in dataset.items(): |
| 33 | + with test_cls.subTest(case=case): |
| 34 | + function(test_cls, data) |
| 35 | + return wrapper |
| 36 | + return real_decorator |
| 37 | + |
| 38 | + |
22 | 39 | class TestUpdater(unittest.TestCase):
|
23 | 40 | # set dump_dir to trigger repository state dumps
|
24 | 41 | dump_dir:Optional[str] = None
|
@@ -79,43 +96,47 @@ def test_refresh(self):
|
79 | 96 |
|
80 | 97 | self._run_refresh()
|
81 | 98 |
|
82 |
| - def test_targets(self): |
83 |
| - targets = { |
84 |
| - "targetpath": b"content", |
85 |
| - "åäö": b"more content", |
86 |
| - "dir/targetpath": b"dir target content" |
87 |
| - } |
| 99 | + targets: DataSet = { |
| 100 | + "standard case": ("targetpath", b"content"), |
| 101 | + "non-asci case": ("åäö", b"more content"), |
| 102 | + "subdirectory case": ("dir/targetpath", b"dir target content"), |
| 103 | + } |
| 104 | + |
| 105 | + @run_sub_tests_with_dataset(targets) |
| 106 | + def test_targets(self, test_case_data: Tuple[str, str]): |
| 107 | + targetpath, content = test_case_data |
| 108 | + # target does not exist yet |
| 109 | + updater = self._run_refresh() |
| 110 | + self.assertIsNone(updater.get_one_valid_targetinfo(targetpath)) |
88 | 111 |
|
89 | 112 | # Add targets to repository
|
90 | 113 | self.sim.targets.version += 1
|
91 |
| - for targetpath, content in targets.items(): |
92 |
| - self.sim.add_target("targets", content, targetpath) |
| 114 | + self.sim.add_target("targets", content, targetpath) |
93 | 115 | self.sim.update_snapshot()
|
94 | 116 |
|
95 | 117 | updater = self._run_refresh()
|
96 |
| - for targetpath, content in targets.items(): |
97 |
| - # target now exists, is not in cache yet |
98 |
| - file_info = updater.get_one_valid_targetinfo(targetpath) |
99 |
| - self.assertIsNotNone(file_info) |
100 |
| - self.assertEqual( |
101 |
| - updater.updated_targets([file_info], self.targets_dir), |
102 |
| - [file_info] |
103 |
| - ) |
104 |
| - |
105 |
| - # download target, assert it is in cache and content is correct |
106 |
| - local_path = updater.download_target(file_info, self.targets_dir) |
107 |
| - self.assertEqual( |
108 |
| - updater.updated_targets([file_info], self.targets_dir), [] |
109 |
| - ) |
110 |
| - self.assertTrue(local_path.startswith(self.targets_dir)) |
111 |
| - with open(local_path, "rb") as f: |
112 |
| - self.assertEqual(f.read(), content) |
113 |
| - |
114 |
| - if "/" in targetpath: |
115 |
| - # assert local_path != targetpath because of the URL encoding |
116 |
| - # make target_path absolute as local_path |
117 |
| - target_path = os.path.join(self.targets_dir, targetpath) |
118 |
| - self.assertNotEqual(target_path, local_path) |
| 118 | + # target now exists, is not in cache yet |
| 119 | + file_info = updater.get_one_valid_targetinfo(targetpath) |
| 120 | + self.assertIsNotNone(file_info) |
| 121 | + self.assertEqual( |
| 122 | + updater.updated_targets([file_info], self.targets_dir), |
| 123 | + [file_info] |
| 124 | + ) |
| 125 | + |
| 126 | + # download target, assert it is in cache and content is correct |
| 127 | + local_path = updater.download_target(file_info, self.targets_dir) |
| 128 | + self.assertEqual( |
| 129 | + updater.updated_targets([file_info], self.targets_dir), [] |
| 130 | + ) |
| 131 | + self.assertTrue(local_path.startswith(self.targets_dir)) |
| 132 | + with open(local_path, "rb") as f: |
| 133 | + self.assertEqual(f.read(), content) |
| 134 | + |
| 135 | + if "/" in targetpath: |
| 136 | + # assert local_path != targetpath because of the URL encoding |
| 137 | + # make target_path absolute as local_path |
| 138 | + target_path = os.path.join(self.targets_dir, targetpath) |
| 139 | + self.assertNotEqual(target_path, local_path) |
119 | 140 |
|
120 | 141 |
|
121 | 142 |
|
|
0 commit comments