~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to tests/upstream_import.py

  • Committer: Michael Ellerman
  • Date: 2005-11-29 07:12:26 UTC
  • mto: (0.3.1 shelf-dev) (325.1.2 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 334.
  • Revision ID: michael@ellerman.id.au-20051129071226-a04b3f827880025d
Unshelve --pick was broken, because we deleted the whole patch, even when only
part of it was unshelved. So now if we unshelve part of a patch, the patch is
replaced with a new patch that has just the unshelved parts. That's a long way
of saying it does what you'd expect.

Implementing this required changing HunkSelector to return both the selected,
and unselected hunks (ie. patches to shelve, and patches to keep).

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
 
        tree.lock_write()
174
 
        try:
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,
180
 
                'file')
181
 
            self.assertEqual(tree.inventory[tree.path2id('FEEDME')].kind,
182
 
                'file')
183
 
            f = file(tree.abspath('junk/food'), 'wb')
184
 
            f.write('I like food\n')
185
 
            f.close()
186
 
 
187
 
            archive_file = self.make_archive2(builder, subdir)
188
 
            importer(tree, archive_file)
189
 
            self.assertTrue(tree.path2id('README') is not None)
190
 
            self.assertTrue(not os.path.exists(tree.abspath('FEEDME')))
191
 
        finally:
192
 
            tree.unlock()
193
 
 
194
 
 
195
 
    def test_untar2(self):
196
 
        tar_file = self.make_messed_tar()
197
 
        tree = BzrDir.create_standalone_workingtree('tree')
198
 
        import_tar(tree, tar_file)
199
 
        self.assertTrue(tree.path2id('project-0.1/README') is not None) 
200
 
 
201
 
    def test_untar_gzip(self):
202
 
        tar_file = self.make_tar(mode='w:gz')
203
 
        tree = BzrDir.create_standalone_workingtree('tree')
204
 
        import_tar(tree, tar_file)
205
 
        self.assertTrue(tree.path2id('README') is not None) 
206
 
 
207
 
def test_suite():
208
 
    return makeSuite(TestImport)