40
43
        super(TestWithUpgradableBranches, self).setUp()
 
41
 
        self.addCleanup(bzrdir.BzrDirFormat._set_default_format,
 
42
 
                        bzrdir.BzrDirFormat.get_default_format())
 
44
 
    def make_current_format_branch_and_checkout(self):
 
 
44
        self.old_format = bzrdir.BzrDirFormat.get_default_format()
 
 
45
        self.old_ui_factory = ui.ui_factory
 
 
46
        self.addCleanup(self.restoreDefaults)
 
 
48
        ui.ui_factory = TestUIFactory()
 
 
49
        # setup a format 5 branch we can upgrade from.
 
 
50
        self.make_branch_and_tree('format_5_branch',
 
 
51
                                  format=bzrdir.BzrDirFormat5())
 
45
53
        current_tree = self.make_branch_and_tree('current_format_branch',
 
 
55
        self.make_branch_and_tree('metadir_weave_branch', format='metaweave')
 
47
56
        current_tree.branch.create_checkout(
 
48
57
            self.get_url('current_format_checkout'), lightweight=True)
 
50
 
    def make_format_5_branch(self):
 
51
 
        # setup a format 5 branch we can upgrade from.
 
52
 
        self.make_branch_and_tree('format_5_branch',
 
53
 
                                  format=bzrdir.BzrDirFormat5())
 
55
 
    def make_metadir_weave_branch(self):
 
56
 
        self.make_branch_and_tree('metadir_weave_branch', format='metaweave')
 
 
59
    def restoreDefaults(self):
 
 
60
        ui.ui_factory = self.old_ui_factory
 
 
61
        bzrdir.BzrDirFormat._set_default_format(self.old_format)
 
58
63
    def test_readonly_url_error(self):
 
59
 
        self.make_format_5_branch()
 
60
 
        (out, err) = self.run_bzr(
 
61
 
            ['upgrade', self.get_readonly_url('format_5_branch')], retcode=3)
 
 
64
        (out, err) = self.run_bzr_captured(
 
 
65
            ['upgrade', self.get_readonly_url('format_5_branch')], 3)
 
62
66
        self.assertEqual(out, "")
 
63
67
        self.assertEqual(err, "bzr: ERROR: Upgrade URL cannot work with readonly URLs.\n")
 
65
69
    def test_upgrade_up_to_date(self):
 
66
 
        self.make_current_format_branch_and_checkout()
 
67
70
        # when up to date we should get a message to that effect
 
68
 
        (out, err) = self.run_bzr('upgrade current_format_branch', retcode=3)
 
 
71
        (out, err) = self.run_bzr_captured(
 
 
72
            ['upgrade', 'current_format_branch'], 3)
 
69
73
        self.assertEqual("", out)
 
70
 
        self.assertEqualDiff("bzr: ERROR: The branch format Meta "
 
71
 
                             "directory format 1 is already at the most "
 
 
74
        self.assertEqualDiff("bzr: ERROR: The branch format Bazaar-NG meta "
 
 
75
                             "directory, format 1 is already at the most "
 
72
76
                             "recent format.\n", err)
 
74
78
    def test_upgrade_up_to_date_checkout_warns_branch_left_alone(self):
 
75
 
        self.make_current_format_branch_and_checkout()
 
76
79
        # when upgrading a checkout, the branch location and a suggestion
 
77
 
        # to upgrade it should be emitted even if the checkout is up to
 
 
80
        # to upgrade it should be emitted even if the checkout is up to 
 
79
 
        (out, err) = self.run_bzr('upgrade current_format_checkout', retcode=3)
 
 
82
        (out, err) = self.run_bzr_captured(
 
 
83
            ['upgrade', 'current_format_checkout'], 3)
 
80
84
        self.assertEqual("This is a checkout. The branch (%s) needs to be "
 
81
 
                         "upgraded separately.\n"
 
 
85
                         "upgraded separately.\n" 
 
82
86
                         % get_transport(self.get_url('current_format_branch')).base,
 
84
 
        self.assertEqualDiff("bzr: ERROR: The branch format Meta "
 
85
 
                             "directory format 1 is already at the most "
 
 
88
        self.assertEqualDiff("bzr: ERROR: The branch format Bazaar-NG meta "
 
 
89
                             "directory, format 1 is already at the most "
 
86
90
                             "recent format.\n", err)
 
88
92
    def test_upgrade_checkout(self):
 
 
101
105
    def test_upgrade_explicit_metaformat(self):
 
102
106
        # users can force an upgrade to metadir format.
 
103
 
        self.make_format_5_branch()
 
104
107
        url = get_transport(self.get_url('format_5_branch')).base
 
105
108
        # check --format takes effect
 
106
109
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
107
 
        backup_dir = 'backup.bzr.~1~'
 
108
 
        (out, err) = self.run_bzr(
 
 
110
        (out, err) = self.run_bzr_captured(
 
109
111
            ['upgrade', '--format=metaweave', url])
 
110
112
        self.assertEqualDiff("""starting upgrade of %s
 
111
 
making backup of %s.bzr
 
 
113
making backup of tree history
 
 
114
%s.bzr has been backed up to %s.bzr.backup
 
 
115
if conversion fails, you can move this directory back to .bzr
 
 
116
if it succeeds, you can remove this directory if you wish
 
113
117
starting upgrade from format 5 to 6
 
114
118
adding prefixes to weaves
 
115
119
adding prefixes to revision-store
 
116
120
starting upgrade from format 6 to metadir
 
118
 
""" % (url, url, url, backup_dir), out)
 
 
122
""" % (url, url, url), out)
 
119
123
        self.assertEqualDiff("", err)
 
120
124
        self.assertTrue(isinstance(
 
121
125
            bzrdir.BzrDir.open(self.get_url('format_5_branch'))._format,
 
122
126
            bzrdir.BzrDirMetaFormat1))
 
124
128
    def test_upgrade_explicit_knit(self):
 
125
 
        # users can force an upgrade to knit format from a metadir weave
 
 
129
        # users can force an upgrade to knit format from a metadir weave 
 
127
 
        self.make_metadir_weave_branch()
 
128
131
        url = get_transport(self.get_url('metadir_weave_branch')).base
 
129
132
        # check --format takes effect
 
130
133
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
131
 
        backup_dir = 'backup.bzr.~1~'
 
132
 
        (out, err) = self.run_bzr(
 
 
134
        (out, err) = self.run_bzr_captured(
 
133
135
            ['upgrade', '--format=knit', url])
 
134
136
        self.assertEqualDiff("""starting upgrade of %s
 
135
 
making backup of %s.bzr
 
 
137
making backup of tree history
 
 
138
%s.bzr has been backed up to %s.bzr.backup
 
 
139
if conversion fails, you can move this directory back to .bzr
 
 
140
if it succeeds, you can remove this directory if you wish
 
137
141
starting repository conversion
 
138
142
repository converted
 
140
 
""" % (url, url, url, backup_dir), out)
 
 
144
""" % (url, url, url), out)
 
141
145
        self.assertEqualDiff("", err)
 
142
146
        converted_dir = bzrdir.BzrDir.open(self.get_url('metadir_weave_branch'))
 
143
147
        self.assertTrue(isinstance(converted_dir._format,
 
 
146
150
                                   RepositoryFormatKnit1))
 
148
152
    def test_upgrade_repo(self):
 
149
 
        self.run_bzr('init-repository --format=metaweave repo')
 
150
 
        self.run_bzr('upgrade --format=knit repo')
 
152
 
    def test_upgrade_permission_check(self):
 
153
 
        """'backup.bzr' should retain permissions of .bzr. Bug #262450"""
 
154
 
        self.requireFeature(features.posix_permissions_feature)
 
155
 
        old_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
 
156
 
        backup_dir = 'backup.bzr.~1~'
 
157
 
        self.run_bzr('init --format=1.6')
 
158
 
        os.chmod('.bzr', old_perms)
 
159
 
        self.run_bzr('upgrade')
 
160
 
        new_perms = os.stat(backup_dir).st_mode & 0777
 
161
 
        self.assertTrue(new_perms == old_perms)
 
164
 
    def test_upgrade_with_existing_backup_dir(self):
 
165
 
        self.make_format_5_branch()
 
166
 
        transport = get_transport(self.get_url('format_5_branch'))
 
168
 
        bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
 
169
 
        backup_dir1 = 'backup.bzr.~1~'
 
170
 
        backup_dir2 = 'backup.bzr.~2~'
 
171
 
        # explicitly create backup_dir1. bzr should create the .~2~ directory
 
173
 
        transport.mkdir(backup_dir1)
 
174
 
        (out, err) = self.run_bzr(
 
175
 
            ['upgrade', '--format=metaweave', url])
 
176
 
        self.assertEqualDiff("""starting upgrade of %s
 
177
 
making backup of %s.bzr
 
179
 
starting upgrade from format 5 to 6
 
180
 
adding prefixes to weaves
 
181
 
adding prefixes to revision-store
 
182
 
starting upgrade from format 6 to metadir
 
184
 
""" % (url, url, url, backup_dir2), out)
 
185
 
        self.assertEqualDiff("", err)
 
186
 
        self.assertTrue(isinstance(
 
187
 
            bzrdir.BzrDir.open(self.get_url('format_5_branch'))._format,
 
188
 
            bzrdir.BzrDirMetaFormat1))
 
189
 
        self.assertTrue(transport.has(backup_dir2))
 
 
153
        self.run_bzr('init-repository', '--format=metaweave', 'repo')
 
 
154
        self.run_bzr('upgrade', '--format=knit', 'repo')
 
191
157
class SFTPTests(TestCaseWithSFTPServer):
 
192
158
    """Tests for upgrade over sftp."""
 
 
161
        super(SFTPTests, self).setUp()
 
 
162
        self.old_ui_factory = ui.ui_factory
 
 
163
        self.addCleanup(self.restoreDefaults)
 
 
165
        ui.ui_factory = TestUIFactory()
 
 
167
    def restoreDefaults(self):
 
 
168
        ui.ui_factory = self.old_ui_factory
 
194
170
    def test_upgrade_url(self):
 
195
 
        self.run_bzr('init --format=weave')
 
 
171
        self.run_bzr('init', '--format=weave')
 
196
172
        t = get_transport(self.get_url())
 
198
 
        out, err = self.run_bzr(['upgrade', '--format=knit', url])
 
199
 
        backup_dir = 'backup.bzr.~1~'
 
 
174
        out, err = self.run_bzr('upgrade', '--format=knit', url)
 
200
175
        self.assertEqualDiff("""starting upgrade of %s
 
201
 
making backup of %s.bzr
 
 
176
making backup of tree history
 
 
177
%s.bzr has been backed up to %s.bzr.backup
 
 
178
if conversion fails, you can move this directory back to .bzr
 
 
179
if it succeeds, you can remove this directory if you wish
 
203
180
starting upgrade from format 6 to metadir
 
204
181
starting repository conversion
 
205
182
repository converted
 
207
 
""" % (url, url, url,backup_dir), out)
 
 
184
""" % (url, url, url), out)
 
208
185
        self.assertEqual('', err)
 
211
 
class UpgradeRecommendedTests(TestCaseWithTransport):
 
 
188
class UpgradeRecommendedTests(TestCaseInTempDir):
 
213
190
    def test_recommend_upgrade_wt4(self):
 
214
191
        # using a deprecated format gives a warning
 
215
 
        self.run_bzr('init --knit a')
 
216
 
        out, err = self.run_bzr('status a')
 
 
192
        self.run_bzr('init', '--knit', 'a')
 
 
193
        out, err = self.run_bzr('status', 'a')
 
217
194
        self.assertContainsRe(err, 'bzr upgrade .*[/\\\\]a')
 
219
196
    def test_no_upgrade_recommendation_from_bzrdir(self):
 
220
197
        # we should only get a recommendation to upgrade when we're accessing
 
221
198
        # the actual workingtree, not when we only open a bzrdir that contains
 
222
199
        # an old workngtree
 
223
 
        self.run_bzr('init --knit a')
 
224
 
        out, err = self.run_bzr('revno a')
 
 
200
        self.run_bzr('init', '--knit', 'a')
 
 
201
        out, err = self.run_bzr('checkout', 'a', 'b')
 
225
202
        if err.find('upgrade') > -1:
 
226
203
            self.fail("message shouldn't suggest upgrade:\n%s" % err)
 
228
 
    def test_upgrade_shared_repo(self):
 
229
 
        repo = self.make_repository('repo', format='2a', shared=True)
 
230
 
        branch = self.make_branch_and_tree('repo/branch', format="pack-0.92")
 
231
 
        self.get_transport('repo/branch/.bzr/repository').delete_tree('.')
 
232
 
        out, err = self.run_bzr(['upgrade'], working_dir='repo/branch')