~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2008-05-30 02:30:22 UTC
  • Revision ID: aaron@aaronbentley.com-20080530023022-l2fzmlvuzkotwnhv
Remove baz-import

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
from bzrlib import osutils
9
9
from bzrlib.bzrdir import BzrDir
10
 
try:
11
 
    from bzrlib.plugins.bzrtools.upstream_import import (
12
 
        common_directory,
13
 
        import_tar,
14
 
        import_zip,
15
 
        import_dir,
16
 
        top_directory,
17
 
        ZipFileWrapper, 
18
 
    )
19
 
except ImportError:
20
 
    from bzrtools.upstream_import import (
21
 
        common_directory,
22
 
        import_tar,
23
 
        import_zip,
24
 
        top_directory,
25
 
        ZipFileWrapper, 
26
 
    )
 
10
from bzrlib.plugins.bzrtools.upstream_import import (
 
11
    common_directory,
 
12
    import_archive,
 
13
    import_tar,
 
14
    import_zip,
 
15
    import_dir,
 
16
    top_directory,
 
17
    ZipFileWrapper,
 
18
)
27
19
from bzrlib.tests import TestCaseInTempDir
28
20
 
29
21
 
 
22
def import_tar_broken(tree, tar_input):
 
23
    """
 
24
    Import a tarfile with names that that end in //, e.g. Feisty Python 2.5
 
25
    """
 
26
    tar_file = tarfile.open('lala', 'r', tar_input)
 
27
    for member in tar_file.members:
 
28
        if member.name.endswith('/'):
 
29
            member.name += '/'
 
30
    import_archive(tree, tar_file)
 
31
 
 
32
 
30
33
class DirFileWriter(object):
31
34
 
32
35
    def __init__(self, fileobj, mode):
78
81
                os.chdir('project-0.1')
79
82
            os.mkdir(prefix + 'junk')
80
83
            archive_file.add(prefix + 'junk')
81
 
            
 
84
 
82
85
            f = file(prefix + 'README', 'wb')
83
86
            f.write('What?')
84
87
            f.close()
97
100
        result.seek(0)
98
101
        return result
99
102
 
100
 
    def make_archive2(self, builder):
 
103
    def make_archive2(self, builder, subdir):
101
104
        result = StringIO()
102
 
        tar_file = builder(result)
 
105
        archive_file = builder(result)
103
106
        os.mkdir('project-0.2')
104
 
        tar_file.add('project-0.2')
105
 
        
106
 
        os.mkdir('project-0.2/junk')
107
 
        tar_file.add('project-0.2/junk')
108
 
 
109
 
        f = file('project-0.2/README', 'wb')
110
 
        f.write('Now?')
111
 
        f.close()
112
 
        tar_file.add('project-0.2/README')
113
 
        tar_file.close()
114
 
 
115
 
        tar_file = builder(result, 'a')
116
 
        tar_file.add('project-0.2/README')
117
 
 
118
 
        rmtree('project-0.2')
119
 
        tar_file.close()
 
107
        try:
 
108
            if subdir:
 
109
                prefix='project-0.2/'
 
110
                archive_file.add('project-0.2')
 
111
            else:
 
112
                prefix=''
 
113
                os.chdir('project-0.2')
 
114
 
 
115
            os.mkdir(prefix + 'junk')
 
116
            archive_file.add(prefix + 'junk')
 
117
 
 
118
            f = file(prefix + 'README', 'wb')
 
119
            f.write('Now?')
 
120
            f.close()
 
121
            archive_file.add(prefix + 'README')
 
122
            archive_file.close()
 
123
 
 
124
            archive_file = builder(result, 'a')
 
125
            archive_file.add(prefix + 'README')
 
126
            archive_file.close()
 
127
 
 
128
        finally:
 
129
            if not subdir:
 
130
                os.chdir('..')
120
131
        result.seek(0)
121
132
        return result
122
133
 
128
139
 
129
140
        os.mkdir('project-0.2')
130
141
        tar_file.add('project-0.2')
131
 
        
 
142
 
132
143
        f = file('project-0.1/README', 'wb')
133
144
        f.write('What?')
134
145
        f.close()
157
168
            return tarfile.open('project-0.1.tar', mode, fileobj)
158
169
        self.archive_test(builder, import_tar)
159
170
 
 
171
    def test_broken_tar(self):
 
172
        def builder(fileobj, mode='w'):
 
173
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
174
        self.archive_test(builder, import_tar_broken, subdir=True)
 
175
 
160
176
    def test_unzip(self):
161
177
        def builder(fileobj, mode='w'):
162
178
            return ZipFileWrapper(fileobj, mode)
172
188
    def archive_test(self, builder, importer, subdir=False):
173
189
        archive_file = self.make_archive(builder, subdir)
174
190
        tree = BzrDir.create_standalone_workingtree('tree')
175
 
        importer(tree, archive_file)
176
 
        self.assertTrue(tree.path2id('README') is not None) 
177
 
        self.assertTrue(tree.path2id('FEEDME') is not None)
178
 
        self.assertTrue(os.path.isfile(tree.abspath('README')))
179
 
        self.assertEqual(tree.inventory[tree.path2id('README')].kind, 'file')
180
 
        self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind, 'file')
181
 
        
182
 
        f = file(tree.abspath('junk/food'), 'wb')
183
 
        f.write('I like food\n')
184
 
        f.close()
 
191
        tree.lock_write()
 
192
        try:
 
193
            importer(tree, archive_file)
 
194
            self.assertTrue(tree.path2id('README') is not None)
 
195
            self.assertTrue(tree.path2id('FEEDME') is not None)
 
196
            self.assertTrue(os.path.isfile(tree.abspath('README')))
 
197
            self.assertEqual(tree.inventory[tree.path2id('README')].kind,
 
198
                'file')
 
199
            self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
 
200
                'file')
 
201
            f = file(tree.abspath('junk/food'), 'wb')
 
202
            f.write('I like food\n')
 
203
            f.close()
185
204
 
186
 
        archive_file = self.make_archive2(builder)
187
 
        importer(tree, archive_file)
188
 
        self.assertTrue(tree.path2id('README') is not None) 
189
 
        self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
 
205
            archive_file = self.make_archive2(builder, subdir)
 
206
            importer(tree, archive_file)
 
207
            self.assertTrue(tree.path2id('README') is not None)
 
208
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
 
209
        finally:
 
210
            tree.unlock()
190
211
 
191
212
 
192
213
    def test_untar2(self):
193
214
        tar_file = self.make_messed_tar()
194
215
        tree = BzrDir.create_standalone_workingtree('tree')
195
216
        import_tar(tree, tar_file)
196
 
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 
 
217
        self.assertTrue(tree.path2id('project-0.1/README') is not None)
197
218
 
198
219
    def test_untar_gzip(self):
199
220
        tar_file = self.make_tar(mode='w:gz')
200
221
        tree = BzrDir.create_standalone_workingtree('tree')
201
222
        import_tar(tree, tar_file)
202
 
        self.assertTrue(tree.path2id('README') is not None) 
 
223
        self.assertTrue(tree.path2id('README') is not None)
203
224
 
204
225
def test_suite():
205
226
    return makeSuite(TestImport)