126
123
class TestRename(tests.TestCaseInTempDir):
125
def create_file(self, filename, content):
126
f = open(filename, 'wb')
132
def _fancy_rename(self, a, b):
133
osutils.fancy_rename(a, b, rename_func=os.rename,
134
unlink_func=os.unlink)
128
136
def test_fancy_rename(self):
129
137
# This should work everywhere
131
osutils.fancy_rename(a, b,
132
rename_func=os.rename,
133
unlink_func=os.unlink)
135
open('a', 'wb').write('something in a\n')
138
self.create_file('a', 'something in a\n')
139
self._fancy_rename('a', 'b')
137
140
self.failIfExists('a')
138
141
self.failUnlessExists('b')
139
142
self.check_file_contents('b', 'something in a\n')
141
open('a', 'wb').write('new something in a\n')
144
self.create_file('a', 'new something in a\n')
145
self._fancy_rename('b', 'a')
144
147
self.check_file_contents('a', 'something in a\n')
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')
157
def test_fancy_rename_fails_if_source_and_target_missing(self):
158
self.assertRaises((IOError, OSError), self._fancy_rename,
159
'missingsource', 'missingtarget')
146
161
def test_rename(self):
147
162
# Rename should be semi-atomic on all platforms
148
open('a', 'wb').write('something in a\n')
163
self.create_file('a', 'something in a\n')
149
164
osutils.rename('a', 'b')
150
165
self.failIfExists('a')
151
166
self.failUnlessExists('b')
152
167
self.check_file_contents('b', 'something in a\n')
154
open('a', 'wb').write('new something in a\n')
169
self.create_file('a', 'new something in a\n')
155
170
osutils.rename('b', 'a')
157
172
self.check_file_contents('a', 'something in a\n')
446
469
def test_canonical_relpath_simple(self):
447
470
f = file('MixedCaseName', 'w')
449
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
450
real_base_dir = osutils.realpath(self.test_base_dir)
451
actual = osutils.canonical_relpath(real_base_dir, 'mixedcasename')
472
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
452
473
self.failUnlessEqual('work/MixedCaseName', actual)
454
475
def test_canonical_relpath_missing_tail(self):
455
476
os.mkdir('MixedCaseParent')
456
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
457
real_base_dir = osutils.realpath(self.test_base_dir)
458
actual = osutils.canonical_relpath(real_base_dir,
477
actual = osutils.canonical_relpath(self.test_base_dir,
459
478
'mixedcaseparent/nochild')
460
479
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
482
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
484
def assertRelpath(self, expected, base, path):
485
actual = osutils._cicp_canonical_relpath(base, path)
486
self.assertEqual(expected, actual)
488
def test_simple(self):
489
self.build_tree(['MixedCaseName'])
490
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
491
self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
493
def test_subdir_missing_tail(self):
494
self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
495
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
496
self.assertRelpath('MixedCaseParent/a_child', base,
497
'MixedCaseParent/a_child')
498
self.assertRelpath('MixedCaseParent/a_child', base,
499
'MixedCaseParent/A_Child')
500
self.assertRelpath('MixedCaseParent/not_child', base,
501
'MixedCaseParent/not_child')
503
def test_at_root_slash(self):
504
# We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
506
if osutils.MIN_ABS_PATHLENGTH > 1:
507
raise tests.TestSkipped('relpath requires %d chars'
508
% osutils.MIN_ABS_PATHLENGTH)
509
self.assertRelpath('foo', '/', '/foo')
511
def test_at_root_drive(self):
512
if sys.platform != 'win32':
513
raise tests.TestNotApplicable('we can only test drive-letter relative'
514
' paths on Windows where we have drive'
517
# The specific issue is that when at the root of a drive, 'abspath'
518
# returns "C:/" or just "/". However, the code assumes that abspath
519
# always returns something like "C:/foo" or "/foo" (no trailing slash).
520
self.assertRelpath('foo', 'C:/', 'C:/foo')
521
self.assertRelpath('foo', 'X:/', 'X:/foo')
522
self.assertRelpath('foo', 'X:/', 'X://foo')
463
525
class TestPumpFile(tests.TestCase):
464
526
"""Test pumpfile method."""
1796
1845
class TestConcurrency(tests.TestCase):
1848
super(TestConcurrency, self).setUp()
1849
self.overrideAttr(osutils, '_cached_local_concurrency')
1798
1851
def test_local_concurrency(self):
1799
1852
concurrency = osutils.local_concurrency()
1800
1853
self.assertIsInstance(concurrency, int)
1855
def test_local_concurrency_environment_variable(self):
1856
os.environ['BZR_CONCURRENCY'] = '2'
1857
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1858
os.environ['BZR_CONCURRENCY'] = '3'
1859
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1860
os.environ['BZR_CONCURRENCY'] = 'foo'
1861
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1863
def test_option_concurrency(self):
1864
os.environ['BZR_CONCURRENCY'] = '1'
1865
self.run_bzr('rocks --concurrency 42')
1866
# Command line overrides envrionment variable
1867
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1868
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1871
class TestFailedToLoadExtension(tests.TestCase):
1873
def _try_loading(self):
1875
import bzrlib._fictional_extension_py
1876
except ImportError, e:
1877
osutils.failed_to_load_extension(e)
1881
super(TestFailedToLoadExtension, self).setUp()
1882
self.overrideAttr(osutils, '_extension_load_failures', [])
1884
def test_failure_to_load(self):
1886
self.assertLength(1, osutils._extension_load_failures)
1887
self.assertEquals(osutils._extension_load_failures[0],
1888
"No module named _fictional_extension_py")
1890
def test_report_extension_load_failures_no_warning(self):
1891
self.assertTrue(self._try_loading())
1892
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
1893
# it used to give a Python warning; it no longer does
1894
self.assertLength(0, warnings)
1896
def test_report_extension_load_failures_message(self):
1898
trace.push_log_file(log)
1899
self.assertTrue(self._try_loading())
1900
osutils.report_extension_load_failures()
1901
self.assertContainsRe(
1903
r"bzr: warning: some compiled extensions could not be loaded; "
1904
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1908
class TestTerminalWidth(tests.TestCase):
1910
def replace_stdout(self, new):
1911
self.overrideAttr(sys, 'stdout', new)
1913
def replace__terminal_size(self, new):
1914
self.overrideAttr(osutils, '_terminal_size', new)
1916
def set_fake_tty(self):
1918
class I_am_a_tty(object):
1922
self.replace_stdout(I_am_a_tty())
1924
def test_default_values(self):
1925
self.assertEqual(80, osutils.default_terminal_width)
1927
def test_defaults_to_BZR_COLUMNS(self):
1928
# BZR_COLUMNS is set by the test framework
1929
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1930
os.environ['BZR_COLUMNS'] = '12'
1931
self.assertEqual(12, osutils.terminal_width())
1933
def test_falls_back_to_COLUMNS(self):
1934
del os.environ['BZR_COLUMNS']
1935
self.assertNotEqual('42', os.environ['COLUMNS'])
1937
os.environ['COLUMNS'] = '42'
1938
self.assertEqual(42, osutils.terminal_width())
1940
def test_tty_default_without_columns(self):
1941
del os.environ['BZR_COLUMNS']
1942
del os.environ['COLUMNS']
1944
def terminal_size(w, h):
1948
# We need to override the osutils definition as it depends on the
1949
# running environment that we can't control (PQM running without a
1950
# controlling terminal is one example).
1951
self.replace__terminal_size(terminal_size)
1952
self.assertEqual(42, osutils.terminal_width())
1954
def test_non_tty_default_without_columns(self):
1955
del os.environ['BZR_COLUMNS']
1956
del os.environ['COLUMNS']
1957
self.replace_stdout(None)
1958
self.assertEqual(None, osutils.terminal_width())
1960
def test_no_TIOCGWINSZ(self):
1961
self.requireFeature(term_ios_feature)
1962
termios = term_ios_feature.module
1963
# bug 63539 is about a termios without TIOCGWINSZ attribute
1965
orig = termios.TIOCGWINSZ
1966
except AttributeError:
1967
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
1970
self.overrideAttr(termios, 'TIOCGWINSZ')
1971
del termios.TIOCGWINSZ
1972
del os.environ['BZR_COLUMNS']
1973
del os.environ['COLUMNS']
1974
# Whatever the result is, if we don't raise an exception, it's ok.
1975
osutils.terminal_width()