~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:
 
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
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
    )
 
27
from bzrlib.tests import TestCaseInTempDir
 
28
 
 
29
 
 
30
class DirFileWriter(object):
 
31
 
 
32
    def __init__(self, fileobj, mode):
 
33
        # We may be asked to 'append'.  If so, fileobj already has a path.
 
34
        # So we copy the existing tree, and overwrite afterward.
 
35
        fileobj.seek(0)
 
36
        existing = fileobj.read()
 
37
        fileobj.seek(0)
 
38
        path = tempfile.mkdtemp(dir=os.getcwd())
 
39
        if existing != '':
 
40
            # copytree requires the directory not to exist
 
41
            os.rmdir(path)
 
42
            copytree(existing, path)
 
43
        fileobj.write(path)
 
44
        self.root = path
 
45
 
 
46
    def add(self, path):
 
47
        target_path = os.path.join(self.root, path)
 
48
        parent = osutils.dirname(target_path)
 
49
        if not os.path.exists(parent):
 
50
            os.makedirs(parent)
 
51
        kind = osutils.file_kind(path)
 
52
        if kind == 'file':
 
53
            copy2(path, target_path)
 
54
        if kind == 'directory':
 
55
            os.mkdir(target_path)
 
56
 
 
57
    def close(self):
 
58
        pass
 
59
 
 
60
 
 
61
class TestImport(TestCaseInTempDir):
 
62
 
 
63
    def make_tar(self, mode='w'):
 
64
        def maker(fileobj):
 
65
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
66
        return self.make_archive(maker)
 
67
 
 
68
    def make_archive(self, maker, subdir=True):
 
69
        result = StringIO()
 
70
        archive_file = maker(result)
 
71
        try:
 
72
            os.mkdir('project-0.1')
 
73
            if subdir:
 
74
                prefix='project-0.1/'
 
75
                archive_file.add('project-0.1')
 
76
            else:
 
77
                prefix=''
 
78
                os.chdir('project-0.1')
 
79
            os.mkdir(prefix + 'junk')
 
80
            archive_file.add(prefix + 'junk')
 
81
            
 
82
            f = file(prefix + 'README', 'wb')
 
83
            f.write('What?')
 
84
            f.close()
 
85
            archive_file.add(prefix + 'README')
 
86
 
 
87
            f = file(prefix + 'FEEDME', 'wb')
 
88
            f.write('Hungry!!')
 
89
            f.close()
 
90
            archive_file.add(prefix + 'FEEDME')
 
91
 
 
92
            archive_file.close()
 
93
        finally:
 
94
            if not subdir:
 
95
                os.chdir('..')
 
96
            rmtree('project-0.1')
 
97
        result.seek(0)
 
98
        return result
 
99
 
 
100
    def make_archive2(self, builder):
 
101
        result = StringIO()
 
102
        tar_file = builder(result)
 
103
        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()
 
120
        result.seek(0)
 
121
        return result
 
122
 
 
123
    def make_messed_tar(self):
 
124
        result = StringIO()
 
125
        tar_file = tarfile.open('project-0.1.tar', 'w', result)
 
126
        os.mkdir('project-0.1')
 
127
        tar_file.add('project-0.1')
 
128
 
 
129
        os.mkdir('project-0.2')
 
130
        tar_file.add('project-0.2')
 
131
        
 
132
        f = file('project-0.1/README', 'wb')
 
133
        f.write('What?')
 
134
        f.close()
 
135
        tar_file.add('project-0.1/README')
 
136
        tar_file.close()
 
137
        rmtree('project-0.1')
 
138
        result.seek(0)
 
139
        return result
 
140
 
 
141
    def make_zip(self):
 
142
        def maker(fileobj):
 
143
            return ZipFileWrapper(fileobj, 'w')
 
144
        return self.make_archive(maker)
 
145
 
 
146
    def test_top_directory(self):
 
147
        self.assertEqual(top_directory('ab/b/c'), 'ab')
 
148
        self.assertEqual(top_directory('/etc'), '/')
 
149
 
 
150
    def test_common_directory(self):
 
151
        self.assertEqual(common_directory(['ab/c/d', 'ab/c/e']), 'ab')
 
152
        self.assertIs(common_directory(['ab/c/d', 'ac/c/e']), None)
 
153
        self.assertIs(None, common_directory(['FEEDME']))
 
154
 
 
155
    def test_untar(self):
 
156
        def builder(fileobj, mode='w'):
 
157
            return tarfile.open('project-0.1.tar', mode, fileobj)
 
158
        self.archive_test(builder, import_tar)
 
159
 
 
160
    def test_unzip(self):
 
161
        def builder(fileobj, mode='w'):
 
162
            return ZipFileWrapper(fileobj, mode)
 
163
        self.archive_test(builder, import_zip)
 
164
 
 
165
    def test_copydir_nosub(self):
 
166
        def builder(fileobj, mode='w'):
 
167
            return DirFileWriter(fileobj, mode)
 
168
        # It would be bogus to test with the result in a subdirectory,
 
169
        # because for directories, the input root is always the output root.
 
170
        self.archive_test(builder, import_dir)
 
171
 
 
172
    def archive_test(self, builder, importer, subdir=False):
 
173
        archive_file = self.make_archive(builder, subdir)
 
174
        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()
 
185
 
 
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')))
 
190
 
 
191
 
 
192
    def test_untar2(self):
 
193
        tar_file = self.make_messed_tar()
 
194
        tree = BzrDir.create_standalone_workingtree('tree')
 
195
        import_tar(tree, tar_file)
 
196
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 
 
197
 
 
198
    def test_untar_gzip(self):
 
199
        tar_file = self.make_tar(mode='w:gz')
 
200
        tree = BzrDir.create_standalone_workingtree('tree')
 
201
        import_tar(tree, tar_file)
 
202
        self.assertTrue(tree.path2id('README') is not None) 
 
203
 
 
204
def test_suite():
 
205
    return makeSuite(TestImport)