def assertDebugFlags(self, expected_flags, conf_bytes): conf = config.GlobalStack() conf.store._load_from_string('[DEFAULT]\n' + conf_bytes) conf.store.save() self.overrideAttr(debug, 'debug_flags', set()) debug.set_debug_flags_from_config() self.assertEqual(set(expected_flags), debug.debug_flags)
def test_lock_without_email(self): global_config = config.GlobalStack() # Intentionally has no email address global_config.set('email', 'User Identity') ld1 = self.get_lock() ld1.create() ld1.lock_write() ld1.unlock()
def test_repository_deprecation_warning_suppressed_global(self): """Old formats give a warning""" conf = config.GlobalStack() conf.set('suppress_warnings', 'format_deprecation') self.make_obsolete_repo('foo') self.enable_deprecation_warning() out, err = self.run_bzr('status', working_dir='foo') self.check_warning(False)
def test_output_encoding_configuration(self): enc = fixtures.generate_unicode_encodings().next() config.GlobalStack().set('output_encoding', enc) ui = tests.TestUIFactory(stdin=None, stdout=tests.StringIOWrapper(), stderr=tests.StringIOWrapper()) output = ui.make_output_stream() self.assertEqual(output.encoding, enc)
def set_debug_flags_from_config(): """Turn on debug flags based on the global configuration""" from bzrlib import config c = config.GlobalStack() for f in c.get('debug_flags'): debug_flags.add(f)
def get_username_for_lock_info(): """Get a username suitable for putting into a lock. It's ok if what's written here is not a proper email address as long as it gives some clue who the user is. """ try: return config.GlobalStack().get('email') except errors.NoWhoami: return osutils.getuser_unicode()
def make_multiple_pending_tree(self): config.GlobalStack().set('email', 'Joe Foo <*****@*****.**>') tree = self.make_branch_and_tree('a') tree.commit('commit 1', timestamp=1196796819, timezone=0) tree2 = tree.bzrdir.clone('b').open_workingtree() tree.commit('commit 2', timestamp=1196796819, timezone=0) tree2.commit('commit 2b', timestamp=1196796819, timezone=0) tree3 = tree2.bzrdir.clone('c').open_workingtree() tree2.commit('commit 3b', timestamp=1196796819, timezone=0) tree3.commit('commit 3c', timestamp=1196796819, timezone=0) tree.merge_from_branch(tree2.branch) tree.merge_from_branch(tree3.branch, force=True) return tree
def test_command_see_config_overrides(self): def run(cmd): # We override the run() command method so we can observe the # overrides from inside. c = config.GlobalStack() self.assertEqual('12', c.get('xx')) self.assertEqual('foo', c.get('yy')) self.overrideAttr(builtins.cmd_rocks, 'run', run) self.run_bzr(['rocks', '-Oxx=12', '-Oyy=foo']) c = config.GlobalStack() # Ensure that we don't leak outside of the command self.assertEqual(None, c.get('xx')) self.assertEqual(None, c.get('yy'))
def _get_current_locale(): if not os.environ.get('LANGUAGE'): from bzrlib import config lang = config.GlobalStack().get('language') if lang: os.environ['LANGUAGE'] = lang return lang if sys.platform == 'win32': _check_win32_locale() for i in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): lang = os.environ.get(i) if lang: return lang return None
def make_multiple_pending_tree(self): config.GlobalStack().set('email', 'Bilbo Baggins <*****@*****.**>') tree = self.make_branch_and_tree('a') tree.commit('Initial checkin.', timestamp=1230912900, timezone=0) tree2 = tree.bzrdir.clone('b').open_workingtree() tree.commit('Minor tweak.', timestamp=1231977840, timezone=0) tree2.commit('Feature X work.', timestamp=1233186240, timezone=0) tree3 = tree2.bzrdir.clone('c').open_workingtree() tree2.commit('Feature X finished.', timestamp=1233187680, timezone=0) tree3.commit('Feature Y, based on initial X work.', timestamp=1233285960, timezone=0) tree.merge_from_branch(tree2.branch) tree.merge_from_branch(tree3.branch, force=True) return tree
def test_gather_user_credentials_prompts(self): service = LaunchpadService() self.assertIs(None, service.registrant_password) g_conf = config.GlobalStack() g_conf.set('email', 'Test User <*****@*****.**>') g_conf.store.save() stdout = tests.StringIOWrapper() stderr = tests.StringIOWrapper() ui.ui_factory = tests.TestUIFactory(stdin='userpass\n', stdout=stdout, stderr=stderr) self.assertIs(None, service.registrant_password) service.gather_user_credentials() self.assertEqual('*****@*****.**', service.registrant_email) self.assertEqual('userpass', service.registrant_password) self.assertEquals('', stdout.getvalue()) self.assertContainsRe(stderr.getvalue(), 'launchpad.net password for test@user\\.com')
def test__get_editor(self): self.overrideEnv('BZR_EDITOR', 'bzr_editor') self.overrideEnv('VISUAL', 'visual') self.overrideEnv('EDITOR', 'editor') conf = config.GlobalStack() conf.store._load_from_string('[DEFAULT]\neditor = config_editor\n') conf.store.save() editors = list(msgeditor._get_editor()) editors = [editor for (editor, cfg_src) in editors] self.assertEqual(['bzr_editor', 'config_editor', 'visual', 'editor'], editors[:4]) if sys.platform == 'win32': self.assertEqual(['wordpad.exe', 'notepad.exe'], editors[4:]) else: self.assertEqual(['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe'], editors[4:])
def test_bzr_serve_supports_configurable_timeout(self): gs = config.GlobalStack() gs.set('serve.client_timeout', 0.2) # Save the config as the subprocess will use it gs.store.save() process, url = self.start_server_port() self.build_tree_contents([('a_file', 'contents\n')]) # We can connect and issue a request t = transport.get_transport_from_url(url) self.assertEqual('contents\n', t.get_bytes('a_file')) # However, if we just wait for more content from the server, it will # eventually disconnect us. m = t.get_smart_medium() m.read_bytes(1) # Now, we wait for timeout to trigger err = process.stderr.readline() self.assertEqual( 'Connection Timeout: disconnecting client after 0.2 seconds\n', err) self.assertServerFinishesCleanly(process)
def _get_editor(): """Return a sequence of possible editor binaries for the current platform""" try: yield os.environ["BZR_EDITOR"], '$BZR_EDITOR' except KeyError: pass e = config.GlobalStack().get('editor') if e is not None: yield e, config.config_filename() for varname in 'VISUAL', 'EDITOR': if varname in os.environ: yield os.environ[varname], '$' + varname if sys.platform == 'win32': for editor in 'wordpad.exe', 'notepad.exe': yield editor, None else: for editor in ['/usr/bin/editor', 'vi', 'pico', 'nano', 'joe']: yield editor, None
def test_auto_break_stale_lock(self): """Locks safely known to be stale are just cleaned up. This generates a warning but no other user interaction. """ self.overrideAttr(lockdir, 'get_host_name', lambda: 'aproperhostname') # This is off by default at present; see the discussion in the bug. # If you change the default, don't forget to update the docs. config.GlobalStack().set('locks.steal_dead', True) # Create a lock pretending to come from a different nonexistent # process on the same machine. l1 = LockDir(self.get_transport(), 'a', extra_holder_info={'pid': '12312313'}) token_1 = l1.attempt_lock() l2 = LockDir(self.get_transport(), 'a') token_2 = l2.attempt_lock() # l1 will notice its lock was stolen. self.assertRaises(errors.LockBroken, l1.unlock) l2.unlock()
def test_pending_with_ghosts(self): """Test when a pending merge's ancestry includes ghosts.""" config.GlobalStack().set('email', 'Joe Foo <*****@*****.**>') tree = self.make_branch_and_tree('a') tree.commit('empty commit') tree2 = tree.bzrdir.clone('b').open_workingtree() tree2.commit('a non-ghost', timestamp=1196796819, timezone=0) tree2.add_parent_tree_id('a-ghost-revision') tree2.commit('commit with ghost', timestamp=1196796819, timezone=0) tree2.commit('another non-ghost', timestamp=1196796819, timezone=0) tree.merge_from_branch(tree2.branch) tree.lock_read() self.addCleanup(tree.unlock) output = StringIO() show_pending_merges(tree, output, verbose=True) self.assertEqualDiff('pending merges:\n' ' Joe Foo 2007-12-04 another non-ghost\n' ' Joe Foo 2007-12-04 [merge] commit with ghost\n' ' (ghost) a-ghost-revision\n' ' Joe Foo 2007-12-04 a non-ghost\n', output.getvalue())
def test_gather_user_credentials_from_auth_conf(self): auth_path = config.authentication_config_filename() service = LaunchpadService() g_conf = config.GlobalStack() g_conf.set('email', 'Test User <*****@*****.**>') g_conf.store.save() # FIXME: auth_path base dir exists only because bazaar.conf has just # been saved, brittle... -- vila 20120731 f = open(auth_path, 'wb') try: scheme, hostinfo = urlparse.urlsplit(service.service_url)[:2] f.write('[section]\n' 'scheme=%s\n' 'host=%s\n' '[email protected]\n' 'password=testpass\n' % (scheme, hostinfo)) finally: f.close() self.assertIs(None, service.registrant_password) service.gather_user_credentials() self.assertEqual('*****@*****.**', service.registrant_email) self.assertEqual('testpass', service.registrant_password)
def test_whoami_remote_directory(self): """Test --directory option with a remote directory.""" wt = self.make_branch_and_tree('subdir') self.set_branch_email(wt.branch, 'Branch Identity <*****@*****.**>') url = self.get_readonly_url() + '/subdir' self.assertWhoAmI('Branch Identity <*****@*****.**>', '--directory', url) url = self.get_url('subdir') self.run_bzr([ 'whoami', '--directory', url, '--branch', 'Changed Identity <*****@*****.**>' ]) # The identity has been set in the branch config (but not the global # config) c = branch.Branch.open(url).get_config_stack() self.assertEqual('Changed Identity <*****@*****.**>', c.get('email')) # Ensuring that the value does not come from the bazaar.conf file # itself requires some isolation setup self.overrideEnv('BZR_EMAIL', None) self.overrideEnv('EMAIL', None) self.overrideAttr(config, '_auto_user_id', lambda: (None, None)) global_conf = config.GlobalStack() self.assertRaises(errors.NoWhoami, global_conf.get, 'email')
def run(cmd): # We override the run() command method so we can observe the # overrides from inside. c = config.GlobalStack() self.assertEqual('12', c.get('xx')) self.assertEqual('foo', c.get('yy'))
def get_config(self): """Get the configuration that governs this lockdir.""" # XXX: This really should also use the locationconfig at least, but # that seems a bit hard to hook up at the moment. -- mbp 20110329 # FIXME: The above is still true ;) -- vila 20110811 return config.GlobalStack()