123
126
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)
136
128
def test_fancy_rename(self):
137
129
# This should work everywhere
138
self.create_file('a', 'something in a\n')
139
self._fancy_rename('a', 'b')
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')
140
137
self.failIfExists('a')
141
138
self.failUnlessExists('b')
142
139
self.check_file_contents('b', 'something in a\n')
144
self.create_file('a', 'new something in a\n')
145
self._fancy_rename('b', 'a')
141
open('a', 'wb').write('new something in a\n')
147
144
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')
161
146
def test_rename(self):
162
147
# Rename should be semi-atomic on all platforms
163
self.create_file('a', 'something in a\n')
148
open('a', 'wb').write('something in a\n')
164
149
osutils.rename('a', 'b')
165
150
self.failIfExists('a')
166
151
self.failUnlessExists('b')
167
152
self.check_file_contents('b', 'something in a\n')
169
self.create_file('a', 'new something in a\n')
154
open('a', 'wb').write('new something in a\n')
170
155
osutils.rename('b', 'a')
172
157
self.check_file_contents('a', 'something in a\n')
471
446
def test_canonical_relpath_simple(self):
472
447
f = file('MixedCaseName', 'w')
474
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
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')
475
452
self.failUnlessEqual('work/MixedCaseName', actual)
477
454
def test_canonical_relpath_missing_tail(self):
478
455
os.mkdir('MixedCaseParent')
479
actual = osutils.canonical_relpath(self.test_base_dir,
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,
480
459
'mixedcaseparent/nochild')
481
460
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
484
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
486
def assertRelpath(self, expected, base, path):
487
actual = osutils._cicp_canonical_relpath(base, path)
488
self.assertEqual(expected, actual)
490
def test_simple(self):
491
self.build_tree(['MixedCaseName'])
492
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
493
self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
495
def test_subdir_missing_tail(self):
496
self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
497
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
498
self.assertRelpath('MixedCaseParent/a_child', base,
499
'MixedCaseParent/a_child')
500
self.assertRelpath('MixedCaseParent/a_child', base,
501
'MixedCaseParent/A_Child')
502
self.assertRelpath('MixedCaseParent/not_child', base,
503
'MixedCaseParent/not_child')
505
def test_at_root_slash(self):
506
# We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
508
if osutils.MIN_ABS_PATHLENGTH > 1:
509
raise tests.TestSkipped('relpath requires %d chars'
510
% osutils.MIN_ABS_PATHLENGTH)
511
self.assertRelpath('foo', '/', '/foo')
513
def test_at_root_drive(self):
514
if sys.platform != 'win32':
515
raise tests.TestNotApplicable('we can only test drive-letter relative'
516
' paths on Windows where we have drive'
519
# The specific issue is that when at the root of a drive, 'abspath'
520
# returns "C:/" or just "/". However, the code assumes that abspath
521
# always returns something like "C:/foo" or "/foo" (no trailing slash).
522
self.assertRelpath('foo', 'C:/', 'C:/foo')
523
self.assertRelpath('foo', 'X:/', 'X:/foo')
524
self.assertRelpath('foo', 'X:/', 'X://foo')
527
463
class TestPumpFile(tests.TestCase):
528
464
"""Test pumpfile method."""
1847
1796
class TestConcurrency(tests.TestCase):
1850
super(TestConcurrency, self).setUp()
1851
self.overrideAttr(osutils, '_cached_local_concurrency')
1853
1798
def test_local_concurrency(self):
1854
1799
concurrency = osutils.local_concurrency()
1855
1800
self.assertIsInstance(concurrency, int)
1857
def test_local_concurrency_environment_variable(self):
1858
os.environ['BZR_CONCURRENCY'] = '2'
1859
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1860
os.environ['BZR_CONCURRENCY'] = '3'
1861
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1862
os.environ['BZR_CONCURRENCY'] = 'foo'
1863
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1865
def test_option_concurrency(self):
1866
os.environ['BZR_CONCURRENCY'] = '1'
1867
self.run_bzr('rocks --concurrency 42')
1868
# Command line overrides envrionment variable
1869
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1870
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1873
class TestFailedToLoadExtension(tests.TestCase):
1875
def _try_loading(self):
1877
import bzrlib._fictional_extension_py
1878
except ImportError, e:
1879
osutils.failed_to_load_extension(e)
1883
super(TestFailedToLoadExtension, self).setUp()
1884
self.overrideAttr(osutils, '_extension_load_failures', [])
1886
def test_failure_to_load(self):
1888
self.assertLength(1, osutils._extension_load_failures)
1889
self.assertEquals(osutils._extension_load_failures[0],
1890
"No module named _fictional_extension_py")
1892
def test_report_extension_load_failures_no_warning(self):
1893
self.assertTrue(self._try_loading())
1894
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
1895
# it used to give a Python warning; it no longer does
1896
self.assertLength(0, warnings)
1898
def test_report_extension_load_failures_message(self):
1900
trace.push_log_file(log)
1901
self.assertTrue(self._try_loading())
1902
osutils.report_extension_load_failures()
1903
self.assertContainsRe(
1905
r"bzr: warning: some compiled extensions could not be loaded; "
1906
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1910
class TestTerminalWidth(tests.TestCase):
1912
def replace_stdout(self, new):
1913
self.overrideAttr(sys, 'stdout', new)
1915
def replace__terminal_size(self, new):
1916
self.overrideAttr(osutils, '_terminal_size', new)
1918
def set_fake_tty(self):
1920
class I_am_a_tty(object):
1924
self.replace_stdout(I_am_a_tty())
1926
def test_default_values(self):
1927
self.assertEqual(80, osutils.default_terminal_width)
1929
def test_defaults_to_BZR_COLUMNS(self):
1930
# BZR_COLUMNS is set by the test framework
1931
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1932
os.environ['BZR_COLUMNS'] = '12'
1933
self.assertEqual(12, osutils.terminal_width())
1935
def test_falls_back_to_COLUMNS(self):
1936
del os.environ['BZR_COLUMNS']
1937
self.assertNotEqual('42', os.environ['COLUMNS'])
1939
os.environ['COLUMNS'] = '42'
1940
self.assertEqual(42, osutils.terminal_width())
1942
def test_tty_default_without_columns(self):
1943
del os.environ['BZR_COLUMNS']
1944
del os.environ['COLUMNS']
1946
def terminal_size(w, h):
1950
# We need to override the osutils definition as it depends on the
1951
# running environment that we can't control (PQM running without a
1952
# controlling terminal is one example).
1953
self.replace__terminal_size(terminal_size)
1954
self.assertEqual(42, osutils.terminal_width())
1956
def test_non_tty_default_without_columns(self):
1957
del os.environ['BZR_COLUMNS']
1958
del os.environ['COLUMNS']
1959
self.replace_stdout(None)
1960
self.assertEqual(None, osutils.terminal_width())
1962
def test_no_TIOCGWINSZ(self):
1963
self.requireFeature(term_ios_feature)
1964
termios = term_ios_feature.module
1965
# bug 63539 is about a termios without TIOCGWINSZ attribute
1967
orig = termios.TIOCGWINSZ
1968
except AttributeError:
1969
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
1972
self.overrideAttr(termios, 'TIOCGWINSZ')
1973
del termios.TIOCGWINSZ
1974
del os.environ['BZR_COLUMNS']
1975
del os.environ['COLUMNS']
1976
# Whatever the result is, if we don't raise an exception, it's ok.
1977
osutils.terminal_width()