File: test_repack_tarball.py

package info (click to toggle)
breezy-debian 2.8.81
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,448 kB
  • sloc: python: 19,137; makefile: 61; sh: 1
file content (147 lines) | stat: -rw-r--r-- 5,309 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#    test_repack_tarball.py -- Testsuite for repacking of tarballs
#    Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
#
#    This file is part of bzr-builddeb.
#
#    bzr-builddeb is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    bzr-builddeb is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with bzr-builddeb; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

import os
import shutil
import tarfile

from ....transport import (
    NoSuchFile,
    FileExists,
)

from ..repack_tarball import (
    repack_tarball,
    get_filetype,
    get_repacker_class,
    UnsupportedRepackFormat,
)
from . import TestCaseInTempDir


def touch(filename):
    f = open(filename, "w")
    try:
        f.write(" ")
    finally:
        f.close()


class TestRepackTarball(TestCaseInTempDir):
    basedir = "package-0.2/"
    files = ["README"]
    bare_tarball_name = "package-0.2.tar"
    tgz_tarball_name = "package-0.2.tgz"
    tar_gz_tarball_name = "package-0.2.tar.gz"
    tar_bz2_tarball_name = "package-0.2.tar.bz2"
    new_tarball = "package_0.2.orig.tar.gz"

    def create_basedir(self):
        """Create the basedir that the source can be built from"""
        os.mkdir(self.basedir)
        for filename in [os.path.join(self.basedir, file) for file in self.files]:
            if filename.endswith("/"):
                os.mkdir(filename)
            else:
                touch(filename)

    def create_old_tarball(self):
        self.create_basedir()
        self.build_tarball(self.basedir, self.old_tarball)
        shutil.rmtree(self.basedir)

    def test_create_old_tarball(self):
        self.create_old_tarball()
        self.assertPathExists(self.old_tarball)

    def test_repack_tarball_non_extant(self):
        error = NoSuchFile
        old_format = get_filetype(self.old_tarball)
        new_format = get_filetype(self.new_tarball)
        if get_repacker_class(old_format, new_format) is None:
            # directory, can't really be detected remotely, so we have a
            # error that could mean two things
            error = UnsupportedRepackFormat
        self.assertRaises(error, repack_tarball, self.old_tarball, self.new_tarball)

    def test_repack_tarball_result_extant(self):
        self.create_old_tarball()
        touch(self.new_tarball)
        self.assertRaises(
            FileExists, repack_tarball, self.old_tarball, self.new_tarball
        )

    def test_repack_tarball_creates_new(self):
        self.create_old_tarball()
        repack_tarball(self.old_tarball, self.new_tarball)
        self.assertPathExists(self.old_tarball)
        self.assertPathExists(self.new_tarball)

    def test_repack_tarball_contents(self):
        self.create_old_tarball()
        repack_tarball(self.old_tarball, self.new_tarball)
        tar = tarfile.open(self.new_tarball, "r:gz")
        try:
            # The tarfile module returns member names of directories with a
            # trailing slash, in Python 2.5 *only*.
            members = [x.rstrip("/") for x in tar.getnames()]
        finally:
            tar.close()
        self.assertEqual(
            members,
            [self.basedir.rstrip("/")]
            + [os.path.join(self.basedir, file).rstrip("/") for file in self.files],
        )

    def test_repack_tarball_with_target_dir(self):
        self.create_old_tarball()
        target_dir = "tarballs"
        repack_tarball(self.old_tarball, self.new_tarball, target_dir=target_dir)
        self.assertPathExists(target_dir)
        self.assertPathExists(os.path.join(target_dir, self.new_tarball))
        self.assertPathExists(self.old_tarball)
        self.assertPathDoesNotExist(self.new_tarball)

    def test_repack_tarball_with_target_dir_exists(self):
        self.create_old_tarball()
        target_dir = "tarballs"
        os.mkdir(target_dir)
        repack_tarball(self.old_tarball, self.new_tarball, target_dir=target_dir)
        self.assertPathExists(target_dir)
        self.assertPathExists(os.path.join(target_dir, self.new_tarball))
        self.assertPathExists(self.old_tarball)
        self.assertPathDoesNotExist(self.new_tarball)

    def test_repack_tarball_with_target_dir_not_dir(self):
        self.create_old_tarball()
        target_dir = "tarballs"
        touch(target_dir)
        # transport gives NoSuchFile rather than NotADirectory for this
        self.assertRaises(
            (IOError, NoSuchFile),
            repack_tarball,
            self.old_tarball,
            self.new_tarball,
            target_dir=target_dir,
        )
        self.assertPathExists(self.old_tarball)
        self.assertPathDoesNotExist(self.new_tarball)
        self.assertPathDoesNotExist(os.path.join(target_dir, self.new_tarball))
        self.assertPathExists(target_dir)