/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: John Arbash Meinel
  • Date: 2006-09-15 00:44:57 UTC
  • mfrom: (2009 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2050.
  • Revision ID: john@arbash-meinel.com-20060915004457-902cec0526a39337
[merge] bzr.dev 2009

Show diffs side-by-side

added added

removed removed

Lines of Context:
567
567
        # and in the worst case, use bzrlib.user_encoding
568
568
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
569
569
 
 
570
 
 
571
class TestSetUnsetEnv(TestCase):
 
572
    """Test updating the environment"""
 
573
 
 
574
    def setUp(self):
 
575
        super(TestSetUnsetEnv, self).setUp()
 
576
 
 
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.')
 
580
        def cleanup():
 
581
            if 'BZR_TEST_ENV_VAR' in os.environ:
 
582
                del os.environ['BZR_TEST_ENV_VAR']
 
583
 
 
584
        self.addCleanup(cleanup)
 
585
 
 
586
    def test_set(self):
 
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'))
 
591
 
 
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'))
 
598
 
 
599
    def test_unicode(self):
 
600
        """Environment can only contain plain strings
 
601
        
 
602
        So Unicode strings must be encoded.
 
603
        """
 
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:
 
608
            try:
 
609
                env_val = uni_val.encode(bzrlib.user_encoding)
 
610
            except UnicodeEncodeError:
 
611
                # Try a different character
 
612
                pass
 
613
            else:
 
614
                break
 
615
        else:
 
616
            raise TestSkipped('Cannot find a unicode character that works in'
 
617
                              ' encoding %s' % (bzrlib.user_encoding,))
 
618
 
 
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'))
 
621
 
 
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)
 
629