~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Aaron Bentley
  • Date: 2007-01-08 17:27:48 UTC
  • Revision ID: abentley@panoramicfeedback.com-20070108172748-1b22qtszaadoby89
Improve bzr import docs

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
 
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
 
)
 
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
    )
19
27
from bzrlib.tests import TestCaseInTempDir
20
28
 
21
29
 
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
 
 
33
30
class DirFileWriter(object):
34
31
 
35
32
    def __init__(self, fileobj, mode):
81
78
                os.chdir('project-0.1')
82
79
            os.mkdir(prefix + 'junk')
83
80
            archive_file.add(prefix + 'junk')
84
 
 
 
81
            
85
82
            f = file(prefix + 'README', 'wb')
86
83
            f.write('What?')
87
84
            f.close()
100
97
        result.seek(0)
101
98
        return result
102
99
 
103
 
    def make_archive2(self, builder, subdir):
 
100
    def make_archive2(self, builder):
104
101
        result = StringIO()
105
 
        archive_file = builder(result)
 
102
        tar_file = builder(result)
106
103
        os.mkdir('project-0.2')
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('..')
 
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()
131
120
        result.seek(0)
132
121
        return result
133
122
 
139
128
 
140
129
        os.mkdir('project-0.2')
141
130
        tar_file.add('project-0.2')
142
 
 
 
131
        
143
132
        f = file('project-0.1/README', 'wb')
144
133
        f.write('What?')
145
134
        f.close()
168
157
            return tarfile.open('project-0.1.tar', mode, fileobj)
169
158
        self.archive_test(builder, import_tar)
170
159
 
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
 
 
176
160
    def test_unzip(self):
177
161
        def builder(fileobj, mode='w'):
178
162
            return ZipFileWrapper(fileobj, mode)
188
172
    def archive_test(self, builder, importer, subdir=False):
189
173
        archive_file = self.make_archive(builder, subdir)
190
174
        tree = BzrDir.create_standalone_workingtree('tree')
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()
 
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()
204
185
 
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()
 
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')))
211
190
 
212
191
 
213
192
    def test_untar2(self):
214
193
        tar_file = self.make_messed_tar()
215
194
        tree = BzrDir.create_standalone_workingtree('tree')
216
195
        import_tar(tree, tar_file)
217
 
        self.assertTrue(tree.path2id('project-0.1/README') is not None)
 
196
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 
218
197
 
219
198
    def test_untar_gzip(self):
220
199
        tar_file = self.make_tar(mode='w:gz')
221
200
        tree = BzrDir.create_standalone_workingtree('tree')
222
201
        import_tar(tree, tar_file)
223
 
        self.assertTrue(tree.path2id('README') is not None)
 
202
        self.assertTrue(tree.path2id('README') is not None) 
224
203
 
225
204
def test_suite():
226
205
    return makeSuite(TestImport)