PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/bzr-2.5.1/bzrlib/tests/blackbox/test_init.py

#
Python | 236 lines | 219 code | 2 blank | 15 comment | 0 complexity | c940842b0bbaa05ce21dce8a6baae8ee MD5 | raw file
Possible License(s): GPL-2.0
  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. """Test 'bzr init'"""
  17. import os
  18. import re
  19. from bzrlib import (
  20. branch as _mod_branch,
  21. config as _mod_config,
  22. osutils,
  23. urlutils,
  24. )
  25. from bzrlib.bzrdir import BzrDirMetaFormat1
  26. from bzrlib.tests import TestSkipped
  27. from bzrlib.tests import TestCaseWithTransport
  28. from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
  29. from bzrlib.workingtree import WorkingTree
  30. class TestInit(TestCaseWithTransport):
  31. def setUp(self):
  32. TestCaseWithTransport.setUp(self)
  33. self._default_label = '2a'
  34. def test_init_with_format(self):
  35. # Verify bzr init --format constructs something plausible
  36. t = self.get_transport()
  37. self.run_bzr('init --format default')
  38. self.assertIsDirectory('.bzr', t)
  39. self.assertIsDirectory('.bzr/checkout', t)
  40. self.assertIsDirectory('.bzr/checkout/lock', t)
  41. def test_init_format_2a(self):
  42. """Smoke test for constructing a format 2a repository."""
  43. out, err = self.run_bzr('init --format=2a')
  44. self.assertEqual("""Created a standalone tree (format: 2a)\n""",
  45. out)
  46. self.assertEqual('', err)
  47. def test_init_colocated(self):
  48. """Smoke test for constructing a colocated branch."""
  49. out, err = self.run_bzr('init --format=development-colo file:,branch=abranch')
  50. self.assertEqual("""Created a lightweight checkout (format: development-colo)\n""",
  51. out)
  52. self.assertEqual('', err)
  53. out, err = self.run_bzr('branches')
  54. self.assertEqual(" abranch\n", out)
  55. self.assertEqual('', err)
  56. def test_init_at_repository_root(self):
  57. # bzr init at the root of a repository should create a branch
  58. # and working tree even when creation of working trees is disabled.
  59. t = self.get_transport()
  60. t.mkdir('repo')
  61. format = BzrDirMetaFormat1()
  62. newdir = format.initialize(t.abspath('repo'))
  63. repo = newdir.create_repository(shared=True)
  64. repo.set_make_working_trees(False)
  65. out, err = self.run_bzr('init repo')
  66. self.assertEqual("""Created a repository tree (format: %s)
  67. Using shared repository: %s
  68. """ % (self._default_label, urlutils.local_path_from_url(
  69. repo.bzrdir.root_transport.external_url())), out)
  70. cwd = osutils.getcwd()
  71. self.assertEndsWith(out, cwd + '/repo/\n')
  72. self.assertEqual('', err)
  73. newdir.open_branch()
  74. newdir.open_workingtree()
  75. def test_init_branch(self):
  76. out, err = self.run_bzr('init')
  77. self.assertEqual("Created a standalone tree (format: %s)\n" % (
  78. self._default_label,), out)
  79. self.assertEqual('', err)
  80. # Can it handle subdirectories of branches too ?
  81. out, err = self.run_bzr('init subdir1')
  82. self.assertEqual("Created a standalone tree (format: %s)\n" % (
  83. self._default_label,), out)
  84. self.assertEqual('', err)
  85. WorkingTree.open('subdir1')
  86. self.run_bzr_error(['Parent directory of subdir2/nothere does not exist'],
  87. 'init subdir2/nothere')
  88. out, err = self.run_bzr('init subdir2/nothere', retcode=3)
  89. self.assertEqual('', out)
  90. os.mkdir('subdir2')
  91. out, err = self.run_bzr('init subdir2')
  92. self.assertEqual("Created a standalone tree (format: %s)\n" % (
  93. self._default_label,), out)
  94. self.assertEqual('', err)
  95. # init an existing branch.
  96. out, err = self.run_bzr('init subdir2', retcode=3)
  97. self.assertEqual('', out)
  98. self.assertTrue(err.startswith('bzr: ERROR: Already a branch:'))
  99. def test_init_branch_quiet(self):
  100. out, err = self.run_bzr('init -q')
  101. self.assertEqual('', out)
  102. self.assertEqual('', err)
  103. def test_init_existing_branch(self):
  104. self.run_bzr('init')
  105. out, err = self.run_bzr('init', retcode=3)
  106. self.assertContainsRe(err, 'Already a branch')
  107. # don't suggest making a checkout, there's already a working tree
  108. self.assertFalse(re.search(r'checkout', err))
  109. def test_init_existing_without_workingtree(self):
  110. # make a repository
  111. repo = self.make_repository('.', shared=True)
  112. repo.set_make_working_trees(False)
  113. # make a branch; by default without a working tree
  114. self.run_bzr('init subdir')
  115. # fail
  116. out, err = self.run_bzr('init subdir', retcode=3)
  117. # suggests using checkout
  118. self.assertContainsRe(err,
  119. 'ontains a branch.*but no working tree.*checkout')
  120. def test_no_defaults(self):
  121. """Init creates no default ignore rules."""
  122. self.run_bzr('init')
  123. self.assertFalse(os.path.exists('.bzrignore'))
  124. def test_init_unicode(self):
  125. # Make sure getcwd can handle unicode filenames
  126. try:
  127. os.mkdir(u'mu-\xb5')
  128. except UnicodeError:
  129. raise TestSkipped("Unable to create Unicode filename")
  130. # try to init unicode dir
  131. self.run_bzr(['init', '-q', u'mu-\xb5'])
  132. def create_simple_tree(self):
  133. tree = self.make_branch_and_tree('tree')
  134. self.build_tree(['tree/a'])
  135. tree.add(['a'], ['a-id'])
  136. tree.commit('one', rev_id='r1')
  137. return tree
  138. def test_init_create_prefix(self):
  139. """'bzr init --create-prefix; will create leading directories."""
  140. tree = self.create_simple_tree()
  141. self.run_bzr_error(['Parent directory of ../new/tree does not exist'],
  142. 'init ../new/tree', working_dir='tree')
  143. self.run_bzr('init ../new/tree --create-prefix', working_dir='tree')
  144. self.assertPathExists('new/tree/.bzr')
  145. def test_init_default_format_option(self):
  146. """bzr init should read default format from option default_format"""
  147. conf = _mod_config.GlobalConfig.from_string('''
  148. [DEFAULT]
  149. default_format = 1.9
  150. ''', save=True)
  151. out, err = self.run_bzr_subprocess('init')
  152. self.assertContainsRe(out, '1.9')
  153. def test_init_no_tree(self):
  154. """'bzr init --no-tree' creates a branch with no working tree."""
  155. out, err = self.run_bzr('init --no-tree')
  156. self.assertStartsWith(out, 'Created a standalone branch')
  157. class TestSFTPInit(TestCaseWithSFTPServer):
  158. def test_init(self):
  159. # init on a remote url should succeed.
  160. out, err = self.run_bzr(['init', '--pack-0.92', self.get_url()])
  161. self.assertEqual(out,
  162. """Created a standalone branch (format: pack-0.92)\n""")
  163. self.assertEqual('', err)
  164. def test_init_existing_branch(self):
  165. # when there is already a branch present, make mention
  166. self.make_branch('.')
  167. # rely on SFTPServer get_url() pointing at '.'
  168. out, err = self.run_bzr_error(['Already a branch'],
  169. ['init', self.get_url()])
  170. # make sure using 'bzr checkout' is not suggested
  171. # for remote locations missing a working tree
  172. self.assertFalse(re.search(r'use bzr checkout', err))
  173. def test_init_existing_branch_with_workingtree(self):
  174. # don't distinguish between the branch having a working tree or not
  175. # when the branch itself is remote.
  176. self.make_branch_and_tree('.')
  177. # rely on SFTPServer get_url() pointing at '.'
  178. self.run_bzr_error(['Already a branch'], ['init', self.get_url()])
  179. def test_init_append_revisions_only(self):
  180. self.run_bzr('init --dirstate-tags normal_branch6')
  181. branch = _mod_branch.Branch.open('normal_branch6')
  182. self.assertEqual(None, branch.get_append_revisions_only())
  183. self.run_bzr('init --append-revisions-only --dirstate-tags branch6')
  184. branch = _mod_branch.Branch.open('branch6')
  185. self.assertEqual(True, branch.get_append_revisions_only())
  186. self.run_bzr_error(['cannot be set to append-revisions-only'],
  187. 'init --append-revisions-only --knit knit')
  188. def test_init_without_username(self):
  189. """Ensure init works if username is not set.
  190. """
  191. # bzr makes user specified whoami mandatory for operations
  192. # like commit as whoami is recorded. init however is not so final
  193. # and uses whoami only in a lock file. Without whoami the login name
  194. # is used. This test is to ensure that init passes even when whoami
  195. # is not available.
  196. self.overrideEnv('EMAIL', None)
  197. self.overrideEnv('BZR_EMAIL', None)
  198. out, err = self.run_bzr(['init', 'foo'])
  199. self.assertEqual(err, '')
  200. self.assertTrue(os.path.exists('foo'))