Fix failing test on windows

When checking for path inside directory we need to use long path names
as the dir path and abspath can use different types of names: short vs.
long.
This commit is contained in:
Kovid Goyal 2023-05-28 19:43:53 +05:30
parent 7444f22e49
commit 737a7ceeb8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 43 additions and 23 deletions

View File

@ -13,7 +13,7 @@ from urllib.parse import quote
from calibre.constants import isbsd, islinux from calibre.constants import isbsd, islinux
from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
from calibre.utils.filenames import ascii_filename from calibre.utils.filenames import ascii_filename, get_long_path_name
from calibre.utils.imghdr import what from calibre.utils.imghdr import what
from calibre.utils.localization import __, get_lang from calibre.utils.localization import __, get_lang
from polyglot.builtins import as_unicode from polyglot.builtins import as_unicode
@ -86,7 +86,7 @@ class HTMLInput(InputFormatPlugin):
if hasattr(stream, 'name'): if hasattr(stream, 'name'):
basedir = os.path.dirname(stream.name) basedir = os.path.dirname(stream.name)
fname = os.path.basename(stream.name) fname = os.path.basename(stream.name)
self.root_dir_of_input = os.path.normcase(os.path.abspath(basedir) + os.sep) self.root_dir_of_input = os.path.normcase(get_long_path_name(os.path.abspath(basedir)) + os.sep)
if file_ext != 'opf': if file_ext != 'opf':
if opts.dont_package: if opts.dont_package:
@ -262,9 +262,10 @@ class HTMLInput(InputFormatPlugin):
if not link: if not link:
return None, None return None, None
link = os.path.abspath(os.path.realpath(link)) link = os.path.abspath(os.path.realpath(link))
if not os.path.normcase(link).startswith(self.root_dir_of_input): q = os.path.normcase(get_long_path_name(link))
if not q.startswith(self.root_dir_of_input):
if not self.opts.allow_local_files_outside_root: if not self.opts.allow_local_files_outside_root:
self.log.warn('Not adding {} as it is outside the document root: {}'.format(link, self.root_dir_of_input)) self.log.warn('Not adding {} as it is outside the document root: {}'.format(q, self.root_dir_of_input))
return None, None return None, None
return link, frag return link, frag

View File

@ -24,7 +24,14 @@ def get_cache():
return cache return cache
once_per_run = set()
def needs_recompile(obj, srcs): def needs_recompile(obj, srcs):
is_ci = os.environ.get('CI', '').lower() == 'true'
if is_ci and obj not in once_per_run:
once_per_run.add(obj)
return True
if isinstance(srcs, str): if isinstance(srcs, str):
srcs = [srcs] srcs = [srcs]
try: try:
@ -39,7 +46,7 @@ def needs_recompile(obj, srcs):
def build_book(src, dest, args=()): def build_book(src, dest, args=()):
from calibre.ebooks.conversion.cli import main from calibre.ebooks.conversion.cli import main
main(['ebook-convert', src, dest] + list(args)) main(['ebook-convert', src, dest, '-vv'] + list(args))
def add_resources(raw, rmap): def add_resources(raw, rmap):
@ -55,22 +62,21 @@ def get_simple_book(fmt='epub'):
ans = os.path.join(cache, 'simple.'+fmt) ans = os.path.join(cache, 'simple.'+fmt)
src = os.path.join(os.path.dirname(__file__), 'simple.html') src = os.path.join(os.path.dirname(__file__), 'simple.html')
if needs_recompile(ans, src): if needs_recompile(ans, src):
with TemporaryDirectory('bpt') as tdir: with TemporaryDirectory('bpt') as tdir, CurrentDir(tdir):
with CurrentDir(tdir): with open(src, 'rb') as sf:
with open(src, 'rb') as sf: raw = sf.read().decode('utf-8')
raw = sf.read().decode('utf-8') raw = add_resources(raw, {
raw = add_resources(raw, { 'LMONOI': P('fonts/liberation/LiberationMono-Italic.ttf'),
'LMONOI': P('fonts/liberation/LiberationMono-Italic.ttf'), 'LMONOR': P('fonts/liberation/LiberationMono-Regular.ttf'),
'LMONOR': P('fonts/liberation/LiberationMono-Regular.ttf'), 'IMAGE1': I('marked.png'),
'IMAGE1': I('marked.png'), 'IMAGE2': I('textures/light_wood.png'),
'IMAGE2': I('textures/light_wood.png'), })
}) shutil.copy2(I('lt.png'), '.')
shutil.copy2(I('lt.png'), '.') x = 'index.html'
x = 'index.html' with open(x, 'wb') as f:
with open(x, 'wb') as f: f.write(raw.encode('utf-8'))
f.write(raw.encode('utf-8')) build_book(x, ans, args=[
build_book(x, ans, args=[ '--level1-toc=//h:h2', '--language=en', '--authors=Kovid Goyal', '--cover=lt.png'])
'--level1-toc=//h:h2', '--language=en', '--authors=Kovid Goyal', '--cover=lt.png', '--allow-local-files-outside-root'])
return ans return ans
@ -85,7 +91,7 @@ def get_split_book(fmt='epub'):
with open(x, 'wb') as f: with open(x, 'wb') as f:
f.write(raw.encode('utf-8')) f.write(raw.encode('utf-8'))
build_book(x, ans, args=['--level1-toc=//h:h2', '--language=en', '--authors=Kovid Goyal', build_book(x, ans, args=['--level1-toc=//h:h2', '--language=en', '--authors=Kovid Goyal',
'--cover=' + I('lt.png'), '--allow-local-files-outside-root']) '--cover=' + I('lt.png')])
finally: finally:
os.remove(x) os.remove(x)
return ans return ans

View File

@ -620,10 +620,20 @@ if iswindows:
return False return False
# Values I have seen: FAT32, exFAT, NTFS # Values I have seen: FAT32, exFAT, NTFS
return tn.upper().startswith('FAT') return tn.upper().startswith('FAT')
def get_long_path_name(path):
from calibre_extensions.winutil import get_long_path_name
if os.path.isabs(path) and not path.startswith(long_path_prefix):
path = long_path_prefix + path
return get_long_path_name(path)
else: else:
def make_long_path_useable(path): def make_long_path_useable(path):
return path return path
def get_long_path_name(path):
return path
def is_fat_filesystem(path): def is_fat_filesystem(path):
# TODO: Implement for Linux and macOS # TODO: Implement for Linux and macOS
return False return False

View File

@ -6,6 +6,9 @@ import unittest, functools, importlib, importlib.resources, os
from calibre.utils.monotonic import monotonic from calibre.utils.monotonic import monotonic
is_ci = os.environ.get('CI', '').lower() == 'true'
def no_endl(f): def no_endl(f):
@functools.wraps(f) @functools.wraps(f)
def func(*args, **kwargs): def func(*args, **kwargs):
@ -329,6 +332,6 @@ def run_cli(suite, verbosity=4, buffer=True):
r = unittest.TextTestRunner r = unittest.TextTestRunner
r.resultclass = unittest.TextTestResult if verbosity < 2 else TestResult r.resultclass = unittest.TextTestResult if verbosity < 2 else TestResult
init_env() init_env()
result = r(verbosity=verbosity, buffer=buffer).run(suite) result = r(verbosity=verbosity, buffer=buffer and not is_ci).run(suite)
if not result.wasSuccessful(): if not result.wasSuccessful():
raise SystemExit(1) raise SystemExit(1)