/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

Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.

This is used to replace various ad hoc implementations of the same logic,
notably the version used in registry's _LazyObjectGetter which had a bug when
getting a module without also getting a member.  And of course, this new
function has unit tests, unlike the replaced code.

This also adds a KnownHooksRegistry subclass to provide a more natural home for
some other logic.

I'm not thrilled about the name of the new module or the new functions, but it's
hard to think of good names for such generic functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
from bzrlib import (
29
29
    errors,
 
30
    lazy_regex,
30
31
    osutils,
 
32
    symbol_versioning,
31
33
    tests,
32
34
    trace,
33
35
    win32utils,
184
186
        shape = sorted(os.listdir('.'))
185
187
        self.assertEquals(['A', 'B'], shape)
186
188
 
187
 
    def test_rename_error(self):
188
 
        # We wrap os.rename to make it give an error including the filenames
189
 
        # https://bugs.launchpad.net/bzr/+bug/491763
190
 
        err = self.assertRaises(OSError, osutils.rename,
191
 
            'nonexistent', 'target')
192
 
        self.assertContainsString(str(err), 'nonexistent')
193
 
 
194
189
 
195
190
class TestRandChars(tests.TestCase):
196
191
 
868
863
        self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
869
864
        # relative path
870
865
        cwd = osutils.getcwd().rstrip('/')
871
 
        drive = osutils._nt_splitdrive(cwd)[0]
 
866
        drive = osutils.ntpath.splitdrive(cwd)[0]
872
867
        self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
873
868
        self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
874
869
        # unicode path
1071
1066
        self.assertExpectedBlocks(expected_dirblocks[1:], result)
1072
1067
 
1073
1068
    def test_walkdirs_os_error(self):
1074
 
        # <https://bugs.edge.launchpad.net/bzr/+bug/338653>
 
1069
        # <https://bugs.launchpad.net/bzr/+bug/338653>
1075
1070
        # Pyrex readdir didn't raise useful messages if it had an error
1076
1071
        # reading the directory
1077
1072
        if sys.platform == 'win32':
1090
1085
        # Ensure the message contains the file name
1091
1086
        self.assertContainsRe(str(e), "\./test-unreadable")
1092
1087
 
 
1088
 
 
1089
    def test_walkdirs_encoding_error(self):
 
1090
        # <https://bugs.launchpad.net/bzr/+bug/488519>
 
1091
        # walkdirs didn't raise a useful message when the filenames
 
1092
        # are not using the filesystem's encoding
 
1093
 
 
1094
        # require a bytestring based filesystem
 
1095
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1096
 
 
1097
        tree = [
 
1098
            '.bzr',
 
1099
            '0file',
 
1100
            '1dir/',
 
1101
            '1dir/0file',
 
1102
            '1dir/1dir/',
 
1103
            '1file'
 
1104
            ]
 
1105
 
 
1106
        self.build_tree(tree)
 
1107
 
 
1108
        # rename the 1file to a latin-1 filename
 
1109
        os.rename("./1file", "\xe8file")
 
1110
 
 
1111
        self._save_platform_info()
 
1112
        win32utils.winver = None # Avoid the win32 detection code
 
1113
        osutils._fs_enc = 'UTF-8'
 
1114
 
 
1115
        # this should raise on error
 
1116
        def attempt():
 
1117
            for dirdetail, dirblock in osutils.walkdirs('.'):
 
1118
                pass
 
1119
 
 
1120
        self.assertRaises(errors.BadFilenameEncoding, attempt)
 
1121
 
1093
1122
    def test__walkdirs_utf8(self):
1094
1123
        tree = [
1095
1124
            '.bzr',
1678
1707
 
1679
1708
class TestReCompile(tests.TestCase):
1680
1709
 
 
1710
    def _deprecated_re_compile_checked(self, *args, **kwargs):
 
1711
        return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
 
1712
            osutils.re_compile_checked, *args, **kwargs)
 
1713
 
1681
1714
    def test_re_compile_checked(self):
1682
 
        r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
 
1715
        r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1683
1716
        self.assertTrue(r.match('aaaa'))
1684
1717
        self.assertTrue(r.match('aAaA'))
1685
1718
 
1686
1719
    def test_re_compile_checked_error(self):
1687
1720
        # like https://bugs.launchpad.net/bzr/+bug/251352
 
1721
 
 
1722
        # Due to possible test isolation error, re.compile is not lazy at
 
1723
        # this point. We re-install lazy compile.
 
1724
        lazy_regex.install_lazy_compile()
1688
1725
        err = self.assertRaises(
1689
1726
            errors.BzrCommandError,
1690
 
            osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
 
1727
            self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1691
1728
        self.assertEqual(
1692
 
            "Invalid regular expression in test case: '*': "
1693
 
            "nothing to repeat",
 
1729
            'Invalid regular expression in test case: '
 
1730
            '"*" nothing to repeat',
1694
1731
            str(err))
1695
1732
 
1696
1733
 
1917
1954
 
1918
1955
class TestTerminalWidth(tests.TestCase):
1919
1956
 
 
1957
    def setUp(self):
 
1958
        tests.TestCase.setUp(self)
 
1959
        self._orig_terminal_size_state = osutils._terminal_size_state
 
1960
        self._orig_first_terminal_size = osutils._first_terminal_size
 
1961
        self.addCleanup(self.restore_osutils_globals)
 
1962
        osutils._terminal_size_state = 'no_data'
 
1963
        osutils._first_terminal_size = None
 
1964
 
 
1965
    def restore_osutils_globals(self):
 
1966
        osutils._terminal_size_state = self._orig_terminal_size_state
 
1967
        osutils._first_terminal_size = self._orig_first_terminal_size
 
1968
 
1920
1969
    def replace_stdout(self, new):
1921
1970
        self.overrideAttr(sys, 'stdout', new)
1922
1971
 
2018
2067
        self.assertEquals(self.path, 'test_file')
2019
2068
        self.assertEquals(self.uid, s.st_uid)
2020
2069
        self.assertEquals(self.gid, s.st_gid)
 
2070
 
 
2071
class TestGetuserUnicode(tests.TestCase):
 
2072
 
 
2073
    def test_ascii_user(self):
 
2074
        osutils.set_or_unset_env('LOGNAME', 'jrandom')
 
2075
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
 
2076
 
 
2077
    def test_unicode_user(self):
 
2078
        ue = osutils.get_user_encoding()
 
2079
        osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
 
2080
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())