mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add a test for the new basename
Also handle paths like X:\ on windows
This commit is contained in:
parent
fdbe5ae812
commit
df660f4f9a
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user