Assert that tarfile contains exactly the entry described by `content`. Args: tar: the path to the TAR file to test. content: an array describing the expected content of the TAR file. Each entry in that list should be a dictionary where each field is a f
(self, tar, content)
| 26 | """Testing for TarFileWriter class.""" |
| 27 | |
| 28 | def assertTarFileContent(self, tar, content): |
| 29 | """Assert that tarfile contains exactly the entry described by `content`. |
| 30 | |
| 31 | Args: |
| 32 | tar: the path to the TAR file to test. |
| 33 | content: an array describing the expected content of the TAR file. Each |
| 34 | entry in that list should be a dictionary where each field is a |
| 35 | field to test in the corresponding TarInfo. For testing the |
| 36 | presence of a file "x", then the entry could simply be |
| 37 | `{"name": "x"}`, the missing field will be ignored. To match the |
| 38 | content of a file entry, use the key "data". |
| 39 | """ |
| 40 | with tarfile.open(tar, "r:") as f: |
| 41 | i = 0 |
| 42 | for current in f: |
| 43 | error_msg = "Extraneous file at end of archive %s: %s" % (tar, |
| 44 | current.name) |
| 45 | self.assertLess(i, len(content), error_msg) |
| 46 | for k, v in content[i].items(): |
| 47 | if k == "data": |
| 48 | value = f.extractfile(current).read() |
| 49 | else: |
| 50 | value = getattr(current, k) |
| 51 | error_msg = " ".join([ |
| 52 | "Value `%s` for key `%s` of file" % (value, k), |
| 53 | "%s in archive %s does" % (current.name, tar), |
| 54 | "not match expected value `%s`" % v |
| 55 | ]) |
| 56 | self.assertEqual(value, v, error_msg) |
| 57 | i += 1 |
| 58 | if i < len(content): |
| 59 | self.fail("Missing file %s in archive %s" % (content[i], tar)) |
| 60 | |
| 61 | def setUp(self): |
| 62 | super(TarFileWriterTest, self).setUp() |
no test coverage detected