summaryrefslogtreecommitdiff
path: root/tests/02_test_upstream_source_tar_unpack.py
blob: e6147abd3250011814696b7c7f3b3698096e1cda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# vim: set fileencoding=utf-8 :

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

from . import context

import os
import shutil
import tarfile
import tempfile
# Try unittest2 for CentOS
try:
    import unittest2 as unittest
except ImportError:
    import unittest

import gbp.pkg

class TestUnpack(unittest.TestCase):
    """Make sure we unpack gzip and bzip2 archives correctly"""
    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):
        filelist = [ 'README', 'setup.py' ]

        name = "%s_0.1.tar.%s" % (self.archive_prefix, comp)
        t = tarfile.open(name= name, mode='w:%s' % comp)
        for f in filelist:
            t.add(os.path.join(self.top, f),
                  os.path.join(self._unpack_dir(comp), f))
        t.close()
        return name, filelist

    def setUp(self):
        self.dir = context.new_tmpdir(__name__)
        self.top = context.projectdir
        context.chdir(self.dir)
        self.archives = {}
        for ext in [ "gz", "bz2" ]:
            self.archives[ext] = self._create_archive(ext)

    def tearDown(self):
        context.teardown()

    def test_upstream_source_type(self):
        for (comp, archive) in self.archives.iteritems():
            source = gbp.pkg.UpstreamSource(archive[0])
            assert source.is_orig() == True
            assert source.is_dir() == False
            assert source.unpacked == None
            source.unpack(".")
            assert source.is_orig() == True
            assert source.is_dir() == False
            assert type(source.unpacked) == str

    def test_upstream_source_unpack(self):
        for (comp, archive) in self.archives.iteritems():
            source = gbp.pkg.UpstreamSource(archive[0])
            source.unpack(".")
            self._check_files(archive[1], comp)

    def test_upstream_source_unpack_no_filter(self):
        for (comp, archive) in self.archives.iteritems():
            source = gbp.pkg.UpstreamSource(archive[0])
            source.unpack(".", [])
            self._check_files(archive[1], comp)

    def test_upstream_source_unpack_filtered(self):
        exclude = "README"

        for (comp, archive) in self.archives.iteritems():
            source = gbp.pkg.UpstreamSource(archive[0])
            source.unpack(".", [exclude])
            archive[1].remove(exclude)
            self._check_files(archive[1], comp)

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