194
203
joined = urlutils.join(*args)
195
204
self.assertEqual(expected, joined)
197
# Test a single element
200
206
# Test relative path joining
207
test('foo', 'foo') # relative fragment with nothing is preserved.
201
208
test('foo/bar', 'foo', 'bar')
202
209
test('http://foo/bar', 'http://foo', 'bar')
203
210
test('http://foo/bar', 'http://foo', '.', 'bar')
204
211
test('http://foo/baz', 'http://foo', 'bar', '../baz')
205
212
test('http://foo/bar/baz', 'http://foo', 'bar/baz')
206
213
test('http://foo/baz', 'http://foo', 'bar/../baz')
214
test('http://foo/baz', 'http://foo/bar/', '../baz')
217
test('http://foo', 'http://foo') # abs url with nothing is preserved.
209
218
test('http://bar', 'http://foo', 'http://bar')
210
219
test('sftp://bzr/foo', 'http://foo', 'bar', 'sftp://bzr/foo')
211
220
test('file:///bar', 'foo', 'file:///bar')
221
test('http://bar/', 'http://foo', 'http://bar/')
222
test('http://bar/a', 'http://foo', 'http://bar/a')
223
test('http://bar/a/', 'http://foo', 'http://bar/a/')
226
test('file:///foo', 'file:///', 'foo')
227
test('file:///bar/foo', 'file:///bar/', 'foo')
228
test('http://host/foo', 'http://host/', 'foo')
229
test('http://host/', 'http://host', '')
213
231
# Invalid joinings
214
232
# Cannot go above root
233
# Implicitly at root:
215
234
self.assertRaises(InvalidURLJoin, urlutils.join,
216
235
'http://foo', '../baz')
236
self.assertRaises(InvalidURLJoin, urlutils.join,
238
# Joining from a path explicitly under the root.
239
self.assertRaises(InvalidURLJoin, urlutils.join,
240
'http://foo/a', '../../b')
242
def test_joinpath(self):
243
def test(expected, *args):
244
joined = urlutils.joinpath(*args)
245
self.assertEqual(expected, joined)
247
# Test a single element
250
# Test relative path joining
251
test('foo/bar', 'foo', 'bar')
252
test('foo/bar', 'foo', '.', 'bar')
253
test('foo/baz', 'foo', 'bar', '../baz')
254
test('foo/bar/baz', 'foo', 'bar/baz')
255
test('foo/baz', 'foo', 'bar/../baz')
257
# Test joining to an absolute path
259
test('/foo', '/foo', '.')
260
test('/foo/bar', '/foo', 'bar')
261
test('/', '/foo', '..')
263
# Test joining with an absolute path
264
test('/bar', 'foo', '/bar')
266
# Test joining to a path with a trailing slash
267
test('foo/bar', 'foo/', 'bar')
270
# Cannot go above root
271
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '../baz')
272
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '..')
273
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '/..')
218
275
def test_function_type(self):
219
276
if sys.platform == 'win32':
266
350
from_url('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
267
351
self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
268
352
from_url('file:///d:/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
353
self.assertEqual('/', from_url('file:///'))
270
355
self.assertRaises(InvalidURL, from_url, '/path/to/foo')
271
356
# Not a valid _win32 url, no drive letter
272
357
self.assertRaises(InvalidURL, from_url, 'file:///path/to/foo')
359
def test_win32_unc_path_from_url(self):
360
from_url = urlutils._win32_local_path_from_url
361
self.assertEqual('//HOST/path', from_url('file://HOST/path'))
362
# despite IE allows 2, 4, 5 and 6 slashes in URL to another machine
363
# we want to use only 2 slashes
364
# Firefox understand only 5 slashes in URL, but it's ugly
365
self.assertRaises(InvalidURL, from_url, 'file:////HOST/path')
366
self.assertRaises(InvalidURL, from_url, 'file://///HOST/path')
367
self.assertRaises(InvalidURL, from_url, 'file://////HOST/path')
368
# check for file://C:/ instead of file:///C:/
369
self.assertRaises(InvalidURL, from_url, 'file://C:/path')
371
def test_win32_extract_drive_letter(self):
372
extract = urlutils._win32_extract_drive_letter
373
self.assertEqual(('file:///C:', '/foo'), extract('file://', '/C:/foo'))
374
self.assertEqual(('file:///d|', '/path'), extract('file://', '/d|/path'))
375
self.assertRaises(InvalidURL, extract, 'file://', '/path')
274
377
def test_split(self):
275
378
# Test bzrlib.urlutils.split()
276
379
split = urlutils.split
306
409
self.assertEqual(('path/..', 'foo'), split('path/../foo'))
307
410
self.assertEqual(('../path', 'foo'), split('../path/foo'))
412
def test_win32_strip_local_trailing_slash(self):
413
strip = urlutils._win32_strip_local_trailing_slash
414
self.assertEqual('file://', strip('file://'))
415
self.assertEqual('file:///', strip('file:///'))
416
self.assertEqual('file:///C', strip('file:///C'))
417
self.assertEqual('file:///C:', strip('file:///C:'))
418
self.assertEqual('file:///d|', strip('file:///d|'))
419
self.assertEqual('file:///C:/', strip('file:///C:/'))
420
self.assertEqual('file:///C:/a', strip('file:///C:/a/'))
309
422
def test_strip_trailing_slash(self):
310
423
sts = urlutils.strip_trailing_slash
311
424
if sys.platform == 'win32':
432
546
test('http://host/', 'http://host', 'http://host/')
433
547
#test('.', 'http://host/', 'http://host')
434
548
test('http://host', 'http://host/', 'http://host')
550
# On Windows file:///C:/path/to and file:///D:/other/path
551
# should not use relative url over the non-existent '/' directory.
552
if sys.platform == 'win32':
554
test('../../other/path',
555
'file:///C:/path/to', 'file:///C:/other/path')
556
#~next two tests is failed, i.e. urlutils.relative_url expects
557
#~to see normalized file URLs?
558
#~test('../../other/path',
559
#~ 'file:///C:/path/to', 'file:///c:/other/path')
560
#~test('../../other/path',
561
#~ 'file:///C:/path/to', 'file:///C|/other/path')
563
# check UNC paths too
564
test('../../other/path',
565
'file://HOST/base/path/to', 'file://HOST/base/other/path')
566
# on different drives
567
test('file:///D:/other/path',
568
'file:///C:/path/to', 'file:///D:/other/path')
569
# TODO: strictly saying in UNC path //HOST/base is full analog
570
# of drive letter for hard disk, and this situation is also
571
# should be exception from rules. [bialix 20071221]
574
class TestCwdToURL(TestCaseInTempDir):
575
"""Test that local_path_to_url works base on the cwd"""
578
# This test will fail if getcwd is not ascii
582
url = urlutils.local_path_to_url('.')
583
self.assertEndsWith(url, '/mytest')
585
def test_non_ascii(self):
586
if win32utils.winver == 'Windows 98':
587
raise TestSkipped('Windows 98 cannot handle unicode filenames')
592
raise TestSkipped('cannot create unicode directory')
596
# On Mac OSX this directory is actually:
597
# u'/dode\u0301' => '/dode\xcc\x81
598
# but we should normalize it back to
599
# u'/dod\xe9' => '/dod\xc3\xa9'
600
url = urlutils.local_path_to_url('.')
601
self.assertEndsWith(url, '/dod%C3%A9')
604
class TestDeriveToLocation(TestCase):
605
"""Test that the mapping of FROM_LOCATION to TO_LOCATION works."""
607
def test_to_locations_derived_from_paths(self):
608
derive = urlutils.derive_to_location
609
self.assertEqual("bar", derive("bar"))
610
self.assertEqual("bar", derive("../bar"))
611
self.assertEqual("bar", derive("/foo/bar"))
612
self.assertEqual("bar", derive("c:/foo/bar"))
613
self.assertEqual("bar", derive("c:bar"))
615
def test_to_locations_derived_from_urls(self):
616
derive = urlutils.derive_to_location
617
self.assertEqual("bar", derive("http://foo/bar"))
618
self.assertEqual("bar", derive("bzr+ssh://foo/bar"))
619
self.assertEqual("foo-bar", derive("lp:foo-bar"))
622
class TestRebaseURL(TestCase):
623
"""Test the behavior of rebase_url."""
625
def test_non_relative(self):
626
result = urlutils.rebase_url('file://foo', 'file://foo',
628
self.assertEqual('file://foo', result)
629
result = urlutils.rebase_url('/foo', 'file://foo',
631
self.assertEqual('/foo', result)
633
def test_different_ports(self):
634
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
635
'foo', 'http://bar:80', 'http://bar:81')
636
self.assertEqual(str(e), "URLs differ by more than path:"
637
" 'http://bar:80' and 'http://bar:81'")
639
def test_different_hosts(self):
640
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
641
'foo', 'http://bar', 'http://baz')
642
self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
645
def test_different_protocol(self):
646
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
647
'foo', 'http://bar', 'ftp://bar')
648
self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
651
def test_rebase_success(self):
652
self.assertEqual('../bar', urlutils.rebase_url('bar', 'http://baz/',
654
self.assertEqual('qux/bar', urlutils.rebase_url('bar',
655
'http://baz/qux', 'http://baz/'))
656
self.assertEqual('.', urlutils.rebase_url('foo',
657
'http://bar/', 'http://bar/foo/'))
658
self.assertEqual('qux/bar', urlutils.rebase_url('../bar',
659
'http://baz/qux/foo', 'http://baz/'))
661
def test_determine_relative_path(self):
662
self.assertEqual('../../baz/bar',
663
urlutils.determine_relative_path(
664
'/qux/quxx', '/baz/bar'))
665
self.assertEqual('..',
666
urlutils.determine_relative_path(
668
self.assertEqual('baz',
669
urlutils.determine_relative_path(
671
self.assertEqual('.', urlutils.determine_relative_path(