529
529
# and in the worst case, use bzrlib.user_encoding
530
530
self.assertEqual('user_encoding', osutils.get_terminal_encoding())
533
class TestSetUnsetEnv(TestCase):
534
"""Test updating the environment"""
537
super(TestSetUnsetEnv, self).setUp()
539
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'),
540
'Environment was not cleaned up properly.'
541
' Variable BZR_TEST_ENV_VAR should not exist.')
543
if 'BZR_TEST_ENV_VAR' in os.environ:
544
del os.environ['BZR_TEST_ENV_VAR']
546
self.addCleanup(cleanup)
549
"""Test that we can set an env variable"""
550
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
551
self.assertEqual(None, old)
552
self.assertEqual('foo', os.environ.get('BZR_TEST_ENV_VAR'))
554
def test_double_set(self):
555
"""Test that we get the old value out"""
556
osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
557
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'bar')
558
self.assertEqual('foo', old)
559
self.assertEqual('bar', os.environ.get('BZR_TEST_ENV_VAR'))
561
def test_unicode(self):
562
"""Environment can only contain plain strings
564
So Unicode strings must be encoded.
566
# Try a few different characters, to see if we can get
567
# one that will be valid in the user_encoding
568
possible_vals = [u'm\xb5', u'\xe1', u'\u0410']
569
for uni_val in possible_vals:
571
env_val = uni_val.encode(bzrlib.user_encoding)
572
except UnicodeEncodeError:
573
# Try a different character
578
raise TestSkipped('Cannot find a unicode character that works in'
579
' encoding %s' % (bzrlib.user_encoding,))
581
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', uni_val)
582
self.assertEqual(env_val, os.environ.get('BZR_TEST_ENV_VAR'))
584
def test_unset(self):
585
"""Test that passing None will remove the env var"""
586
osutils.set_or_unset_env('BZR_TEST_ENV_VAR', 'foo')
587
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
588
self.assertEqual('foo', old)
589
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
590
self.failIf('BZR_TEST_ENV_VAR' in os.environ)