567
567
# and in the worst case, use bzrlib.user_encoding
568
568
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
571
class TestSetUnsetEnv(TestCase):
572
"""Test updating the environment"""
575
super(TestSetUnsetEnv, self).setUp()
577
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
578
'Environment was not cleaned up properly.'
579
' Variable BZR_TEST_ENV_VAR should not exist.')
581
if 'BZR_TEST_ENV_VAR' in os.environ:
582
del os.environ['BZR_TEST_ENV_VAR']
584
self.addCleanup(cleanup)
587
"""Test that we can set an env variable"""
588
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
589
self.assertEqual(None, old)
590
self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
592
def test_double_set(self):
593
"""Test that we get the old value out"""
594
osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
595
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
596
self.assertEqual('foo', old)
597
self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
599
def test_unicode(self):
600
"""Environment can only contain plain strings
602
So Unicode strings must be encoded.
604
# Try a few different characters, to see if we can get
605
# one that will be valid in the user_encoding
606
possible_vals = [u'm\xb5', u'\xe1', u'\u0410']
607
for uni_val in possible_vals:
609
env_val = uni_val.encode(bzrlib.user_encoding)
610
except UnicodeEncodeError:
611
# Try a different character
616
raise TestSkipped('Cannot find a unicode character that works in'
617
' encoding %s' % (bzrlib.user_encoding,))
619
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
620
self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
622
def test_unset(self):
623
"""Test that passing None will remove the env var"""
624
osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
625
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
626
self.assertEqual('foo', old)
627
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
628
self.failIf('BZR_TEST_ENV_VAR' in os.environ)