126
124
class TestRename(tests.TestCaseInTempDir):
126
def create_file(self, filename, content):
127
f = open(filename, 'wb')
133
def _fancy_rename(self, a, b):
134
osutils.fancy_rename(a, b, rename_func=os.rename,
135
unlink_func=os.unlink)
128
137
def test_fancy_rename(self):
129
138
# 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')
139
self.create_file('a', 'something in a\n')
140
self._fancy_rename('a', 'b')
137
141
self.failIfExists('a')
138
142
self.failUnlessExists('b')
139
143
self.check_file_contents('b', 'something in a\n')
141
open('a', 'wb').write('new something in a\n')
145
self.create_file('a', 'new something in a\n')
146
self._fancy_rename('b', 'a')
144
148
self.check_file_contents('a', 'something in a\n')
150
def test_fancy_rename_fails_source_missing(self):
151
# An exception should be raised, and the target should be left in place
152
self.create_file('target', 'data in target\n')
153
self.assertRaises((IOError, OSError), self._fancy_rename,
154
'missingsource', 'target')
155
self.failUnlessExists('target')
156
self.check_file_contents('target', 'data in target\n')
158
def test_fancy_rename_fails_if_source_and_target_missing(self):
159
self.assertRaises((IOError, OSError), self._fancy_rename,
160
'missingsource', 'missingtarget')
146
162
def test_rename(self):
147
163
# Rename should be semi-atomic on all platforms
148
open('a', 'wb').write('something in a\n')
164
self.create_file('a', 'something in a\n')
149
165
osutils.rename('a', 'b')
150
166
self.failIfExists('a')
151
167
self.failUnlessExists('b')
152
168
self.check_file_contents('b', 'something in a\n')
154
open('a', 'wb').write('new something in a\n')
170
self.create_file('a', 'new something in a\n')
155
171
osutils.rename('b', 'a')
157
173
self.check_file_contents('a', 'something in a\n')
446
472
def test_canonical_relpath_simple(self):
447
473
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')
475
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
452
476
self.failUnlessEqual('work/MixedCaseName', actual)
454
478
def test_canonical_relpath_missing_tail(self):
455
479
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,
480
actual = osutils.canonical_relpath(self.test_base_dir,
459
481
'mixedcaseparent/nochild')
460
482
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
485
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
487
def assertRelpath(self, expected, base, path):
488
actual = osutils._cicp_canonical_relpath(base, path)
489
self.assertEqual(expected, actual)
491
def test_simple(self):
492
self.build_tree(['MixedCaseName'])
493
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
494
self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
496
def test_subdir_missing_tail(self):
497
self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
498
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
499
self.assertRelpath('MixedCaseParent/a_child', base,
500
'MixedCaseParent/a_child')
501
self.assertRelpath('MixedCaseParent/a_child', base,
502
'MixedCaseParent/A_Child')
503
self.assertRelpath('MixedCaseParent/not_child', base,
504
'MixedCaseParent/not_child')
506
def test_at_root_slash(self):
507
# We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
509
if osutils.MIN_ABS_PATHLENGTH > 1:
510
raise tests.TestSkipped('relpath requires %d chars'
511
% osutils.MIN_ABS_PATHLENGTH)
512
self.assertRelpath('foo', '/', '/foo')
514
def test_at_root_drive(self):
515
if sys.platform != 'win32':
516
raise tests.TestNotApplicable('we can only test drive-letter relative'
517
' paths on Windows where we have drive'
520
# The specific issue is that when at the root of a drive, 'abspath'
521
# returns "C:/" or just "/". However, the code assumes that abspath
522
# always returns something like "C:/foo" or "/foo" (no trailing slash).
523
self.assertRelpath('foo', 'C:/', 'C:/foo')
524
self.assertRelpath('foo', 'X:/', 'X:/foo')
525
self.assertRelpath('foo', 'X:/', 'X://foo')
463
528
class TestPumpFile(tests.TestCase):
464
529
"""Test pumpfile method."""
1777
1848
class TestConcurrency(tests.TestCase):
1851
super(TestConcurrency, self).setUp()
1852
self.overrideAttr(osutils, '_cached_local_concurrency')
1779
1854
def test_local_concurrency(self):
1780
1855
concurrency = osutils.local_concurrency()
1781
1856
self.assertIsInstance(concurrency, int)
1858
def test_local_concurrency_environment_variable(self):
1859
os.environ['BZR_CONCURRENCY'] = '2'
1860
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1861
os.environ['BZR_CONCURRENCY'] = '3'
1862
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1863
os.environ['BZR_CONCURRENCY'] = 'foo'
1864
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1866
def test_option_concurrency(self):
1867
os.environ['BZR_CONCURRENCY'] = '1'
1868
self.run_bzr('rocks --concurrency 42')
1869
# Command line overrides envrionment variable
1870
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1871
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1874
class TestFailedToLoadExtension(tests.TestCase):
1876
def _try_loading(self):
1878
import bzrlib._fictional_extension_py
1879
except ImportError, e:
1880
osutils.failed_to_load_extension(e)
1884
super(TestFailedToLoadExtension, self).setUp()
1885
self.overrideAttr(osutils, '_extension_load_failures', [])
1887
def test_failure_to_load(self):
1889
self.assertLength(1, osutils._extension_load_failures)
1890
self.assertEquals(osutils._extension_load_failures[0],
1891
"No module named _fictional_extension_py")
1893
def test_report_extension_load_failures_no_warning(self):
1894
self.assertTrue(self._try_loading())
1895
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
1896
# it used to give a Python warning; it no longer does
1897
self.assertLength(0, warnings)
1899
def test_report_extension_load_failures_message(self):
1901
trace.push_log_file(log)
1902
self.assertTrue(self._try_loading())
1903
osutils.report_extension_load_failures()
1904
self.assertContainsRe(
1906
r"bzr: warning: some compiled extensions could not be loaded; "
1907
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1911
class TestTerminalWidth(tests.TestCase):
1913
def replace_stdout(self, new):
1914
self.overrideAttr(sys, 'stdout', new)
1916
def replace__terminal_size(self, new):
1917
self.overrideAttr(osutils, '_terminal_size', new)
1919
def set_fake_tty(self):
1921
class I_am_a_tty(object):
1925
self.replace_stdout(I_am_a_tty())
1927
def test_default_values(self):
1928
self.assertEqual(80, osutils.default_terminal_width)
1930
def test_defaults_to_BZR_COLUMNS(self):
1931
# BZR_COLUMNS is set by the test framework
1932
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1933
os.environ['BZR_COLUMNS'] = '12'
1934
self.assertEqual(12, osutils.terminal_width())
1936
def test_falls_back_to_COLUMNS(self):
1937
del os.environ['BZR_COLUMNS']
1938
self.assertNotEqual('42', os.environ['COLUMNS'])
1940
os.environ['COLUMNS'] = '42'
1941
self.assertEqual(42, osutils.terminal_width())
1943
def test_tty_default_without_columns(self):
1944
del os.environ['BZR_COLUMNS']
1945
del os.environ['COLUMNS']
1947
def terminal_size(w, h):
1951
# We need to override the osutils definition as it depends on the
1952
# running environment that we can't control (PQM running without a
1953
# controlling terminal is one example).
1954
self.replace__terminal_size(terminal_size)
1955
self.assertEqual(42, osutils.terminal_width())
1957
def test_non_tty_default_without_columns(self):
1958
del os.environ['BZR_COLUMNS']
1959
del os.environ['COLUMNS']
1960
self.replace_stdout(None)
1961
self.assertEqual(None, osutils.terminal_width())
1963
def test_no_TIOCGWINSZ(self):
1964
self.requireFeature(term_ios_feature)
1965
termios = term_ios_feature.module
1966
# bug 63539 is about a termios without TIOCGWINSZ attribute
1968
orig = termios.TIOCGWINSZ
1969
except AttributeError:
1970
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
1973
self.overrideAttr(termios, 'TIOCGWINSZ')
1974
del termios.TIOCGWINSZ
1975
del os.environ['BZR_COLUMNS']
1976
del os.environ['COLUMNS']
1977
# Whatever the result is, if we don't raise an exception, it's ok.
1978
osutils.terminal_width()
1980
class TestCreationOps(tests.TestCaseInTempDir):
1981
_test_needs_features = [features.chown_feature]
1984
tests.TestCaseInTempDir.setUp(self)
1985
self.overrideAttr(os, 'chown', self._dummy_chown)
1987
# params set by call to _dummy_chown
1988
self.path = self.uid = self.gid = None
1990
def _dummy_chown(self, path, uid, gid):
1991
self.path, self.uid, self.gid = path, uid, gid
1993
def test_copy_ownership_from_path(self):
1994
"""copy_ownership_from_path test with specified src."""
1996
f = open('test_file', 'wt')
1997
osutils.copy_ownership_from_path('test_file', ownsrc)
2000
self.assertEquals(self.path, 'test_file')
2001
self.assertEquals(self.uid, s.st_uid)
2002
self.assertEquals(self.gid, s.st_gid)
2004
def test_copy_ownership_nonesrc(self):
2005
"""copy_ownership_from_path test with src=None."""
2006
f = open('test_file', 'wt')
2007
# should use parent dir for permissions
2008
osutils.copy_ownership_from_path('test_file')
2011
self.assertEquals(self.path, 'test_file')
2012
self.assertEquals(self.uid, s.st_uid)
2013
self.assertEquals(self.gid, s.st_gid)
2015
class TestGetuserUnicode(tests.TestCase):
2017
def test_ascii_user(self):
2018
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2019
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2021
def test_unicode_user(self):
2022
ue = osutils.get_user_encoding()
2023
osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
2024
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())