Add a test for the new basename

Also handle paths like X:\ on windows
This commit is contained in:
Kovid Goyal 2017-06-17 10:56:41 +05:30
parent fdbe5ae812
commit df660f4f9a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 18 additions and 2 deletions

View File

@ -32,7 +32,12 @@ def samefile(a, b):
def basename(path):
while path and path[-1] in ('/' + os.sep):
path = path[:-1]
return os.path.basename(path)
ans = os.path.basename(path)
if not ans:
# Can happen for a path like D:\ on windows
if len(path) == 2 and path[1] == ':':
ans = path[0]
return ans or 'Library'
def init_library(library_path, is_default_library):
@ -52,7 +57,7 @@ def make_library_id_unique(library_id, existing):
return library_id
def library_id_from_path(path, existing):
def library_id_from_path(path, existing=frozenset()):
library_id = basename(path).replace(' ', '_')
return make_library_id_unique(library_id, existing)

View File

@ -6,11 +6,22 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3'
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import os
from calibre.srv.tests.base import BaseTest
class TestRouter(BaseTest):
def test_library_id_construction(self):
from calibre.srv.library_broker import library_id_from_path
self.ae(library_id_from_path('as'), 'as')
self.ae(library_id_from_path('as/'), 'as')
self.ae(library_id_from_path('as////'), 'as')
self.ae(library_id_from_path('/as/'), 'as')
if os.sep == '\\':
self.ae(library_id_from_path('as/' + os.sep), 'as')
self.ae(library_id_from_path('X:' + os.sep), 'X')
def test_route_construction(self):
' Test route construction '
from calibre.srv.routes import Route, endpoint, RouteError