~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2007-03-07 14:08:29 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070307140829-95mubswbtpbr7zsx
Update NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from StringIO import StringIO
 
3
from shutil import rmtree, copy2, copytree
 
4
import tarfile
 
5
import tempfile
 
6
from unittest import makeSuite
 
7
 
 
8
from bzrlib import osutils
 
9
from bzrlib.bzrdir import BzrDir
 
10
from bzrlib.plugins.bzrtools.upstream_import import (
 
11
    common_directory,
 
12
    import_tar,
 
13
    import_zip,
 
14
    import_dir,
 
15
    top_directory,
 
16
    ZipFileWrapper, 
 
17
)
 
18
from bzrlib.tests import TestCaseInTempDir
 
19
 
 
20
class DirFileWriter(object):
 
21
 
 
22
    def __init__(self, fileobj, mode):
 
23
        # We may be asked to 'append'.  If so, fileobj already has a path.
 
24
        # So we copy the existing tree, and overwrite afterward.
 
25
        fileobj.seek(0)
 
26
        existing = fileobj.read()
 
27
        fileobj.seek(0)
 
28
        path = tempfile.mkdtemp(dir=os.getcwd())
 
29
        if existing != '':
 
30
            # copytree requires the directory not to exist
 
31
            os.rmdir(path)
 
32
            copytree(existing, path)
 
33
        fileobj.write(path)
 
34
        self.root = path
 
35
 
 
36
    def add(self, path):
 
37
        target_path = os.path.join(self.root, path)
 
38
        parent = osutils.dirname(target_path)
 
39
        if not os.path.exists(parent):
 
40
            os.makedirs(parent)
 
41
        kind = osutils.file_kind(path)
 
42
        if kind == 'file':
 
43
            copy2(path, target_path)
 
44
        if kind == 'directory':
 
45
            os.mkdir(target_path)
 
46
 
 
47
    def close(self):
 
48
        pass
 
49
 
 
50
 
 
51
class TestImport(TestCaseInTempDir):
 
52
 
 
53
    def make_tar(self, mode='w'):
 
54
        def maker(fileobj):
 
55
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
56
        return self.make_archive(maker)
 
57
 
 
58
    def make_archive(self, maker, subdir=True):
 
59
        result = StringIO()
 
60
        archive_file = maker(result)
 
61
        try:
 
62
            os.mkdir('project-0.1')
 
63
            if subdir:
 
64
                prefix='project-0.1/'
 
65
                archive_file.add('project-0.1')
 
66
            else:
 
67
                prefix=''
 
68
                os.chdir('project-0.1')
 
69
            os.mkdir(prefix + 'junk')
 
70
            archive_file.add(prefix + 'junk')
 
71
            
 
72
            f = file(prefix + 'README', 'wb')
 
73
            f.write('What?')
 
74
            f.close()
 
75
            archive_file.add(prefix + 'README')
 
76
 
 
77
            f = file(prefix + 'FEEDME', 'wb')
 
78
            f.write('Hungry!!')
 
79
            f.close()
 
80
            archive_file.add(prefix + 'FEEDME')
 
81
 
 
82
            archive_file.close()
 
83
        finally:
 
84
            if not subdir:
 
85
                os.chdir('..')
 
86
            rmtree('project-0.1')
 
87
        result.seek(0)
 
88
        return result
 
89
 
 
90
    def make_archive2(self, builder, subdir):
 
91
        result = StringIO()
 
92
        archive_file = builder(result)
 
93
        os.mkdir('project-0.2')
 
94
        try:
 
95
            if subdir:
 
96
                prefix='project-0.2/'
 
97
                archive_file.add('project-0.2')
 
98
            else:
 
99
                prefix=''
 
100
                os.chdir('project-0.2')
 
101
            
 
102
            os.mkdir(prefix + 'junk')
 
103
            archive_file.add(prefix + 'junk')
 
104
 
 
105
            f = file(prefix + 'README', 'wb')
 
106
            f.write('Now?')
 
107
            f.close()
 
108
            archive_file.add(prefix + 'README')
 
109
            archive_file.close()
 
110
 
 
111
            archive_file = builder(result, 'a')
 
112
            archive_file.add(prefix + 'README')
 
113
            archive_file.close()
 
114
 
 
115
        finally:
 
116
            if not subdir:
 
117
                os.chdir('..')
 
118
        result.seek(0)
 
119
        return result
 
120
 
 
121
    def make_messed_tar(self):
 
122
        result = StringIO()
 
123
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
 
124
        os.mkdir('project-0.1')
 
125
        tar_file.add('project-0.1')
 
126
 
 
127
        os.mkdir('project-0.2')
 
128
        tar_file.add('project-0.2')
 
129
        
 
130
        f = file('project-0.1/README', 'wb')
 
131
        f.write('What?')
 
132
        f.close()
 
133
        tar_file.add('project-0.1/README')
 
134
        tar_file.close()
 
135
        rmtree('project-0.1')
 
136
        result.seek(0)
 
137
        return result
 
138
 
 
139
    def make_zip(self):
 
140
        def maker(fileobj):
 
141
            return ZipFileWrapper(fileobj, 'w')
 
142
        return self.make_archive(maker)
 
143
 
 
144
    def test_top_directory(self):
 
145
        self.assertEqual(top_directory('ab/b/c'), 'ab')
 
146
        self.assertEqual(top_directory('/etc'), '/')
 
147
 
 
148
    def test_common_directory(self):
 
149
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
 
150
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
 
151
        self.assertIs(None, common_directory(['FEEDME']))
 
152
 
 
153
    def test_untar(self):
 
154
        def builder(fileobj, mode='w'):
 
155
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
156
        self.archive_test(builder, import_tar)
 
157
 
 
158
    def test_unzip(self):
 
159
        def builder(fileobj, mode='w'):
 
160
            return ZipFileWrapper(fileobj, mode)
 
161
        self.archive_test(builder, import_zip)
 
162
 
 
163
    def test_copydir_nosub(self):
 
164
        def builder(fileobj, mode='w'):
 
165
            return DirFileWriter(fileobj, mode)
 
166
        # It would be bogus to test with the result in a subdirectory,
 
167
        # because for directories, the input root is always the output root.
 
168
        self.archive_test(builder, import_dir)
 
169
 
 
170
    def archive_test(self, builder, importer, subdir=False):
 
171
        archive_file = self.make_archive(builder, subdir)
 
172
        tree = BzrDir.create_standalone_workingtree('tree')
 
173
        importer(tree, archive_file)
 
174
        self.assertTrue(tree.path2id('README') is not None) 
 
175
        self.assertTrue(tree.path2id('FEEDME') is not None)
 
176
        self.assertTrue(os.path.isfile(tree.abspath('README')))
 
177
        self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
 
178
        self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
 
179
        
 
180
        f = file(tree.abspath('junk/food'), 'wb')
 
181
        f.write('I like food\n')
 
182
        f.close()
 
183
 
 
184
        archive_file = self.make_archive2(builder, subdir)
 
185
        importer(tree, archive_file)
 
186
        self.assertTrue(tree.path2id('README') is not None) 
 
187
        self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
 
188
 
 
189
 
 
190
    def test_untar2(self):
 
191
        tar_file = self.make_messed_tar()
 
192
        tree = BzrDir.create_standalone_workingtree('tree')
 
193
        import_tar(tree, tar_file)
 
194
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 
 
195
 
 
196
    def test_untar_gzip(self):
 
197
        tar_file = self.make_tar(mode='w:gz')
 
198
        tree = BzrDir.create_standalone_workingtree('tree')
 
199
        import_tar(tree, tar_file)
 
200
        self.assertTrue(tree.path2id('README') is not None) 
 
201
 
 
202
def test_suite():
 
203
    return makeSuite(TestImport)