mirror of
				https://github.com/kovidgoyal/calibre.git
				synced 2025-11-03 19:17:02 -05:00 
			
		
		
		
	Use the importlib resource infrastructure to run tests from directories
That way the tests will work even in frozen builds
This commit is contained in:
		
							parent
							
								
									cc9a40868e
								
							
						
					
					
						commit
						2a0bdcfcbb
					
				@ -6,13 +6,11 @@ __license__   = 'GPL v3'
 | 
				
			|||||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
					__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
				
			||||||
__docformat__ = 'restructuredtext en'
 | 
					__docformat__ = 'restructuredtext en'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import os
 | 
					from calibre.utils.run_tests import find_tests_in_package, run_tests
 | 
				
			||||||
from calibre.utils.run_tests import find_tests_in_dir, run_tests
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def find_tests():
 | 
					def find_tests():
 | 
				
			||||||
    base = os.path.dirname(os.path.abspath(__file__))
 | 
					    return find_tests_in_package('calibre.db.tests')
 | 
				
			||||||
    return find_tests_in_dir(base)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
				
			|||||||
@ -5,13 +5,11 @@
 | 
				
			|||||||
__license__ = 'GPL v3'
 | 
					__license__ = 'GPL v3'
 | 
				
			||||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
					__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import os
 | 
					from calibre.utils.run_tests import find_tests_in_package, run_tests
 | 
				
			||||||
from calibre.utils.run_tests import find_tests_in_dir, run_tests
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def find_tests():
 | 
					def find_tests():
 | 
				
			||||||
    base = os.path.dirname(os.path.abspath(__file__))
 | 
					    return find_tests_in_package('calibre.ebooks.oeb.polish.tests')
 | 
				
			||||||
    return find_tests_in_dir(base)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
				
			|||||||
@ -5,13 +5,11 @@
 | 
				
			|||||||
__license__ = 'GPL v3'
 | 
					__license__ = 'GPL v3'
 | 
				
			||||||
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
					__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import os
 | 
					from calibre.utils.run_tests import find_tests_in_package, run_tests
 | 
				
			||||||
from calibre.utils.run_tests import find_tests_in_dir, run_tests
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def find_tests():
 | 
					def find_tests():
 | 
				
			||||||
    base = os.path.dirname(os.path.abspath(__file__))
 | 
					    return find_tests_in_package('calibre.srv.tests')
 | 
				
			||||||
    return find_tests_in_dir(base)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,7 @@
 | 
				
			|||||||
# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
 | 
					# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import unittest, functools, os, importlib, zipfile
 | 
					import unittest, functools, importlib
 | 
				
			||||||
from calibre.utils.monotonic import monotonic
 | 
					from calibre.utils.monotonic import monotonic
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -53,20 +53,9 @@ class TestResult(unittest.TextTestResult):
 | 
				
			|||||||
                self.stream.writeln('\nSlowest tests: %s' % ' '.join(slowest))
 | 
					                self.stream.writeln('\nSlowest tests: %s' % ' '.join(slowest))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def find_tests_in_dir(path, excludes=('main.py',)):
 | 
					def find_tests_in_package(package, excludes=('main.py',)):
 | 
				
			||||||
    if not os.path.exists(path) and '.zip' in path:
 | 
					    loader = importlib.import_module(package).__spec__.loader
 | 
				
			||||||
        idx = path.rfind('.zip')
 | 
					    items = list(loader.contents())
 | 
				
			||||||
        zf = path[:idx+4]
 | 
					 | 
				
			||||||
        prefix = os.path.relpath(path, zf).replace(os.sep, '/')
 | 
					 | 
				
			||||||
        package = prefix.replace('/', '.')
 | 
					 | 
				
			||||||
        with zipfile.ZipFile(zf) as f:
 | 
					 | 
				
			||||||
            namelist = f.namelist()
 | 
					 | 
				
			||||||
        items = [i for i in namelist if i.startswith(prefix) and i.count('/') == prefix.count('/') + 1]
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
        d = os.path.dirname
 | 
					 | 
				
			||||||
        base = d(d(d(os.path.abspath(__file__))))
 | 
					 | 
				
			||||||
        package = os.path.relpath(path, base).replace(os.sep, '/').replace('/', '.')
 | 
					 | 
				
			||||||
        items = os.listdir(path)
 | 
					 | 
				
			||||||
    suits = []
 | 
					    suits = []
 | 
				
			||||||
    for x in items:
 | 
					    for x in items:
 | 
				
			||||||
        if x.endswith('.py') and x not in excludes:
 | 
					        if x.endswith('.py') and x not in excludes:
 | 
				
			||||||
 | 
				
			|||||||
@ -5,12 +5,13 @@
 | 
				
			|||||||
__license__ = 'GPL v3'
 | 
					__license__ = 'GPL v3'
 | 
				
			||||||
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
					__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import unittest, os, argparse
 | 
					import unittest, argparse
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def find_tests():
 | 
					def find_tests():
 | 
				
			||||||
    from calibre.utils.run_tests import find_tests_in_dir
 | 
					    from calibre.utils.run_tests import find_tests_in_package
 | 
				
			||||||
    base = os.path.dirname(os.path.abspath(__file__))
 | 
					    return find_tests_in_package('tinycss.tests')
 | 
				
			||||||
    return find_tests_in_dir(base)
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def run_tests(find_tests=find_tests, for_build=False):
 | 
					def run_tests(find_tests=find_tests, for_build=False):
 | 
				
			||||||
    if not for_build:
 | 
					    if not for_build:
 | 
				
			||||||
@ -36,7 +37,7 @@ def run_tests(find_tests=find_tests, for_build=False):
 | 
				
			|||||||
        except StopIteration:
 | 
					        except StopIteration:
 | 
				
			||||||
            pass
 | 
					            pass
 | 
				
			||||||
        if ans is None:
 | 
					        if ans is None:
 | 
				
			||||||
            print ('No test named %s found' % args.name)
 | 
					            print('No test named %s found' % args.name)
 | 
				
			||||||
            raise SystemExit(1)
 | 
					            raise SystemExit(1)
 | 
				
			||||||
        tests = ans
 | 
					        tests = ans
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
@ -50,7 +51,6 @@ def run_tests(find_tests=find_tests, for_build=False):
 | 
				
			|||||||
    if for_build and result.errors or result.failures:
 | 
					    if for_build and result.errors or result.failures:
 | 
				
			||||||
        raise SystemExit(1)
 | 
					        raise SystemExit(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    run_tests()
 | 
					    run_tests()
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user