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')
365
380
# Instead blackbox.test_locale should check for localized
366
381
# dates once they do occur in output strings.
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))
368
391
def test_local_time_offset(self):
369
392
"""Test that local_time_offset() returns a sane value."""
370
393
offset = osutils.local_time_offset()
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."""
625
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')
628
709
class TestSafeUnicode(tests.TestCase):
630
711
def test_from_ascii_string(self):
1777
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)
1779
1867
def test_local_concurrency(self):
1780
1868
concurrency = osutils.local_concurrency()
1781
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 test_default_values(self):
1932
self.assertEquals(80, osutils.default_terminal_width)
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())
1940
def test_tty_default_without_columns(self):
1941
del os.environ['BZR_COLUMNS']
1942
del os.environ['COLUMNS']
1943
orig_stdout = sys.stdout
1945
sys.stdout = orig_stdout
1946
self.addCleanup(restore)
1948
class I_am_a_tty(object):
1952
sys.stdout = I_am_a_tty()
1953
self.assertEquals(None, osutils.terminal_width())
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
1960
sys.stdout = orig_stdout
1961
self.addCleanup(restore)
1963
self.assertEquals(None, osutils.terminal_width())
1965
def test_no_TIOCGWINSZ(self):
1966
self.requireFeature(TermIOSFeature)
1967
termios = TermIOSFeature.module
1968
# bug 63539 is about a termios without TIOCGWINSZ attribute
1970
orig = termios.TIOCGWINSZ
1971
except AttributeError:
1972
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
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())