125
126
class TestRename(tests.TestCaseInTempDir):
127
def create_file(self, filename, content):
128
f = open(filename, 'wb')
134
def _fancy_rename(self, a, b):
135
osutils.fancy_rename(a, b, rename_func=os.rename,
136
unlink_func=os.unlink)
138
128
def test_fancy_rename(self):
139
129
# This should work everywhere
140
self.create_file('a', 'something in a\n')
141
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')
142
137
self.failIfExists('a')
143
138
self.failUnlessExists('b')
144
139
self.check_file_contents('b', 'something in a\n')
146
self.create_file('a', 'new something in a\n')
147
self._fancy_rename('b', 'a')
141
open('a', 'wb').write('new something in a\n')
149
144
self.check_file_contents('a', 'something in a\n')
151
def test_fancy_rename_fails_source_missing(self):
152
# An exception should be raised, and the target should be left in place
153
self.create_file('target', 'data in target\n')
154
self.assertRaises((IOError, OSError), self._fancy_rename,
155
'missingsource', 'target')
156
self.failUnlessExists('target')
157
self.check_file_contents('target', 'data in target\n')
159
def test_fancy_rename_fails_if_source_and_target_missing(self):
160
self.assertRaises((IOError, OSError), self._fancy_rename,
161
'missingsource', 'missingtarget')
163
146
def test_rename(self):
164
147
# Rename should be semi-atomic on all platforms
165
self.create_file('a', 'something in a\n')
148
open('a', 'wb').write('something in a\n')
166
149
osutils.rename('a', 'b')
167
150
self.failIfExists('a')
168
151
self.failUnlessExists('b')
169
152
self.check_file_contents('b', 'something in a\n')
171
self.create_file('a', 'new something in a\n')
154
open('a', 'wb').write('new something in a\n')
172
155
osutils.rename('b', 'a')
174
157
self.check_file_contents('a', 'something in a\n')
473
434
def test_canonical_relpath_simple(self):
474
435
f = file('MixedCaseName', 'w')
476
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
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')
477
440
self.failUnlessEqual('work/MixedCaseName', actual)
479
442
def test_canonical_relpath_missing_tail(self):
480
443
os.mkdir('MixedCaseParent')
481
actual = osutils.canonical_relpath(self.test_base_dir,
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,
482
447
'mixedcaseparent/nochild')
483
448
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
486
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
488
def assertRelpath(self, expected, base, path):
489
actual = osutils._cicp_canonical_relpath(base, path)
490
self.assertEqual(expected, actual)
492
def test_simple(self):
493
self.build_tree(['MixedCaseName'])
494
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
495
self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
497
def test_subdir_missing_tail(self):
498
self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
499
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
500
self.assertRelpath('MixedCaseParent/a_child', base,
501
'MixedCaseParent/a_child')
502
self.assertRelpath('MixedCaseParent/a_child', base,
503
'MixedCaseParent/A_Child')
504
self.assertRelpath('MixedCaseParent/not_child', base,
505
'MixedCaseParent/not_child')
507
def test_at_root_slash(self):
508
# We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
510
if osutils.MIN_ABS_PATHLENGTH > 1:
511
raise tests.TestSkipped('relpath requires %d chars'
512
% osutils.MIN_ABS_PATHLENGTH)
513
self.assertRelpath('foo', '/', '/foo')
515
def test_at_root_drive(self):
516
if sys.platform != 'win32':
517
raise tests.TestNotApplicable('we can only test drive-letter relative'
518
' paths on Windows where we have drive'
521
# The specific issue is that when at the root of a drive, 'abspath'
522
# returns "C:/" or just "/". However, the code assumes that abspath
523
# always returns something like "C:/foo" or "/foo" (no trailing slash).
524
self.assertRelpath('foo', 'C:/', 'C:/foo')
525
self.assertRelpath('foo', 'X:/', 'X:/foo')
526
self.assertRelpath('foo', 'X:/', 'X://foo')
529
451
class TestPumpFile(tests.TestCase):
530
452
"""Test pumpfile method."""
1892
1765
class TestConcurrency(tests.TestCase):
1895
super(TestConcurrency, self).setUp()
1896
self.overrideAttr(osutils, '_cached_local_concurrency')
1898
1767
def test_local_concurrency(self):
1899
1768
concurrency = osutils.local_concurrency()
1900
1769
self.assertIsInstance(concurrency, int)
1902
def test_local_concurrency_environment_variable(self):
1903
os.environ['BZR_CONCURRENCY'] = '2'
1904
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
os.environ['BZR_CONCURRENCY'] = '3'
1906
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
os.environ['BZR_CONCURRENCY'] = 'foo'
1908
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1910
def test_option_concurrency(self):
1911
os.environ['BZR_CONCURRENCY'] = '1'
1912
self.run_bzr('rocks --concurrency 42')
1913
# Command line overrides envrionment variable
1914
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1918
class TestFailedToLoadExtension(tests.TestCase):
1920
def _try_loading(self):
1922
import bzrlib._fictional_extension_py
1923
except ImportError, e:
1924
osutils.failed_to_load_extension(e)
1928
super(TestFailedToLoadExtension, self).setUp()
1929
self.overrideAttr(osutils, '_extension_load_failures', [])
1931
def test_failure_to_load(self):
1933
self.assertLength(1, osutils._extension_load_failures)
1934
self.assertEquals(osutils._extension_load_failures[0],
1935
"No module named _fictional_extension_py")
1937
def test_report_extension_load_failures_no_warning(self):
1938
self.assertTrue(self._try_loading())
1939
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
1940
# it used to give a Python warning; it no longer does
1941
self.assertLength(0, warnings)
1943
def test_report_extension_load_failures_message(self):
1945
trace.push_log_file(log)
1946
self.assertTrue(self._try_loading())
1947
osutils.report_extension_load_failures()
1948
self.assertContainsRe(
1950
r"bzr: warning: some compiled extensions could not be loaded; "
1951
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1955
class TestTerminalWidth(tests.TestCase):
1958
tests.TestCase.setUp(self)
1959
self._orig_terminal_size_state = osutils._terminal_size_state
1960
self._orig_first_terminal_size = osutils._first_terminal_size
1961
self.addCleanup(self.restore_osutils_globals)
1962
osutils._terminal_size_state = 'no_data'
1963
osutils._first_terminal_size = None
1965
def restore_osutils_globals(self):
1966
osutils._terminal_size_state = self._orig_terminal_size_state
1967
osutils._first_terminal_size = self._orig_first_terminal_size
1969
def replace_stdout(self, new):
1970
self.overrideAttr(sys, 'stdout', new)
1972
def replace__terminal_size(self, new):
1973
self.overrideAttr(osutils, '_terminal_size', new)
1975
def set_fake_tty(self):
1977
class I_am_a_tty(object):
1981
self.replace_stdout(I_am_a_tty())
1983
def test_default_values(self):
1984
self.assertEqual(80, osutils.default_terminal_width)
1986
def test_defaults_to_BZR_COLUMNS(self):
1987
# BZR_COLUMNS is set by the test framework
1988
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
os.environ['BZR_COLUMNS'] = '12'
1990
self.assertEqual(12, osutils.terminal_width())
1992
def test_falls_back_to_COLUMNS(self):
1993
del os.environ['BZR_COLUMNS']
1994
self.assertNotEqual('42', os.environ['COLUMNS'])
1996
os.environ['COLUMNS'] = '42'
1997
self.assertEqual(42, osutils.terminal_width())
1999
def test_tty_default_without_columns(self):
2000
del os.environ['BZR_COLUMNS']
2001
del os.environ['COLUMNS']
2003
def terminal_size(w, h):
2007
# We need to override the osutils definition as it depends on the
2008
# running environment that we can't control (PQM running without a
2009
# controlling terminal is one example).
2010
self.replace__terminal_size(terminal_size)
2011
self.assertEqual(42, osutils.terminal_width())
2013
def test_non_tty_default_without_columns(self):
2014
del os.environ['BZR_COLUMNS']
2015
del os.environ['COLUMNS']
2016
self.replace_stdout(None)
2017
self.assertEqual(None, osutils.terminal_width())
2019
def test_no_TIOCGWINSZ(self):
2020
self.requireFeature(term_ios_feature)
2021
termios = term_ios_feature.module
2022
# bug 63539 is about a termios without TIOCGWINSZ attribute
2024
orig = termios.TIOCGWINSZ
2025
except AttributeError:
2026
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
2029
self.overrideAttr(termios, 'TIOCGWINSZ')
2030
del termios.TIOCGWINSZ
2031
del os.environ['BZR_COLUMNS']
2032
del os.environ['COLUMNS']
2033
# Whatever the result is, if we don't raise an exception, it's ok.
2034
osutils.terminal_width()
2036
class TestCreationOps(tests.TestCaseInTempDir):
2037
_test_needs_features = [features.chown_feature]
2040
tests.TestCaseInTempDir.setUp(self)
2041
self.overrideAttr(os, 'chown', self._dummy_chown)
2043
# params set by call to _dummy_chown
2044
self.path = self.uid = self.gid = None
2046
def _dummy_chown(self, path, uid, gid):
2047
self.path, self.uid, self.gid = path, uid, gid
2049
def test_copy_ownership_from_path(self):
2050
"""copy_ownership_from_path test with specified src."""
2052
f = open('test_file', 'wt')
2053
osutils.copy_ownership_from_path('test_file', ownsrc)
2056
self.assertEquals(self.path, 'test_file')
2057
self.assertEquals(self.uid, s.st_uid)
2058
self.assertEquals(self.gid, s.st_gid)
2060
def test_copy_ownership_nonesrc(self):
2061
"""copy_ownership_from_path test with src=None."""
2062
f = open('test_file', 'wt')
2063
# should use parent dir for permissions
2064
osutils.copy_ownership_from_path('test_file')
2067
self.assertEquals(self.path, 'test_file')
2068
self.assertEquals(self.uid, s.st_uid)
2069
self.assertEquals(self.gid, s.st_gid)
2071
class TestGetuserUnicode(tests.TestCase):
2073
def test_ascii_user(self):
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2075
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2077
def test_unicode_user(self):
2078
ue = osutils.get_user_encoding()
2079
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2081
raise tests.TestSkipped(
2082
'Cannot find a unicode character that works in encoding %s'
2083
% (osutils.get_user_encoding(),))
2084
uni_username = u'jrandom' + uni_val
2085
encoded_username = uni_username.encode(ue)
2086
osutils.set_or_unset_env('LOGNAME', encoded_username)
2087
self.assertEqual(uni_username, osutils.getuser_unicode())