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>'
 | 
			
		||||
__docformat__ = 'restructuredtext en'
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
from calibre.utils.run_tests import find_tests_in_dir, run_tests
 | 
			
		||||
from calibre.utils.run_tests import find_tests_in_package, run_tests
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def find_tests():
 | 
			
		||||
    base = os.path.dirname(os.path.abspath(__file__))
 | 
			
		||||
    return find_tests_in_dir(base)
 | 
			
		||||
    return find_tests_in_package('calibre.db.tests')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
 | 
			
		||||
@ -5,13 +5,11 @@
 | 
			
		||||
__license__ = 'GPL v3'
 | 
			
		||||
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
from calibre.utils.run_tests import find_tests_in_dir, run_tests
 | 
			
		||||
from calibre.utils.run_tests import find_tests_in_package, run_tests
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def find_tests():
 | 
			
		||||
    base = os.path.dirname(os.path.abspath(__file__))
 | 
			
		||||
    return find_tests_in_dir(base)
 | 
			
		||||
    return find_tests_in_package('calibre.ebooks.oeb.polish.tests')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
 | 
			
		||||
@ -5,13 +5,11 @@
 | 
			
		||||
__license__ = 'GPL v3'
 | 
			
		||||
__copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
			
		||||
 | 
			
		||||
import os
 | 
			
		||||
from calibre.utils.run_tests import find_tests_in_dir, run_tests
 | 
			
		||||
from calibre.utils.run_tests import find_tests_in_package, run_tests
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def find_tests():
 | 
			
		||||
    base = os.path.dirname(os.path.abspath(__file__))
 | 
			
		||||
    return find_tests_in_dir(base)
 | 
			
		||||
    return find_tests_in_package('calibre.srv.tests')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
 | 
			
		||||
@ -3,7 +3,7 @@
 | 
			
		||||
# 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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -53,20 +53,9 @@ class TestResult(unittest.TextTestResult):
 | 
			
		||||
                self.stream.writeln('\nSlowest tests: %s' % ' '.join(slowest))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def find_tests_in_dir(path, excludes=('main.py',)):
 | 
			
		||||
    if not os.path.exists(path) and '.zip' in path:
 | 
			
		||||
        idx = path.rfind('.zip')
 | 
			
		||||
        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)
 | 
			
		||||
def find_tests_in_package(package, excludes=('main.py',)):
 | 
			
		||||
    loader = importlib.import_module(package).__spec__.loader
 | 
			
		||||
    items = list(loader.contents())
 | 
			
		||||
    suits = []
 | 
			
		||||
    for x in items:
 | 
			
		||||
        if x.endswith('.py') and x not in excludes:
 | 
			
		||||
 | 
			
		||||
@ -5,12 +5,13 @@
 | 
			
		||||
__license__ = 'GPL v3'
 | 
			
		||||
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
 | 
			
		||||
 | 
			
		||||
import unittest, os, argparse
 | 
			
		||||
import unittest, argparse
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def find_tests():
 | 
			
		||||
    from calibre.utils.run_tests import find_tests_in_dir
 | 
			
		||||
    base = os.path.dirname(os.path.abspath(__file__))
 | 
			
		||||
    return find_tests_in_dir(base)
 | 
			
		||||
    from calibre.utils.run_tests import find_tests_in_package
 | 
			
		||||
    return find_tests_in_package('tinycss.tests')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def run_tests(find_tests=find_tests, for_build=False):
 | 
			
		||||
    if not for_build:
 | 
			
		||||
@ -36,7 +37,7 @@ def run_tests(find_tests=find_tests, for_build=False):
 | 
			
		||||
        except StopIteration:
 | 
			
		||||
            pass
 | 
			
		||||
        if ans is None:
 | 
			
		||||
            print ('No test named %s found' % args.name)
 | 
			
		||||
            print('No test named %s found' % args.name)
 | 
			
		||||
            raise SystemExit(1)
 | 
			
		||||
        tests = ans
 | 
			
		||||
    else:
 | 
			
		||||
@ -50,7 +51,6 @@ def run_tests(find_tests=find_tests, for_build=False):
 | 
			
		||||
    if for_build and result.errors or result.failures:
 | 
			
		||||
        raise SystemExit(1)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    run_tests()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user