06_test_upstream_source.py 3.09 KB
Newer Older
1 2 3
# vim: set fileencoding=utf-8 :

"""Test the L{UpstreamSource} class"""
4

5 6
from . import context

7 8 9 10 11 12
import glob
import os
import tarfile
import unittest
import zipfile

13
from gbp.pkg import UpstreamSource
14

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

16
class TestDir(unittest.TestCase):
17
    def setUp(self):
18 19
        self.tmpdir = context.new_tmpdir(__name__)
        self.upstream_dir = self.tmpdir.join('test-1.0')
20 21
        os.mkdir(self.upstream_dir)

22 23
    def test_directory(self):
        """Upstream source is a directory"""
24
        source = UpstreamSource(self.upstream_dir)
25
        self.assertEqual(source.is_orig(), False)
26 27
        self.assertEqual(source.path, self.upstream_dir)
        self.assertEqual(source.unpacked, self.upstream_dir)
28
        self.assertEqual(source.guess_version(), ('test', '1.0'))
29

30
    def tearDown(self):
31
        context.teardown()
32

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

34
class TestTar(unittest.TestCase):
35
    """Test if packing tar archives works"""
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    def _check_tar(self, us, positive=[], negative=[]):
        t = tarfile.open(name=us.path, mode="r:bz2")
        for f in positive:
            i = t.getmember(f)
            self.assertEqual(type(i), tarfile.TarInfo)

        for f in negative:
            try:
                t.getmember(f)
                self.fail("Found %s in archive" % f)
            except KeyError:
                pass
        t.close()

    def setUp(self):
51 52
        self.tmpdir = context.new_tmpdir(__name__)
        self.source = UpstreamSource(os.path.join(context.projectdir, "gbp"))
53 54

    def tearDown(self):
55
        context.teardown()
56 57 58

    def test_pack_tar(self):
        """Check if packing tar archives works"""
59 60
        target = self.tmpdir.join("gbp_0.1.tar.bz2")
        repacked = self.source.pack(target)
61 62
        self.assertEqual(repacked.is_orig(), True)
        self.assertEqual(repacked.is_dir(), False)
63
        self.assertEqual(repacked.guess_version(), ('gbp', '0.1'))
64
        self._check_tar(repacked, ["gbp/errors.py", "gbp/__init__.py"])
65 66 67

    def test_pack_filtered(self):
        """Check if filtering out files works"""
68 69
        target = self.tmpdir.join("gbp_0.1.tar.bz2")
        repacked = self.source.pack(target, ["__init__.py"])
70 71
        self.assertEqual(repacked.is_orig(), True)
        self.assertEqual(repacked.is_dir(), False)
72
        self._check_tar(repacked, ["gbp/errors.py"],
73 74 75 76
                                  ["gbp/__init__.py"])


class TestZip(unittest.TestCase):
77
    """Test if unpacking zip archives works"""
78
    def setUp(self):
79 80 81 82
        self.tmpdir = context.new_tmpdir(__name__)
        self.zipfile = self.tmpdir.join("gbp-0.1.zip")
        z = zipfile.ZipFile(self.zipfile, "w")
        for f in glob.glob(os.path.join(context.projectdir, "gbp/*.py")):
83 84 85 86
            z.write(f, f, zipfile.ZIP_DEFLATED)
        z.close()

    def tearDown(self):
87
        context.teardown()
88 89 90

    def test_unpack(self):
        source = UpstreamSource(self.zipfile)
91 92
        self.assertEqual(source.is_orig(), False)
        self.assertEqual(source.is_dir(), False)
93
        self.assertEqual(source.unpacked, None)
94
        self.assertEqual(source.guess_version(), ('gbp', '0.1'))
95
        source.unpack(str(self.tmpdir))
96
        self.assertNotEqual(source.unpacked, None)