~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2006-12-12 22:58:12 UTC
  • Revision ID: abentley@panoramicfeedback.com-20061212225812-9xwer0h2uqjz2lxb
Generalize tests for zip

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    def extractfile(self, infowrapper):
30
30
        return StringIO(self.zipfile.read(infowrapper.name))
31
31
 
 
32
    def add(self, filename):
 
33
        if isdir(filename):
 
34
            self.zipfile.writestr(filename+'/', '')
 
35
        else:
 
36
            self.zipfile.write(filename)
 
37
 
 
38
 
 
39
    def close(self):
 
40
        self.zipfile.close()
 
41
 
32
42
 
33
43
class ZipInfoWrapper(object):
34
44
    
209
219
class TestImport(TestCaseInTempDir):
210
220
 
211
221
    def make_tar(self, mode='w'):
 
222
        def maker(fileobj):
 
223
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
224
        return self.make_archive(maker)
 
225
 
 
226
    def make_archive(self, maker):
212
227
        result = StringIO()
213
 
        tar_file = tarfile.open('project-0.1.tar', mode, result)
 
228
        archive_file = maker(result)
214
229
        os.mkdir('project-0.1')
215
 
        tar_file.add('project-0.1')
 
230
        archive_file.add('project-0.1')
216
231
        os.mkdir('project-0.1/junk')
217
 
        tar_file.add('project-0.1/junk')
 
232
        archive_file.add('project-0.1/junk')
218
233
        
219
234
        f = file('project-0.1/README', 'wb')
220
235
        f.write('What?')
221
236
        f.close()
222
 
        tar_file.add('project-0.1/README')
 
237
        archive_file.add('project-0.1/README')
223
238
 
224
239
        f = file('project-0.1/FEEDME', 'wb')
225
240
        f.write('Hungry!!')
226
241
        f.close()
227
 
        tar_file.add('project-0.1/FEEDME')
 
242
        archive_file.add('project-0.1/FEEDME')
228
243
 
229
 
        tar_file.close()
 
244
        archive_file.close()
230
245
        rmtree('project-0.1')
231
246
        result.seek(0)
232
247
        return result
270
285
        result.seek(0)
271
286
        return result
272
287
 
273
 
    def make_zip(self, mode='w'):
274
 
        result = StringIO()
275
 
        zip_file = zipfile.ZipFile(result, mode)
276
 
        os.mkdir('project-0.1')
277
 
        zip_file.writestr('project-0.1/', '')
278
 
        os.mkdir('project-0.1/junk')
279
 
        zip_file.writestr('project-0.1/junk/', '')
280
 
        
281
 
        f = file('project-0.1/README', 'wb')
282
 
        f.write('What?')
283
 
        f.close()
284
 
        zip_file.write('project-0.1/README')
285
 
 
286
 
        f = file('project-0.1/FEEDME', 'wb')
287
 
        f.write('Hungry!!')
288
 
        f.close()
289
 
        zip_file.write('project-0.1/FEEDME')
290
 
 
291
 
        zip_file.close()
292
 
        rmtree('project-0.1')
293
 
        result.seek(0)
294
 
        return result
 
288
    def make_zip(self):
 
289
        def maker(fileobj):
 
290
            return ZipFileWrapper(zipfile.ZipFile(fileobj, 'w'))
 
291
        return self.make_archive(maker)
295
292
 
296
293
    def test_top_directory(self):
297
294
        self.assertEqual(top_directory('ab/b/c'), 'ab')