1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
import os
import shutil
import unittest
from senf import uri2fsn, fsn2uri, fsnative
from quodlibet.util.path import iscommand, limit_path, \
get_home_dir, uri_is_valid, is_hidden, uri2gsturi
from quodlibet.util import print_d
from . import TestCase, skipIf
is_win = os.name == "nt"
path_set = bool(os.environ.get('PATH', False))
def test_uri2gsturi():
assert uri2gsturi("file:///foo/bar") == "file:///foo/bar"
if is_win:
assert uri2gsturi("file://foo/bar") == "file:////foo/bar"
assert uri2gsturi("https://foo.bar.org") == "https://foo.bar.org"
class Tishidden(TestCase):
@skipIf(is_win, "unix-like hidden")
def test_leading_dot(self):
assert is_hidden(fsnative("."))
assert is_hidden(fsnative("foo/.bar"))
def test_normal_names_not_hidden(self):
assert not is_hidden(fsnative("foo"))
assert not is_hidden(fsnative(".foo/bar"))
def test_multiple_dots(self):
assert not is_hidden(fsnative("...and Justice For All.flac"))
class Turi(TestCase):
def test_uri2fsn(self):
if os.name != "nt":
path = uri2fsn("file:///home/piman/cr%21azy")
self.assertTrue(isinstance(path, fsnative))
self.assertEqual(path, fsnative(u"/home/piman/cr!azy"))
else:
path = uri2fsn("file:///C:/foo")
self.assertTrue(isinstance(path, fsnative))
self.assertEqual(path, fsnative(u"C:\\foo"))
def test_uri2fsn_invalid(self):
self.assertRaises(ValueError, uri2fsn, "http://example.com")
def test_path_as_uri(self):
if os.name != "nt":
self.assertRaises(ValueError, uri2fsn, "/foo")
else:
self.assertRaises(ValueError, uri2fsn, u"C:\\foo")
def test_fsn2uri(self):
if os.name != "nt":
uri = fsn2uri(fsnative(u"/öäü.txt"))
self.assertEqual(uri, u"file:///%C3%B6%C3%A4%C3%BC.txt")
else:
uri = fsn2uri(fsnative(u"C:\\öäü.txt"))
self.assertEqual(
uri, "file:///C:/%C3%B6%C3%A4%C3%BC.txt")
self.assertEqual(
fsn2uri(u"C:\\SomeDir\xe4"), "file:///C:/SomeDir%C3%A4")
def test_roundtrip(self):
if os.name == "nt":
paths = [u"C:\\öäü.txt"]
else:
paths = [u"/öäü.txt", u"/a/foo/bar", u"/a/b/foo/bar"]
for source in paths:
path = uri2fsn(fsn2uri(fsnative(source)))
self.assertTrue(isinstance(path, fsnative))
self.assertEqual(path, fsnative(source))
def test_win_unc_path(self):
if os.name == "nt":
self.assertEqual(
fsn2uri(u"\\\\server\\share\\path"),
u"file://server/share/path")
def test_uri_is_valid(self):
self.assertTrue(uri_is_valid(u"file:///foo"))
self.assertTrue(uri_is_valid(u"file:///C:/foo"))
self.assertTrue(uri_is_valid(u"http://www.example.com"))
self.assertFalse(uri_is_valid(u"/bla"))
self.assertFalse(uri_is_valid(u"test"))
self.assertFalse(uri_is_valid(u""))
assert not uri_is_valid(u"file:///öäü")
assert not uri_is_valid(u"file:///öäü".encode("utf-8"))
class Tget_x_dir(TestCase):
def test_get_home_dir(self):
self.assertTrue(isinstance(get_home_dir(), fsnative))
self.assertTrue(os.path.isabs(get_home_dir()))
class Tlimit_path(TestCase):
def test_main(self):
if os.name == "nt":
path = u'C:\\foobar\\ä%s\\%s' % ("x" * 300, "x" * 300)
path = limit_path(path)
self.failUnlessEqual(len(path), 3 + 6 + 1 + 255 + 1 + 255)
else:
path = '/foobar/ä%s/%s' % ("x" * 300, "x" * 300)
path = limit_path(path)
self.failUnlessEqual(len(path), 1 + 6 + 1 + 255 + 1 + 255)
path = fsnative(u"foo%s.ext" % (u"x" * 300))
new = limit_path(path, ellipsis=False)
self.assertTrue(isinstance(new, fsnative))
self.assertEqual(len(new), 255)
self.assertTrue(new.endswith(fsnative(u"xx.ext")))
new = limit_path(path)
self.assertTrue(isinstance(new, fsnative))
self.assertEqual(len(new), 255)
self.assertTrue(new.endswith(fsnative(u"...ext")))
self.assertTrue(isinstance(limit_path(fsnative()), fsnative))
self.assertEqual(limit_path(fsnative()), fsnative())
class Tiscommand(TestCase):
@unittest.skipIf(is_win, "Unix only")
def test_unix(self):
self.failUnless(iscommand("ls"))
self.failUnless(iscommand(shutil.which("ls")))
self.failUnless(iscommand("whoami"))
def test_both(self):
self.failIf(iscommand("zzzzzzzzz"))
self.failIf(iscommand("/bin/zzzzzzzzz"))
self.failIf(iscommand(""))
self.failIf(iscommand("/bin"))
self.failIf(iscommand("X11"))
@unittest.skipUnless(path_set, "Can only test with a valid $PATH")
@unittest.skipIf(is_win, "needs porting")
def test_looks_in_path(self):
path_dirs = set(os.environ['PATH'].split(os.path.pathsep))
dirs = path_dirs - set(os.defpath.split(os.path.pathsep))
for d in dirs:
if os.path.isdir(d):
for file_path in sorted(os.listdir(d)):
p = os.path.join(d, file_path)
if os.path.isfile(p) and os.access(p, os.X_OK):
print_d("Testing %s" % p)
self.failUnless(iscommand(p), msg=p)
return
|