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')
434
469
def test_canonical_relpath_simple(self):
435
470
f = file('MixedCaseName', 'w')
437
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
438
real_base_dir = osutils.realpath(self.test_base_dir)
439
actual = osutils.canonical_relpath(real_base_dir, 'mixedcasename')
472
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
440
473
self.failUnlessEqual('work/MixedCaseName', actual)
442
475
def test_canonical_relpath_missing_tail(self):
443
476
os.mkdir('MixedCaseParent')
444
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
445
real_base_dir = osutils.realpath(self.test_base_dir)
446
actual = osutils.canonical_relpath(real_base_dir,
477
actual = osutils.canonical_relpath(self.test_base_dir,
447
478
'mixedcaseparent/nochild')
448
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')
451
525
class TestPumpFile(tests.TestCase):
452
526
"""Test pumpfile method."""
613
687
self.assertEqual("1234", output.getvalue())
690
class TestRelpath(tests.TestCase):
692
def test_simple_relpath(self):
693
cwd = osutils.getcwd()
694
subdir = cwd + '/subdir'
695
self.assertEqual('subdir', osutils.relpath(cwd, subdir))
697
def test_deep_relpath(self):
698
cwd = osutils.getcwd()
699
subdir = cwd + '/sub/subsubdir'
700
self.assertEqual('sub/subsubdir', osutils.relpath(cwd, subdir))
702
def test_not_relative(self):
703
self.assertRaises(errors.PathNotChild,
704
osutils.relpath, 'C:/path', 'H:/path')
705
self.assertRaises(errors.PathNotChild,
706
osutils.relpath, 'C:/', 'H:/path')
616
709
class TestSafeUnicode(tests.TestCase):
618
711
def test_from_ascii_string(self):
1765
1858
class TestConcurrency(tests.TestCase):
1861
super(TestConcurrency, self).setUp()
1862
orig = osutils._cached_local_concurrency
1864
osutils._cached_local_concurrency = orig
1865
self.addCleanup(restore)
1767
1867
def test_local_concurrency(self):
1768
1868
concurrency = osutils.local_concurrency()
1769
1869
self.assertIsInstance(concurrency, int)
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))
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))
1887
class TestFailedToLoadExtension(tests.TestCase):
1889
def _try_loading(self):
1891
import bzrlib._fictional_extension_py
1892
except ImportError, e:
1893
osutils.failed_to_load_extension(e)
1897
super(TestFailedToLoadExtension, self).setUp()
1898
self.saved_failures = osutils._extension_load_failures[:]
1899
del osutils._extension_load_failures[:]
1900
self.addCleanup(self.restore_failures)
1902
def restore_failures(self):
1903
osutils._extension_load_failures = self.saved_failures
1905
def test_failure_to_load(self):
1907
self.assertLength(1, osutils._extension_load_failures)
1908
self.assertEquals(osutils._extension_load_failures[0],
1909
"No module named _fictional_extension_py")
1911
def test_report_extension_load_failures_no_warning(self):
1912
self.assertTrue(self._try_loading())
1913
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
1914
# it used to give a Python warning; it no longer does
1915
self.assertLength(0, warnings)
1917
def test_report_extension_load_failures_message(self):
1919
trace.push_log_file(log)
1920
self.assertTrue(self._try_loading())
1921
osutils.report_extension_load_failures()
1922
self.assertContainsRe(
1924
r"bzr: warning: some compiled extensions could not be loaded; "
1925
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1929
class TestTerminalWidth(tests.TestCase):
1931
def replace_stdout(self, new):
1932
orig_stdout = sys.stdout
1934
sys.stdout = orig_stdout
1935
self.addCleanup(restore)
1938
def replace__terminal_size(self, new):
1939
orig__terminal_size = osutils._terminal_size
1941
osutils._terminal_size = orig__terminal_size
1942
self.addCleanup(restore)
1943
osutils._terminal_size = new
1945
def set_fake_tty(self):
1947
class I_am_a_tty(object):
1951
self.replace_stdout(I_am_a_tty())
1953
def test_default_values(self):
1954
self.assertEqual(80, osutils.default_terminal_width)
1956
def test_defaults_to_BZR_COLUMNS(self):
1957
# BZR_COLUMNS is set by the test framework
1958
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1959
os.environ['BZR_COLUMNS'] = '12'
1960
self.assertEqual(12, osutils.terminal_width())
1962
def test_falls_back_to_COLUMNS(self):
1963
del os.environ['BZR_COLUMNS']
1964
self.assertNotEqual('42', os.environ['COLUMNS'])
1966
os.environ['COLUMNS'] = '42'
1967
self.assertEqual(42, osutils.terminal_width())
1969
def test_tty_default_without_columns(self):
1970
del os.environ['BZR_COLUMNS']
1971
del os.environ['COLUMNS']
1973
def terminal_size(w, h):
1977
# We need to override the osutils definition as it depends on the
1978
# running environment that we can't control (PQM running without a
1979
# controlling terminal is one example).
1980
self.replace__terminal_size(terminal_size)
1981
self.assertEqual(42, osutils.terminal_width())
1983
def test_non_tty_default_without_columns(self):
1984
del os.environ['BZR_COLUMNS']
1985
del os.environ['COLUMNS']
1986
self.replace_stdout(None)
1987
self.assertEqual(None, osutils.terminal_width())
1989
def test_no_TIOCGWINSZ(self):
1990
self.requireFeature(term_ios_feature)
1991
termios = term_ios_feature.module
1992
# bug 63539 is about a termios without TIOCGWINSZ attribute
1994
orig = termios.TIOCGWINSZ
1995
except AttributeError:
1996
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
2000
termios.TIOCGWINSZ = orig
2001
self.addCleanup(restore)
2002
del termios.TIOCGWINSZ
2003
del os.environ['BZR_COLUMNS']
2004
del os.environ['COLUMNS']
2005
# Whatever the result is, if we don't raise an exception, it's ok.
2006
osutils.terminal_width()