/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: Martin Pool
  • Date: 2009-12-09 05:47:32 UTC
  • mfrom: (4879 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4880.
  • Revision ID: mbp@sourcefrog.net-20091209054732-7414e9uma23mfv6x
trivial merge of trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
 
54
54
UTF8DirReaderFeature = _UTF8DirReaderFeature()
55
55
 
 
56
TermIOSFeature = tests.ModuleAvailableFeature('termios')
 
57
 
56
58
 
57
59
def _already_unicode(s):
58
60
    return s
59
61
 
60
62
 
61
 
def _fs_enc_to_unicode(s):
62
 
    return s.decode(osutils._fs_enc)
63
 
 
64
 
 
65
63
def _utf8_to_unicode(s):
66
64
    return s.decode('UTF-8')
67
65
 
87
85
    if test__walkdirs_win32.Win32ReadDirFeature.available():
88
86
        try:
89
87
            from bzrlib import _walkdirs_win32
90
 
            # TODO: check on windows, it may be that we need to use/add
91
 
            # safe_unicode instead of _fs_enc_to_unicode
92
88
            scenarios.append(
93
89
                ('win32',
94
90
                 dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
95
 
                      _native_to_unicode=_fs_enc_to_unicode)))
 
91
                      _native_to_unicode=_already_unicode)))
96
92
        except ImportError:
97
93
            pass
98
94
    return scenarios
126
122
 
127
123
class TestRename(tests.TestCaseInTempDir):
128
124
 
 
125
    def create_file(self, filename, content):
 
126
        f = open(filename, 'wb')
 
127
        try:
 
128
            f.write(content)
 
129
        finally:
 
130
            f.close()
 
131
 
 
132
    def _fancy_rename(self, a, b):
 
133
        osutils.fancy_rename(a, b, rename_func=os.rename,
 
134
                             unlink_func=os.unlink)
 
135
 
129
136
    def test_fancy_rename(self):
130
137
        # This should work everywhere
131
 
        def rename(a, b):
132
 
            osutils.fancy_rename(a, b,
133
 
                    rename_func=os.rename,
134
 
                    unlink_func=os.unlink)
135
 
 
136
 
        open('a', 'wb').write('something in a\n')
137
 
        rename('a', 'b')
 
138
        self.create_file('a', 'something in a\n')
 
139
        self._fancy_rename('a', 'b')
138
140
        self.failIfExists('a')
139
141
        self.failUnlessExists('b')
140
142
        self.check_file_contents('b', 'something in a\n')
141
143
 
142
 
        open('a', 'wb').write('new something in a\n')
143
 
        rename('b', 'a')
 
144
        self.create_file('a', 'new something in a\n')
 
145
        self._fancy_rename('b', 'a')
144
146
 
145
147
        self.check_file_contents('a', 'something in a\n')
146
148
 
 
149
    def test_fancy_rename_fails_source_missing(self):
 
150
        # An exception should be raised, and the target should be left in place
 
151
        self.create_file('target', 'data in target\n')
 
152
        self.assertRaises((IOError, OSError), self._fancy_rename,
 
153
                          'missingsource', 'target')
 
154
        self.failUnlessExists('target')
 
155
        self.check_file_contents('target', 'data in target\n')
 
156
 
 
157
    def test_fancy_rename_fails_if_source_and_target_missing(self):
 
158
        self.assertRaises((IOError, OSError), self._fancy_rename,
 
159
                          'missingsource', 'missingtarget')
 
160
 
147
161
    def test_rename(self):
148
162
        # Rename should be semi-atomic on all platforms
149
 
        open('a', 'wb').write('something in a\n')
 
163
        self.create_file('a', 'something in a\n')
150
164
        osutils.rename('a', 'b')
151
165
        self.failIfExists('a')
152
166
        self.failUnlessExists('b')
153
167
        self.check_file_contents('b', 'something in a\n')
154
168
 
155
 
        open('a', 'wb').write('new something in a\n')
 
169
        self.create_file('a', 'new something in a\n')
156
170
        osutils.rename('b', 'a')
157
171
 
158
172
        self.check_file_contents('a', 'something in a\n')
366
380
        # Instead blackbox.test_locale should check for localized
367
381
        # dates once they do occur in output strings.
368
382
 
 
383
    def test_format_date_with_offset_in_original_timezone(self):
 
384
        self.assertEqual("Thu 1970-01-01 00:00:00 +0000",
 
385
            osutils.format_date_with_offset_in_original_timezone(0))
 
386
        self.assertEqual("Fri 1970-01-02 03:46:40 +0000",
 
387
            osutils.format_date_with_offset_in_original_timezone(100000))
 
388
        self.assertEqual("Fri 1970-01-02 05:46:40 +0200",
 
389
            osutils.format_date_with_offset_in_original_timezone(100000, 7200))
 
390
 
369
391
    def test_local_time_offset(self):
370
392
        """Test that local_time_offset() returns a sane value."""
371
393
        offset = osutils.local_time_offset()
1613
1635
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
1614
1636
        self.build_tree_contents([('foo', text)])
1615
1637
        expected_sha = osutils.sha_string(text)
1616
 
        f = open('foo')
 
1638
        f = open('foo', 'rb')
1617
1639
        self.addCleanup(f.close)
1618
1640
        size, sha = osutils.size_sha_file(f)
1619
1641
        self.assertEqual(38, size)
1835
1857
 
1836
1858
class TestConcurrency(tests.TestCase):
1837
1859
 
 
1860
    def setUp(self):
 
1861
        super(TestConcurrency, self).setUp()
 
1862
        orig = osutils._cached_local_concurrency
 
1863
        def restore():
 
1864
            osutils._cached_local_concurrency = orig
 
1865
        self.addCleanup(restore)
 
1866
 
1838
1867
    def test_local_concurrency(self):
1839
1868
        concurrency = osutils.local_concurrency()
1840
1869
        self.assertIsInstance(concurrency, int)
1841
1870
 
 
1871
    def test_local_concurrency_environment_variable(self):
 
1872
        os.environ['BZR_CONCURRENCY'] = '2'
 
1873
        self.assertEqual(2, osutils.local_concurrency(use_cache=False))
 
1874
        os.environ['BZR_CONCURRENCY'] = '3'
 
1875
        self.assertEqual(3, osutils.local_concurrency(use_cache=False))
 
1876
        os.environ['BZR_CONCURRENCY'] = 'foo'
 
1877
        self.assertEqual(1, osutils.local_concurrency(use_cache=False))
 
1878
 
 
1879
    def test_option_concurrency(self):
 
1880
        os.environ['BZR_CONCURRENCY'] = '1'
 
1881
        self.run_bzr('rocks --concurrency 42')
 
1882
        # Command line overrides envrionment variable
 
1883
        self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
 
1884
        self.assertEquals(42, osutils.local_concurrency(use_cache=False))
 
1885
 
1842
1886
 
1843
1887
class TestFailedToLoadExtension(tests.TestCase):
1844
1888
 
1880
1924
            r"bzr: warning: some compiled extensions could not be loaded; "
1881
1925
            "see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1882
1926
            )
 
1927
 
 
1928
 
 
1929
class TestTerminalWidth(tests.TestCase):
 
1930
 
 
1931
    def test_default_values(self):
 
1932
        self.assertEquals(80, osutils.default_terminal_width)
 
1933
 
 
1934
    def test_defaults_to_BZR_COLUMNS(self):
 
1935
        # BZR_COLUMNS is set by the test framework
 
1936
        self.assertEquals('80', os.environ['BZR_COLUMNS'])
 
1937
        os.environ['BZR_COLUMNS'] = '12'
 
1938
        self.assertEquals(12, osutils.terminal_width())
 
1939
 
 
1940
    def test_tty_default_without_columns(self):
 
1941
        del os.environ['BZR_COLUMNS']
 
1942
        del os.environ['COLUMNS']
 
1943
        orig_stdout = sys.stdout
 
1944
        def restore():
 
1945
            sys.stdout = orig_stdout
 
1946
        self.addCleanup(restore)
 
1947
 
 
1948
        class I_am_a_tty(object):
 
1949
            def isatty(self):
 
1950
                return True
 
1951
 
 
1952
        sys.stdout = I_am_a_tty()
 
1953
        self.assertEquals(None, osutils.terminal_width())
 
1954
 
 
1955
    def test_non_tty_default_without_columns(self):
 
1956
        del os.environ['BZR_COLUMNS']
 
1957
        del os.environ['COLUMNS']
 
1958
        orig_stdout = sys.stdout
 
1959
        def restore():
 
1960
            sys.stdout = orig_stdout
 
1961
        self.addCleanup(restore)
 
1962
        sys.stdout = None
 
1963
        self.assertEquals(None, osutils.terminal_width())
 
1964
 
 
1965
    def test_no_TIOCGWINSZ(self):
 
1966
        self.requireFeature(TermIOSFeature)
 
1967
        termios = TermIOSFeature.module
 
1968
        # bug 63539 is about a termios without TIOCGWINSZ attribute
 
1969
        try:
 
1970
            orig = termios.TIOCGWINSZ
 
1971
        except AttributeError:
 
1972
            # We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
 
1973
            pass
 
1974
        else:
 
1975
            def restore():
 
1976
                termios.TIOCGWINSZ = orig
 
1977
            self.addCleanup(restore)
 
1978
            del termios.TIOCGWINSZ
 
1979
        del os.environ['BZR_COLUMNS']
 
1980
        del os.environ['COLUMNS']
 
1981
        self.assertIs(None, osutils.terminal_width())