~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

Add a new method ``Tree.revision_tree`` which allows access to cached
trees for arbitrary revisions. This allows the in development dirstate
tree format to provide access to the callers to cached copies of 
inventory data which are cheaper to access than inventories from the
repository. (Robert Collins, Martin Pool)

Show diffs side-by-side

added added

removed removed

Lines of Context:
529
529
        # and in the worst case, use bzrlib.user_encoding
530
530
        self.assertEqual('user_encoding', osutils.get_terminal_encoding())
531
531
 
 
532
 
 
533
class TestSetUnsetEnv(TestCase):
 
534
    """Test updating the environment"""
 
535
 
 
536
    def setUp(self):
 
537
        super(TestSetUnsetEnv, self).setUp()
 
538
 
 
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.')
 
542
        def cleanup():
 
543
            if 'BZR_TEST_ENV_VAR' in os.environ:
 
544
                del os.environ['BZR_TEST_ENV_VAR']
 
545
 
 
546
        self.addCleanup(cleanup)
 
547
 
 
548
    def test_set(self):
 
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'))
 
553
 
 
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'))
 
560
 
 
561
    def test_unicode(self):
 
562
        """Environment can only contain plain strings
 
563
        
 
564
        So Unicode strings must be encoded.
 
565
        """
 
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:
 
570
            try:
 
571
                env_val = uni_val.encode(bzrlib.user_encoding)
 
572
            except UnicodeEncodeError:
 
573
                # Try a different character
 
574
                pass
 
575
            else:
 
576
                break
 
577
        else:
 
578
            raise TestSkipped('Cannot find a unicode character that works in'
 
579
                              ' encoding %s' % (bzrlib.user_encoding,))
 
580
 
 
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'))
 
583
 
 
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)
 
591