Merge lp:~james-w/linaro-image-tools/tarfile-matchers into lp:linaro-image-tools/11.11
- tarfile-matchers
- Merge into trunk
Status: | Merged |
---|---|
Approved by: | Michael Hudson-Doyle |
Approved revision: | 84 |
Merged at revision: | 55 |
Proposed branch: | lp:~james-w/linaro-image-tools/tarfile-matchers |
Merge into: | lp:linaro-image-tools/11.11 |
Prerequisite: | lp:~james-w/linaro-image-tools/tarfile-improvements |
Diff against target: |
423 lines (+398/-0) 4 files modified
hwpack/tarfile_matchers.py (+130/-0) hwpack/testing.py (+39/-0) hwpack/tests/__init__.py (+1/-0) hwpack/tests/test_tarfile_matchers.py (+228/-0) |
To merge this branch: | bzr merge lp:~james-w/linaro-image-tools/tarfile-matchers |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Michael Hudson-Doyle (community) | Approve | ||
Review via email:
|
Commit message
Description of the change
Hi,
This adds some matchers that I will use for testing the output of
hwpack-create. It's a lot easier to read and write the tests with this
style, rather than exercising the tarfile API directly.
I don't much like these tests though, suggestions for how to improve them
welcome.
Thanks,
James
- 77. By James Westby
-
Merged tarfile-
improvements into tarfile-matchers. - 78. By James Westby
-
Merged tarfile-
improvements into tarfile-matchers. - 79. By James Westby
-
Merged tarfile-
improvements into tarfile-matchers. - 80. By James Westby
-
Merged tarfile-
improvements into tarfile-matchers. - 81. By James Westby
-
Reduce duplication in the tests. Thanks Michael.
- 82. By James Westby
-
Add __eq__ and __hash__ on the mismatches.
- 83. By James Westby
-
Take advantage of the new __eq__ when checking TarfileWrongVal
ueMismatch. - 84. By James Westby
-
Also test the values set on TarfileMissingP
athMismatch.
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
James Westby (james-w) wrote : | # |
On Tue, 31 Aug 2010 02:44:41 -0000, Michael Hudson <email address hidden> wrote:
> Review: Approve
> It looks mostly fine. I agree the tests are a bit horrible.
>
> Could you fold this sort of thing:
>
> + backing_file = StringIO()
> + with writeable_
> + tf.create_
> + with standard_
>
> into some kind of context manager that would let you write:
>
> with test_tarfile(
>
> ? That would help a bit.
Done, thanks for the suggestion.
> As for the assertions, could you construct a mismatch using keyword
> arguments and then test that the mismatches match? I don't know if
> that would be better, but it seems worth a try.
I took a stab at this, let me know what you think now.
Thanks,
James
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
Michael Hudson-Doyle (mwhudson) wrote : | # |
I think the test_tarfile helper has improved things.
For the second of my suggestions, I actually meant constructing the mismatch in the test and then using assertEquals. But I don't know if that would be any better either (probably not).
I feel bad for causing you to write all those hash and eq tests!
- 85. By James Westby
-
Merged tarfile-
improvements into tarfile-matchers.
Preview Diff
1 | === added file 'hwpack/tarfile_matchers.py' | |||
2 | --- hwpack/tarfile_matchers.py 1970-01-01 00:00:00 +0000 | |||
3 | +++ hwpack/tarfile_matchers.py 2010-09-01 20:48:43 +0000 | |||
4 | @@ -0,0 +1,130 @@ | |||
5 | 1 | from testtools.matchers import Matcher, Mismatch | ||
6 | 2 | |||
7 | 3 | |||
8 | 4 | class TarfileMissingPathMismatch(Mismatch): | ||
9 | 5 | """A Mismatch indicating that a required path was missing from a tarfile. | ||
10 | 6 | """ | ||
11 | 7 | |||
12 | 8 | def __init__(self, tarball, path): | ||
13 | 9 | """Create a TarfileMissingPathMismatch Mismatch. | ||
14 | 10 | |||
15 | 11 | :param tarball: the tarfile that was checked. | ||
16 | 12 | :param path: the path that was expected to be present. | ||
17 | 13 | """ | ||
18 | 14 | self.tarball = tarball | ||
19 | 15 | self.path = path | ||
20 | 16 | |||
21 | 17 | def describe(self): | ||
22 | 18 | return '"%s" has no path "%s"' % (self.tarball, self.path) | ||
23 | 19 | |||
24 | 20 | def __eq__(self, other): | ||
25 | 21 | return self.tarball == other.tarball and self.path == other.path | ||
26 | 22 | |||
27 | 23 | def __hash__(self): | ||
28 | 24 | return hash((self.tarball, self.path)) | ||
29 | 25 | |||
30 | 26 | |||
31 | 27 | class TarfileWrongValueMismatch(Mismatch): | ||
32 | 28 | """A Mismatch indicating that an entry in a tarfile was not as expected. | ||
33 | 29 | """ | ||
34 | 30 | |||
35 | 31 | def __init__(self, attribute, tarball, path, expected, actual): | ||
36 | 32 | """Create a TarfileWrongValueMismatch Mismatch. | ||
37 | 33 | |||
38 | 34 | :param attribute: the attribute that was not as expected. | ||
39 | 35 | :type attribute: str | ||
40 | 36 | :param tarball: the tarfile that was checked. | ||
41 | 37 | :param path: the path that was checked. | ||
42 | 38 | :param expected: the expected value of the attribute. | ||
43 | 39 | :param actual: the value that was found. | ||
44 | 40 | """ | ||
45 | 41 | self.attribute = attribute | ||
46 | 42 | self.tarball = tarball | ||
47 | 43 | self.path = path | ||
48 | 44 | self.expected = expected | ||
49 | 45 | self.actual = actual | ||
50 | 46 | |||
51 | 47 | def describe(self): | ||
52 | 48 | return 'The path "%s" in "%s" has %s %s, expected %s' % ( | ||
53 | 49 | self.path, self.tarball, self.attribute, self.actual, | ||
54 | 50 | self.expected) | ||
55 | 51 | |||
56 | 52 | def __eq__(self, other): | ||
57 | 53 | return (self.attribute == other.attribute | ||
58 | 54 | and self.tarball == other.tarball | ||
59 | 55 | and self.path == other.path | ||
60 | 56 | and self.expected == other.expected | ||
61 | 57 | and self.actual == other.actual) | ||
62 | 58 | |||
63 | 59 | def __hash__(self): | ||
64 | 60 | return hash( | ||
65 | 61 | (self.attribute, self.tarball, self.path, self.expected, | ||
66 | 62 | self.actual)) | ||
67 | 63 | |||
68 | 64 | |||
69 | 65 | class TarfileHasFile(Matcher): | ||
70 | 66 | """Check that a tarfile has an entry with certain values.""" | ||
71 | 67 | |||
72 | 68 | def __init__(self, path, type=None, size=None, mtime=None, mode=None, | ||
73 | 69 | linkname=None, uid=None, gid=None, uname=None, gname=None, | ||
74 | 70 | content=None): | ||
75 | 71 | """Create a TarfileHasFile Matcher. | ||
76 | 72 | |||
77 | 73 | :param path: the path that must be present. | ||
78 | 74 | :type path: str | ||
79 | 75 | :param type: the type of TarInfo that must be at `path`, or None | ||
80 | 76 | to not check. | ||
81 | 77 | :param size: the size that the entry at `path` must have, or None | ||
82 | 78 | to not check. | ||
83 | 79 | :param mtime: the mtime that the entry at `path` must have, or None | ||
84 | 80 | to not check. | ||
85 | 81 | :param mode: the mode that the entry at `path` must have, or None | ||
86 | 82 | to not check. | ||
87 | 83 | :param linkname: the linkname that the entry at `path` must have, | ||
88 | 84 | or None to not check. | ||
89 | 85 | :param uid: the user id that the entry at `path` must have, or None | ||
90 | 86 | to not check. | ||
91 | 87 | :param gid: the group id that the entry at `path` must have, or None | ||
92 | 88 | to not check. | ||
93 | 89 | :param uname: the username that the entry at `path` must have, or | ||
94 | 90 | None to not check. | ||
95 | 91 | :param gname: the group name that the entry at `path` must have, or | ||
96 | 92 | None to not check. | ||
97 | 93 | :param content: the content that `path` must have when extracted, | ||
98 | 94 | or None to not check. | ||
99 | 95 | """ | ||
100 | 96 | self.path = path | ||
101 | 97 | self.type = type | ||
102 | 98 | self.size = size | ||
103 | 99 | self.mtime = mtime | ||
104 | 100 | self.mode = mode | ||
105 | 101 | self.linkname = linkname | ||
106 | 102 | self.uid = uid | ||
107 | 103 | self.gid = gid | ||
108 | 104 | self.uname = uname | ||
109 | 105 | self.gname = gname | ||
110 | 106 | self.content = content | ||
111 | 107 | |||
112 | 108 | def match(self, tarball): | ||
113 | 109 | """Match a tarfile.TarFile against the expected values.""" | ||
114 | 110 | if self.path not in tarball.getnames(): | ||
115 | 111 | return TarfileMissingPathMismatch(tarball, self.path) | ||
116 | 112 | info = tarball.getmember(self.path) | ||
117 | 113 | for attr in ( | ||
118 | 114 | "type", "size", "mtime", "mode", "linkname", "uid", "gid", | ||
119 | 115 | "uname", "gname"): | ||
120 | 116 | expected = getattr(self, attr, None) | ||
121 | 117 | if expected is not None: | ||
122 | 118 | actual = getattr(info, attr) | ||
123 | 119 | if expected != actual: | ||
124 | 120 | return TarfileWrongValueMismatch( | ||
125 | 121 | attr, tarball, self.path, expected, actual) | ||
126 | 122 | if self.content is not None: | ||
127 | 123 | actual = tarball.extractfile(self.path).read() | ||
128 | 124 | if actual != self.content: | ||
129 | 125 | return TarfileWrongValueMismatch( | ||
130 | 126 | "content", tarball, self.path, self.content, actual) | ||
131 | 127 | return None | ||
132 | 128 | |||
133 | 129 | def __str__(self): | ||
134 | 130 | return 'tarfile has file "%s"' % (self.path, ) | ||
135 | 0 | 131 | ||
136 | === added file 'hwpack/testing.py' | |||
137 | --- hwpack/testing.py 1970-01-01 00:00:00 +0000 | |||
138 | +++ hwpack/testing.py 2010-09-01 20:48:43 +0000 | |||
139 | @@ -0,0 +1,39 @@ | |||
140 | 1 | from contextlib import contextmanager | ||
141 | 2 | from StringIO import StringIO | ||
142 | 3 | import tarfile | ||
143 | 4 | |||
144 | 5 | from hwpack.better_tarfile import writeable_tarfile | ||
145 | 6 | |||
146 | 7 | |||
147 | 8 | @contextmanager | ||
148 | 9 | def test_tarfile(contents=[], **kwargs): | ||
149 | 10 | """Create a tarfile with the given contents, then re-open it for reading. | ||
150 | 11 | |||
151 | 12 | This context manager creates a tarfile with the given contents, then | ||
152 | 13 | reopens it for reading and yields it for use in a with block. When | ||
153 | 14 | the block ends the tarfile will be closed. | ||
154 | 15 | |||
155 | 16 | The contents can be specified as a list of tuples of (path, contents), | ||
156 | 17 | where if the path ends with '/' it is considered to be a directory and | ||
157 | 18 | the contents ignored. | ||
158 | 19 | |||
159 | 20 | :param contents: the contents to put in the tarball, defaults to the | ||
160 | 21 | empty list. | ||
161 | 22 | :type contents: a list of tuples of (str, str) | ||
162 | 23 | :param kwargs: keyword arguments for the better_tarfile.TarFile | ||
163 | 24 | constructor. | ||
164 | 25 | """ | ||
165 | 26 | backing_file = StringIO() | ||
166 | 27 | with writeable_tarfile(backing_file, **kwargs) as tf: | ||
167 | 28 | for path, content in contents: | ||
168 | 29 | if path[-1] == "/": | ||
169 | 30 | tf.create_dir(path) | ||
170 | 31 | else: | ||
171 | 32 | tf.create_file_from_string(path, content) | ||
172 | 33 | if contents: | ||
173 | 34 | backing_file.seek(0) | ||
174 | 35 | tf = tarfile.TarFile.open(mode="r", fileobj=backing_file) | ||
175 | 36 | try: | ||
176 | 37 | yield tf | ||
177 | 38 | finally: | ||
178 | 39 | tf.close() | ||
179 | 0 | 40 | ||
180 | === modified file 'hwpack/tests/__init__.py' | |||
181 | --- hwpack/tests/__init__.py 2010-09-01 20:48:43 +0000 | |||
182 | +++ hwpack/tests/__init__.py 2010-09-01 20:48:43 +0000 | |||
183 | @@ -3,6 +3,7 @@ | |||
184 | 3 | def test_suite(): | 3 | def test_suite(): |
185 | 4 | module_names = ['hwpack.tests.test_config', | 4 | module_names = ['hwpack.tests.test_config', |
186 | 5 | 'hwpack.tests.test_better_tarfile', | 5 | 'hwpack.tests.test_better_tarfile', |
187 | 6 | 'hwpack.tests.test_tarfile_matchers', | ||
188 | 6 | ] | 7 | ] |
189 | 7 | loader = unittest.TestLoader() | 8 | loader = unittest.TestLoader() |
190 | 8 | suite = loader.loadTestsFromNames(module_names) | 9 | suite = loader.loadTestsFromNames(module_names) |
191 | 9 | 10 | ||
192 | === added file 'hwpack/tests/test_tarfile_matchers.py' | |||
193 | --- hwpack/tests/test_tarfile_matchers.py 1970-01-01 00:00:00 +0000 | |||
194 | +++ hwpack/tests/test_tarfile_matchers.py 2010-09-01 20:48:43 +0000 | |||
195 | @@ -0,0 +1,228 @@ | |||
196 | 1 | from StringIO import StringIO | ||
197 | 2 | import tarfile | ||
198 | 3 | |||
199 | 4 | from testtools import TestCase | ||
200 | 5 | |||
201 | 6 | from hwpack.tarfile_matchers import ( | ||
202 | 7 | TarfileHasFile, | ||
203 | 8 | TarfileMissingPathMismatch, | ||
204 | 9 | TarfileWrongValueMismatch, | ||
205 | 10 | ) | ||
206 | 11 | from hwpack.testing import test_tarfile | ||
207 | 12 | |||
208 | 13 | |||
209 | 14 | class TarfileMissingPathMismatchTests(TestCase): | ||
210 | 15 | |||
211 | 16 | def test_describe(self): | ||
212 | 17 | mismatch = TarfileMissingPathMismatch("foo", "bar") | ||
213 | 18 | self.assertEqual('"foo" has no path "bar"', mismatch.describe()) | ||
214 | 19 | |||
215 | 20 | def test_eq(self): | ||
216 | 21 | mismatch1 = TarfileMissingPathMismatch("foo", "bar") | ||
217 | 22 | mismatch2 = TarfileMissingPathMismatch("foo", "bar") | ||
218 | 23 | self.assertEqual(mismatch1, mismatch2) | ||
219 | 24 | |||
220 | 25 | def test_no_eq_different_tarball(self): | ||
221 | 26 | mismatch1 = TarfileMissingPathMismatch("foo", "bar") | ||
222 | 27 | mismatch2 = TarfileMissingPathMismatch("baz", "bar") | ||
223 | 28 | self.assertNotEqual(mismatch1, mismatch2) | ||
224 | 29 | |||
225 | 30 | def test_no_eq_different_path(self): | ||
226 | 31 | mismatch1 = TarfileMissingPathMismatch("foo", "bar") | ||
227 | 32 | mismatch2 = TarfileMissingPathMismatch("foo", "baz") | ||
228 | 33 | self.assertNotEqual(mismatch1, mismatch2) | ||
229 | 34 | |||
230 | 35 | def test_hash_equal(self): | ||
231 | 36 | mismatch1 = TarfileMissingPathMismatch("foo", "bar") | ||
232 | 37 | mismatch2 = TarfileMissingPathMismatch("foo", "bar") | ||
233 | 38 | self.assertEqual(hash(mismatch1), hash(mismatch2)) | ||
234 | 39 | |||
235 | 40 | def test_different_tarball_different_hash(self): | ||
236 | 41 | mismatch1 = TarfileMissingPathMismatch("foo", "bar") | ||
237 | 42 | mismatch2 = TarfileMissingPathMismatch("baz", "bar") | ||
238 | 43 | self.assertNotEqual(hash(mismatch1), hash(mismatch2)) | ||
239 | 44 | |||
240 | 45 | def test_different_path_different_hash(self): | ||
241 | 46 | mismatch1 = TarfileMissingPathMismatch("foo", "bar") | ||
242 | 47 | mismatch2 = TarfileMissingPathMismatch("foo", "baz") | ||
243 | 48 | self.assertNotEqual(hash(mismatch1), hash(mismatch2)) | ||
244 | 49 | |||
245 | 50 | |||
246 | 51 | class TarfileWrongTypeMismatchTests(TestCase): | ||
247 | 52 | |||
248 | 53 | def test_describe(self): | ||
249 | 54 | mismatch = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
250 | 55 | self.assertEqual( | ||
251 | 56 | 'The path "bar" in "foo" has type 2, expected 1', | ||
252 | 57 | mismatch.describe()) | ||
253 | 58 | |||
254 | 59 | def test_eq(self): | ||
255 | 60 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
256 | 61 | mismatch2 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
257 | 62 | self.assertEqual(mismatch1, mismatch2) | ||
258 | 63 | |||
259 | 64 | def test_not_eq_different_attribute(self): | ||
260 | 65 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
261 | 66 | mismatch2 = TarfileWrongValueMismatch("size", "foo", "bar", 1, 2) | ||
262 | 67 | self.assertNotEqual(mismatch1, mismatch2) | ||
263 | 68 | |||
264 | 69 | def test_not_eq_different_tarball(self): | ||
265 | 70 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
266 | 71 | mismatch2 = TarfileWrongValueMismatch("type", "baz", "bar", 1, 2) | ||
267 | 72 | self.assertNotEqual(mismatch1, mismatch2) | ||
268 | 73 | |||
269 | 74 | def test_not_eq_different_path(self): | ||
270 | 75 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
271 | 76 | mismatch2 = TarfileWrongValueMismatch("type", "foo", "baz", 1, 2) | ||
272 | 77 | self.assertNotEqual(mismatch1, mismatch2) | ||
273 | 78 | |||
274 | 79 | def test_not_eq_different_expected(self): | ||
275 | 80 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
276 | 81 | mismatch2 = TarfileWrongValueMismatch("type", "foo", "bar", 3, 2) | ||
277 | 82 | self.assertNotEqual(mismatch1, mismatch2) | ||
278 | 83 | |||
279 | 84 | def test_not_eq_different_actual(self): | ||
280 | 85 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
281 | 86 | mismatch2 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 3) | ||
282 | 87 | self.assertNotEqual(mismatch1, mismatch2) | ||
283 | 88 | |||
284 | 89 | def test_hash_equal(self): | ||
285 | 90 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
286 | 91 | mismatch2 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
287 | 92 | self.assertEqual(hash(mismatch1), hash(mismatch2)) | ||
288 | 93 | |||
289 | 94 | def test_different_attribute_different_hash(self): | ||
290 | 95 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
291 | 96 | mismatch2 = TarfileWrongValueMismatch("size", "foo", "bar", 1, 2) | ||
292 | 97 | self.assertNotEqual(hash(mismatch1), hash(mismatch2)) | ||
293 | 98 | |||
294 | 99 | def test_different_tarball_different_hash(self): | ||
295 | 100 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
296 | 101 | mismatch2 = TarfileWrongValueMismatch("type", "baz", "bar", 1, 2) | ||
297 | 102 | self.assertNotEqual(hash(mismatch1), hash(mismatch2)) | ||
298 | 103 | |||
299 | 104 | def test_different_path_different_hash(self): | ||
300 | 105 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
301 | 106 | mismatch2 = TarfileWrongValueMismatch("type", "foo", "baz", 1, 2) | ||
302 | 107 | self.assertNotEqual(hash(mismatch1), hash(mismatch2)) | ||
303 | 108 | |||
304 | 109 | def test_different_expected_different_hash(self): | ||
305 | 110 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
306 | 111 | mismatch2 = TarfileWrongValueMismatch("type", "foo", "bar", 3, 2) | ||
307 | 112 | self.assertNotEqual(hash(mismatch1), hash(mismatch2)) | ||
308 | 113 | |||
309 | 114 | def test_different_actual_different_hash(self): | ||
310 | 115 | mismatch1 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 2) | ||
311 | 116 | mismatch2 = TarfileWrongValueMismatch("type", "foo", "bar", 1, 3) | ||
312 | 117 | self.assertNotEqual(hash(mismatch1), hash(mismatch2)) | ||
313 | 118 | |||
314 | 119 | |||
315 | 120 | class TarfileHasFileTests(TestCase): | ||
316 | 121 | |||
317 | 122 | def test_str(self): | ||
318 | 123 | matcher = TarfileHasFile("foo") | ||
319 | 124 | self.assertEqual('tarfile has file "foo"', str(matcher)) | ||
320 | 125 | |||
321 | 126 | def test_matches(self): | ||
322 | 127 | backing_file = StringIO() | ||
323 | 128 | with test_tarfile(contents=[("foo", "")]) as tf: | ||
324 | 129 | matcher = TarfileHasFile("foo") | ||
325 | 130 | self.assertIs(None, matcher.match(tf)) | ||
326 | 131 | |||
327 | 132 | def test_mismatches_missing_path(self): | ||
328 | 133 | backing_file = StringIO() | ||
329 | 134 | with test_tarfile() as tf: | ||
330 | 135 | matcher = TarfileHasFile("foo") | ||
331 | 136 | mismatch = matcher.match(tf) | ||
332 | 137 | self.assertIsInstance(mismatch, TarfileMissingPathMismatch) | ||
333 | 138 | self.assertEqual(TarfileMissingPathMismatch(tf, "foo"), mismatch) | ||
334 | 139 | |||
335 | 140 | def assertValueMismatch(self, mismatch, tarball, path, attribute, | ||
336 | 141 | expected, actual): | ||
337 | 142 | self.assertIsInstance(mismatch, TarfileWrongValueMismatch) | ||
338 | 143 | expected_mismatch = TarfileWrongValueMismatch( | ||
339 | 144 | attribute, tarball, path, expected, actual) | ||
340 | 145 | self.assertEqual(expected_mismatch, mismatch) | ||
341 | 146 | |||
342 | 147 | def test_mismatches_wrong_type(self): | ||
343 | 148 | backing_file = StringIO() | ||
344 | 149 | with test_tarfile(contents=[("foo", "")]) as tf: | ||
345 | 150 | matcher = TarfileHasFile("foo", type=tarfile.DIRTYPE) | ||
346 | 151 | mismatch = matcher.match(tf) | ||
347 | 152 | self.assertValueMismatch( | ||
348 | 153 | mismatch, tf, "foo", "type", tarfile.DIRTYPE, | ||
349 | 154 | tarfile.REGTYPE) | ||
350 | 155 | |||
351 | 156 | def test_mismatches_wrong_size(self): | ||
352 | 157 | backing_file = StringIO() | ||
353 | 158 | with test_tarfile(contents=[("foo", "")]) as tf: | ||
354 | 159 | matcher = TarfileHasFile("foo", size=1235) | ||
355 | 160 | mismatch = matcher.match(tf) | ||
356 | 161 | self.assertValueMismatch( | ||
357 | 162 | mismatch, tf, "foo", "size", 1235, 0) | ||
358 | 163 | |||
359 | 164 | def test_mismatches_wrong_mtime(self): | ||
360 | 165 | backing_file = StringIO() | ||
361 | 166 | with test_tarfile(contents=[("foo", "")], default_mtime=12345) as tf: | ||
362 | 167 | matcher = TarfileHasFile("foo", mtime=54321) | ||
363 | 168 | mismatch = matcher.match(tf) | ||
364 | 169 | self.assertValueMismatch( | ||
365 | 170 | mismatch, tf, "foo", "mtime", 54321, 12345) | ||
366 | 171 | |||
367 | 172 | def test_mismatches_wrong_mode(self): | ||
368 | 173 | backing_file = StringIO() | ||
369 | 174 | with test_tarfile(contents=[("foo", "")]) as tf: | ||
370 | 175 | matcher = TarfileHasFile("foo", mode=0000) | ||
371 | 176 | mismatch = matcher.match(tf) | ||
372 | 177 | self.assertValueMismatch( | ||
373 | 178 | mismatch, tf, "foo", "mode", 0000, 0644) | ||
374 | 179 | |||
375 | 180 | def test_mismatches_wrong_linkname(self): | ||
376 | 181 | backing_file = StringIO() | ||
377 | 182 | with test_tarfile(contents=[("foo", "")]) as tf: | ||
378 | 183 | matcher = TarfileHasFile("foo", linkname="somelink") | ||
379 | 184 | mismatch = matcher.match(tf) | ||
380 | 185 | self.assertValueMismatch( | ||
381 | 186 | mismatch, tf, "foo", "linkname", "somelink", "") | ||
382 | 187 | |||
383 | 188 | def test_mismatches_wrong_uid(self): | ||
384 | 189 | backing_file = StringIO() | ||
385 | 190 | with test_tarfile(contents=[("foo", "")], default_uid=100) as tf: | ||
386 | 191 | matcher = TarfileHasFile("foo", uid=99) | ||
387 | 192 | mismatch = matcher.match(tf) | ||
388 | 193 | self.assertValueMismatch( | ||
389 | 194 | mismatch, tf, "foo", "uid", 99, 100) | ||
390 | 195 | |||
391 | 196 | def test_mismatches_wrong_gid(self): | ||
392 | 197 | backing_file = StringIO() | ||
393 | 198 | with test_tarfile(contents=[("foo", "")], default_gid=100) as tf: | ||
394 | 199 | matcher = TarfileHasFile("foo", gid=99) | ||
395 | 200 | mismatch = matcher.match(tf) | ||
396 | 201 | self.assertValueMismatch( | ||
397 | 202 | mismatch, tf, "foo", "gid", 99, 100) | ||
398 | 203 | |||
399 | 204 | def test_mismatches_wrong_uname(self): | ||
400 | 205 | backing_file = StringIO() | ||
401 | 206 | with test_tarfile( | ||
402 | 207 | contents=[("foo", "")], default_uname="someuser") as tf: | ||
403 | 208 | matcher = TarfileHasFile("foo", uname="otheruser") | ||
404 | 209 | mismatch = matcher.match(tf) | ||
405 | 210 | self.assertValueMismatch( | ||
406 | 211 | mismatch, tf, "foo", "uname", "otheruser", "someuser") | ||
407 | 212 | |||
408 | 213 | def test_mismatches_wrong_gname(self): | ||
409 | 214 | backing_file = StringIO() | ||
410 | 215 | with test_tarfile( | ||
411 | 216 | contents=[("foo", "")], default_gname="somegroup") as tf: | ||
412 | 217 | matcher = TarfileHasFile("foo", gname="othergroup") | ||
413 | 218 | mismatch = matcher.match(tf) | ||
414 | 219 | self.assertValueMismatch( | ||
415 | 220 | mismatch, tf, "foo", "gname", "othergroup", "somegroup") | ||
416 | 221 | |||
417 | 222 | def test_mismatches_wrong_content(self): | ||
418 | 223 | backing_file = StringIO() | ||
419 | 224 | with test_tarfile(contents=[("foo", "somecontent")]) as tf: | ||
420 | 225 | matcher = TarfileHasFile("foo", content="othercontent") | ||
421 | 226 | mismatch = matcher.match(tf) | ||
422 | 227 | self.assertValueMismatch( | ||
423 | 228 | mismatch, tf, "foo", "content", "othercontent", "somecontent") |
It looks mostly fine. I agree the tests are a bit horrible.
Could you fold this sort of thing:
+ backing_file = StringIO() tarfile( backing_ file, default_ mtime=12345) as tf: file_from_ string( "foo", "") tarfile( backing_ file) as tf:
+ with writeable_
+ tf.create_
+ with standard_
into some kind of context manager that would let you write:
with test_tarfile( default_ mtime=12345, contents=[('foo', '')]) as tf:
? That would help a bit.
As for the assertions, could you construct a mismatch using keyword arguments and then test that the mismatches match? I don't know if that would be better, but it seems worth a try.