Option to run tests under sanitizer

This commit is contained in:
Kovid Goyal 2021-03-22 09:35:17 +05:30
parent ccc85bc358
commit 5ce36cc3e6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 15 additions and 3 deletions

View File

@ -4,9 +4,11 @@
import os import os
import subprocess
import sys
import unittest import unittest
from setup import Command, islinux, ismacos, iswindows, SRC from setup import SRC, Command, islinux, ismacos, iswindows
TEST_MODULES = frozenset('srv db polish opf css docx cfi matcher icu smartypants build misc dbcli ebooks'.split()) TEST_MODULES = frozenset('srv db polish opf css docx cfi matcher icu smartypants build misc dbcli ebooks'.split())
@ -176,9 +178,19 @@ class Test(Command):
' Choices: %s' % ', '.join(sorted(TEST_MODULES))) ' Choices: %s' % ', '.join(sorted(TEST_MODULES)))
parser.add_option('--exclude-test-name', default=[], action='append', parser.add_option('--exclude-test-name', default=[], action='append',
help='The name of an individual test to be excluded from the test run. Can be specified more than once for multiple tests.') help='The name of an individual test to be excluded from the test run. Can be specified more than once for multiple tests.')
parser.add_option('--under-sanitize', default=False, action='store_true',
help='Run the test suite with the sanitizer preloaded')
def run(self, opts): def run(self, opts):
from calibre.utils.run_tests import run_cli, filter_tests_by_name, remove_tests_by_name if opts.under_sanitize and 'libasan' not in os.environ.get('LD_PRELOAD', ''):
os.environ['LD_PRELOAD'] = os.path.abspath(subprocess.check_output('gcc -print-file-name=libasan.so'.split()).decode('utf-8').strip())
os.environ['ASAN_OPTIONS'] = 'detect_leaks=0'
self.info(f'Re-execing with LD_PRELOAD={os.environ["LD_PRELOAD"]}')
sys.stdout.flush()
os.execl('setup.py', *sys.argv)
from calibre.utils.run_tests import (
filter_tests_by_name, remove_tests_by_name, run_cli
)
tests = find_tests(which_tests=frozenset(opts.test_module), exclude_tests=frozenset(opts.exclude_test_module)) tests = find_tests(which_tests=frozenset(opts.test_module), exclude_tests=frozenset(opts.exclude_test_module))
if opts.test_name: if opts.test_name:
tests = filter_tests_by_name(tests, *opts.test_name) tests = filter_tests_by_name(tests, *opts.test_name)

View File

@ -274,7 +274,7 @@ def test(return_tests=False):
class Test(unittest.TestCase): class Test(unittest.TestCase):
@unittest.skipIf(is_sanitized, 'Sanitizer enabled will cant check for leaks') @unittest.skipIf(is_sanitized, 'Sanitizer enabled cant check for leaks')
def test_mem_leaks(self): def test_mem_leaks(self):
import gc import gc
from calibre.utils.mem import get_memory as memory from calibre.utils.mem import get_memory as memory