02_test_upstream_source_tar_unpack.py 2.6 KB
Newer Older
Guido Günther's avatar
Guido Günther committed
1 2
# vim: set fileencoding=utf-8 :

3 4
"""Test L{UpstreamSource}'s tarball unpack"""

5 6
from . import context

Guido Günther's avatar
Guido Günther committed
7 8
import os
import tarfile
9
import unittest
Guido Günther's avatar
Guido Günther committed
10

11
import gbp.pkg
Guido Günther's avatar
Guido Günther committed
12

Guido Günther's avatar
Guido Günther committed
13

14
class TestUnpack(unittest.TestCase):
Guido Günther's avatar
Guido Günther committed
15
    """Make sure we unpack gzip and bzip2 archives correctly"""
16 17 18 19 20 21 22 23 24 25 26 27
    archive_prefix = "archive"

    def _unpack_dir(self, compression):
        return "%s-%s" % (self.archive_prefix, compression)

    def _check_files(self, files, comp):
        """Check if files exist in the unpacked dir"""
        for f in files:
            target = os.path.join(self._unpack_dir(comp), f)
            assert os.path.exists(target), "%s does not exist" % target

    def _create_archive(self, comp):
28
        filelist = ['README.md', 'setup.py']
29 30

        name = "%s_0.1.tar.%s" % (self.archive_prefix, comp)
Guido Günther's avatar
Guido Günther committed
31
        t = tarfile.open(name=name, mode='w:%s' % comp)
32 33 34
        for f in filelist:
            t.add(os.path.join(self.top, f),
                  os.path.join(self._unpack_dir(comp), f))
Guido Günther's avatar
Guido Günther committed
35
        t.close()
36
        return name, filelist
Guido Günther's avatar
Guido Günther committed
37 38

    def setUp(self):
39 40 41
        self.dir = context.new_tmpdir(__name__)
        self.top = context.projectdir
        context.chdir(self.dir)
Guido Günther's avatar
Guido Günther committed
42
        self.archives = {}
Guido Günther's avatar
Guido Günther committed
43
        for ext in ["gz", "bz2"]:
44
            self.archives[ext] = self._create_archive(ext)
Guido Günther's avatar
Guido Günther committed
45 46

    def tearDown(self):
47
        context.teardown()
Guido Günther's avatar
Guido Günther committed
48

49
    def test_upstream_source_type(self):
Guido Günther's avatar
Guido Günther committed
50
        for (comp, archive) in self.archives.items():
51
            source = gbp.pkg.UpstreamSource(archive[0])
Guido Günther's avatar
Guido Günther committed
52 53 54
            assert source.is_orig() is True
            assert source.is_dir() is False
            assert source.unpacked is None
55
            source.unpack(".")
Guido Günther's avatar
Guido Günther committed
56 57
            assert source.is_orig() is True
            assert source.is_dir() is False
58 59 60
            assert type(source.unpacked) == str

    def test_upstream_source_unpack(self):
Guido Günther's avatar
Guido Günther committed
61
        for (comp, archive) in self.archives.items():
62
            source = gbp.pkg.UpstreamSource(archive[0])
63 64 65 66
            source.unpack(".")
            self._check_files(archive[1], comp)

    def test_upstream_source_unpack_no_filter(self):
Guido Günther's avatar
Guido Günther committed
67
        for (comp, archive) in self.archives.items():
68
            source = gbp.pkg.UpstreamSource(archive[0])
69 70 71 72
            source.unpack(".", [])
            self._check_files(archive[1], comp)

    def test_upstream_source_unpack_filtered(self):
73
        exclude = "README.md"
74

Guido Günther's avatar
Guido Günther committed
75
        for (comp, archive) in self.archives.items():
76
            source = gbp.pkg.UpstreamSource(archive[0])
77 78 79
            source.unpack(".", [exclude])
            archive[1].remove(exclude)
            self._check_files(archive[1], comp)
Guido Günther's avatar
Guido Günther committed
80 81

# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: