diff options
author | Michael Hudson <michael.hudson@linaro.org> | 2010-12-16 11:41:36 +1300 |
---|---|---|
committer | Michael Hudson <michael.hudson@linaro.org> | 2010-12-16 11:41:36 +1300 |
commit | 112d5fd26baadd01aaed85d3cb07b22f6fa8d2fc (patch) | |
tree | 995a29d557fa8cc2a2f11224c5a5880c7574a0f4 /hwpack | |
parent | 11fe1a12c0fc813acb064b49330a4db1a77dab29 (diff) |
allow matching the "content" (i.e. the file listing) of directories in tarfiles
Diffstat (limited to 'hwpack')
-rw-r--r-- | hwpack/tarfile_matchers.py | 22 | ||||
-rw-r--r-- | hwpack/tests/test_tarfile_matchers.py | 21 |
2 files changed, 39 insertions, 4 deletions
diff --git a/hwpack/tarfile_matchers.py b/hwpack/tarfile_matchers.py index 919efd5..ae81661 100644 --- a/hwpack/tarfile_matchers.py +++ b/hwpack/tarfile_matchers.py @@ -1,3 +1,5 @@ +import tarfile + from testtools.matchers import Annotate, Equals, Matcher, Mismatch @@ -141,10 +143,22 @@ class TarfileHasFile(Matcher): return TarfileWrongValueMismatch( "mtime", tarball, self.path, self.mtime, info.mtime) if self.content_matcher is not None: - actual = tarball.extractfile(self.path).read() - content_mismatch = self.content_matcher.match(actual) - if content_mismatch: - return content_mismatch + if info.type == tarfile.DIRTYPE: + contents = [] + path_frags = self.path.split('/') + for name in tarball.getnames(): + name_frags = name.split('/') + if (len(name_frags) == len(path_frags) + 1 and + name_frags[:-1] == path_frags): + contents.append(name_frags[-1]) + content_mismatch = self.content_matcher.match(contents) + if content_mismatch: + return content_mismatch + else: + actual = tarball.extractfile(self.path).read() + content_mismatch = self.content_matcher.match(actual) + if content_mismatch: + return content_mismatch return None def __str__(self): diff --git a/hwpack/tests/test_tarfile_matchers.py b/hwpack/tests/test_tarfile_matchers.py index aa4f3bc..a92de3d 100644 --- a/hwpack/tests/test_tarfile_matchers.py +++ b/hwpack/tests/test_tarfile_matchers.py @@ -84,6 +84,17 @@ class TarfileHasFileTests(TestCase): matcher = TarfileHasFile("foo") self.assertIs(None, matcher.match(tf)) + def test_matches_directory(self): + with test_tarfile(contents=[("foo/", "")]) as tf: + matcher = TarfileHasFile("foo", type=tarfile.DIRTYPE) + self.assertIs(None, matcher.match(tf)) + + def test_matches_directory_content(self): + with test_tarfile(contents=[("foo/", ""), ("foo/bar", "")]) as tf: + matcher = TarfileHasFile( + "foo", type=tarfile.DIRTYPE, content=["bar"]) + self.assertIs(None, matcher.match(tf)) + def test_mismatches_missing_path(self): with test_tarfile() as tf: matcher = TarfileHasFile("foo") @@ -173,6 +184,16 @@ class TarfileHasFileTests(TestCase): "path \"foo\" did not match", mismatch.describe()) + def test_mismatches_wrong_directory_content(self): + with test_tarfile(contents=[("foo/", ""), ("foo/bar", "")]) as tf: + matcher = TarfileHasFile( + "foo", type=tarfile.DIRTYPE, content=["baz"]) + mismatch = matcher.match(tf) + self.assertEquals( + "['baz'] != ['bar']: The content of " + "path \"foo\" did not match", + mismatch.describe()) + def test_matches_mtime_with_skew(self): with test_tarfile(contents=[("foo", "")], default_mtime=12345) as tf: matcher = TarfileHasFile("foo", mtime=12346, mtime_skew=1) |