1# Copyright (C) 2006-2011 Canonical Ltd 2# 3# This program is free software; you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation; either version 2 of the License, or 6# (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program; if not, write to the Free Software 15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 16 17 18"""Test 'brz init'""" 19 20import os 21import re 22 23from breezy import ( 24 branch as _mod_branch, 25 config as _mod_config, 26 osutils, 27 urlutils, 28 ) 29from breezy.bzr.bzrdir import BzrDirMetaFormat1 30from breezy.tests import TestSkipped 31from breezy.tests import TestCaseWithTransport 32from breezy.tests.test_sftp_transport import TestCaseWithSFTPServer 33from breezy.workingtree import WorkingTree 34 35 36class TestInit(TestCaseWithTransport): 37 38 def setUp(self): 39 super(TestInit, self).setUp() 40 self._default_label = '2a' 41 42 def test_init_with_format(self): 43 # Verify brz init --format constructs something plausible 44 t = self.get_transport() 45 self.run_bzr('init --format default') 46 self.assertIsDirectory('.bzr', t) 47 self.assertIsDirectory('.bzr/checkout', t) 48 self.assertIsDirectory('.bzr/checkout/lock', t) 49 50 def test_init_format_2a(self): 51 """Smoke test for constructing a format 2a repository.""" 52 out, err = self.run_bzr('init --format=2a') 53 self.assertEqual("""Created a standalone tree (format: 2a)\n""", 54 out) 55 self.assertEqual('', err) 56 57 def test_init_format_bzr(self): 58 """Smoke test for constructing a format with the 'bzr' alias.""" 59 out, err = self.run_bzr('init --format=bzr') 60 self.assertEqual( 61 "Created a standalone tree (format: %s)\n" % self._default_label, 62 out) 63 self.assertEqual('', err) 64 65 def test_init_colocated(self): 66 """Smoke test for constructing a colocated branch.""" 67 out, err = self.run_bzr( 68 'init --format=development-colo file:,branch=abranch') 69 self.assertEqual("""Created a standalone tree (format: development-colo)\n""", 70 out) 71 self.assertEqual('', err) 72 out, err = self.run_bzr('branches') 73 self.assertEqual(" abranch\n", out) 74 self.assertEqual('', err) 75 76 def test_init_at_repository_root(self): 77 # brz init at the root of a repository should create a branch 78 # and working tree even when creation of working trees is disabled. 79 t = self.get_transport() 80 t.mkdir('repo') 81 format = BzrDirMetaFormat1() 82 newdir = format.initialize(t.abspath('repo')) 83 repo = newdir.create_repository(shared=True) 84 repo.set_make_working_trees(False) 85 out, err = self.run_bzr('init repo') 86 self.assertEqual("""Created a repository tree (format: %s) 87Using shared repository: %s 88""" % (self._default_label, urlutils.local_path_from_url( 89 repo.controldir.root_transport.external_url())), out) 90 cwd = osutils.getcwd() 91 self.assertEndsWith(out, cwd + '/repo/\n') 92 self.assertEqual('', err) 93 newdir.open_branch() 94 newdir.open_workingtree() 95 96 def test_init_branch(self): 97 out, err = self.run_bzr('init') 98 self.assertEqual("Created a standalone tree (format: %s)\n" % ( 99 self._default_label,), out) 100 self.assertEqual('', err) 101 102 # Can it handle subdirectories of branches too ? 103 out, err = self.run_bzr('init subdir1') 104 self.assertEqual("Created a standalone tree (format: %s)\n" % ( 105 self._default_label,), out) 106 self.assertEqual('', err) 107 WorkingTree.open('subdir1') 108 109 self.run_bzr_error(['Parent directory of subdir2/nothere does not exist'], 110 'init subdir2/nothere') 111 out, err = self.run_bzr('init subdir2/nothere', retcode=3) 112 self.assertEqual('', out) 113 114 os.mkdir('subdir2') 115 out, err = self.run_bzr('init subdir2') 116 self.assertEqual("Created a standalone tree (format: %s)\n" % ( 117 self._default_label,), out) 118 self.assertEqual('', err) 119 # init an existing branch. 120 out, err = self.run_bzr('init subdir2', retcode=3) 121 self.assertEqual('', out) 122 self.assertTrue(err.startswith('brz: ERROR: Already a branch:')) 123 124 def test_init_branch_quiet(self): 125 out, err = self.run_bzr('init -q') 126 self.assertEqual('', out) 127 self.assertEqual('', err) 128 129 def test_init_existing_branch(self): 130 self.run_bzr('init') 131 out, err = self.run_bzr('init', retcode=3) 132 self.assertContainsRe(err, 'Already a branch') 133 # don't suggest making a checkout, there's already a working tree 134 self.assertFalse(re.search(r'checkout', err)) 135 136 def test_init_existing_without_workingtree(self): 137 # make a repository 138 repo = self.make_repository('.', shared=True) 139 repo.set_make_working_trees(False) 140 # make a branch; by default without a working tree 141 self.run_bzr('init subdir') 142 # fail 143 out, err = self.run_bzr('init subdir', retcode=3) 144 # suggests using checkout 145 self.assertContainsRe(err, 146 'ontains a branch.*but no working tree.*checkout') 147 148 def test_no_defaults(self): 149 """Init creates no default ignore rules.""" 150 self.run_bzr('init') 151 self.assertFalse(os.path.exists('.bzrignore')) 152 153 def test_init_unicode(self): 154 # Make sure getcwd can handle unicode filenames 155 try: 156 os.mkdir(u'mu-\xb5') 157 except UnicodeError: 158 raise TestSkipped("Unable to create Unicode filename") 159 # try to init unicode dir 160 self.run_bzr(['init', '-q', u'mu-\xb5']) 161 162 def create_simple_tree(self): 163 tree = self.make_branch_and_tree('tree') 164 self.build_tree(['tree/a']) 165 tree.add(['a'], [b'a-id']) 166 tree.commit('one', rev_id=b'r1') 167 return tree 168 169 def test_init_create_prefix(self): 170 """'brz init --create-prefix; will create leading directories.""" 171 tree = self.create_simple_tree() 172 173 self.run_bzr_error(['Parent directory of ../new/tree does not exist'], 174 'init ../new/tree', working_dir='tree') 175 self.run_bzr('init ../new/tree --create-prefix', working_dir='tree') 176 self.assertPathExists('new/tree/.bzr') 177 178 def test_init_default_format_option(self): 179 """brz init should read default format from option default_format""" 180 g_store = _mod_config.GlobalStore() 181 g_store._load_from_string(b''' 182[DEFAULT] 183default_format = 1.9 184''') 185 g_store.save() 186 out, err = self.run_bzr_subprocess('init') 187 self.assertContainsRe(out, b'1.9') 188 189 def test_init_no_tree(self): 190 """'brz init --no-tree' creates a branch with no working tree.""" 191 out, err = self.run_bzr('init --no-tree') 192 self.assertStartsWith(out, 'Created a standalone branch') 193 194 195class TestSFTPInit(TestCaseWithSFTPServer): 196 197 def test_init(self): 198 # init on a remote url should succeed. 199 out, err = self.run_bzr(['init', '--format=pack-0.92', self.get_url()]) 200 self.assertEqual(out, 201 """Created a standalone branch (format: pack-0.92)\n""") 202 self.assertEqual('', err) 203 204 def test_init_existing_branch(self): 205 # when there is already a branch present, make mention 206 self.make_branch('.') 207 208 # rely on SFTPServer get_url() pointing at '.' 209 out, err = self.run_bzr_error(['Already a branch'], 210 ['init', self.get_url()]) 211 212 # make sure using 'brz checkout' is not suggested 213 # for remote locations missing a working tree 214 self.assertFalse(re.search(r'use brz checkout', err)) 215 216 def test_init_existing_branch_with_workingtree(self): 217 # don't distinguish between the branch having a working tree or not 218 # when the branch itself is remote. 219 self.make_branch_and_tree('.') 220 221 # rely on SFTPServer get_url() pointing at '.' 222 self.run_bzr_error(['Already a branch'], ['init', self.get_url()]) 223 224 def test_init_append_revisions_only(self): 225 self.run_bzr('init --format=dirstate-tags normal_branch6') 226 branch = _mod_branch.Branch.open('normal_branch6') 227 self.assertEqual(None, branch.get_append_revisions_only()) 228 self.run_bzr( 229 'init --append-revisions-only --format=dirstate-tags branch6') 230 branch = _mod_branch.Branch.open('branch6') 231 self.assertEqual(True, branch.get_append_revisions_only()) 232 self.run_bzr_error(['cannot be set to append-revisions-only'], 233 'init --append-revisions-only --format=knit knit') 234 235 def test_init_without_username(self): 236 """Ensure init works if username is not set. 237 """ 238 # brz makes user specified whoami mandatory for operations 239 # like commit as whoami is recorded. init however is not so final 240 # and uses whoami only in a lock file. Without whoami the login name 241 # is used. This test is to ensure that init passes even when whoami 242 # is not available. 243 self.overrideEnv('EMAIL', None) 244 self.overrideEnv('BRZ_EMAIL', None) 245 out, err = self.run_bzr(['init', 'foo']) 246 self.assertEqual(err, '') 247 self.assertTrue(os.path.exists('foo')) 248