| 1 |
"""Common utilities for Brotli tests.""" |
| 2 |
|
| 3 |
from __future__ import print_function |
| 4 |
import filecmp |
| 5 |
import glob |
| 6 |
import itertools |
| 7 |
import os |
| 8 |
import sys |
| 9 |
import sysconfig |
| 10 |
import tempfile |
| 11 |
import unittest |
| 12 |
|
| 13 |
|
| 14 |
project_dir = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
| 15 |
test_dir = os.getenv('BROTLI_TESTS_PATH') |
| 16 |
BRO_ARGS = [os.getenv('BROTLI_WRAPPER')] |
| 17 |
|
| 18 |
|
| 19 |
if test_dir is None: |
| 20 |
test_dir = os.path.join(project_dir, 'tests') |
| 21 |
if BRO_ARGS[0] is None: |
| 22 |
python_exe = sys.executable or 'python' |
| 23 |
bro_path = os.path.join(project_dir, 'python', 'bro.py') |
| 24 |
BRO_ARGS = [python_exe, bro_path] |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
platform_lib_name = 'lib.{platform}-{version[0]}.{version[1]}'.format( |
| 29 |
platform=sysconfig.get_platform(), version=sys.version_info |
| 30 |
) |
| 31 |
build_dir = os.path.join(project_dir, 'bin', platform_lib_name) |
| 32 |
|
| 33 |
|
| 34 |
if build_dir not in sys.path: |
| 35 |
sys.path.insert(0, build_dir) |
| 36 |
TEST_ENV = dict(os.environ) |
| 37 |
if 'PYTHONPATH' not in TEST_ENV: |
| 38 |
TEST_ENV['PYTHONPATH'] = build_dir |
| 39 |
else: |
| 40 |
TEST_ENV['PYTHONPATH'] = build_dir + os.pathsep + TEST_ENV['PYTHONPATH'] |
| 41 |
|
| 42 |
TESTDATA_DIR = os.path.join(test_dir, 'testdata') |
| 43 |
|
| 44 |
TESTDATA_FILES = [ |
| 45 |
'empty', |
| 46 |
'10x10y', |
| 47 |
'alice29.txt', |
| 48 |
'random_org_10k.bin', |
| 49 |
'mapsdatazrh', |
| 50 |
'ukkonooa', |
| 51 |
'cp1251-utf16le', |
| 52 |
'cp852-utf8', |
| 53 |
|
| 54 |
] |
| 55 |
|
| 56 |
|
| 57 |
TESTDATA_PATH_CANDIDATES = [ |
| 58 |
os.path.join(TESTDATA_DIR, f) for f in TESTDATA_FILES |
| 59 |
] |
| 60 |
|
| 61 |
TESTDATA_PATHS = [ |
| 62 |
path for path in TESTDATA_PATH_CANDIDATES if os.path.isfile(path) |
| 63 |
] |
| 64 |
|
| 65 |
TESTDATA_PATHS_FOR_DECOMPRESSION = glob.glob( |
| 66 |
os.path.join(TESTDATA_DIR, '*.compressed') |
| 67 |
) |
| 68 |
|
| 69 |
TEMP_DIR = tempfile.mkdtemp() |
| 70 |
|
| 71 |
|
| 72 |
def get_temp_compressed_name(filename): |
| 73 |
return os.path.join(TEMP_DIR, os.path.basename(filename + '.bro')) |
| 74 |
|
| 75 |
|
| 76 |
def get_temp_uncompressed_name(filename): |
| 77 |
return os.path.join(TEMP_DIR, os.path.basename(filename + '.unbro')) |
| 78 |
|
| 79 |
|
| 80 |
def bind_method_args(method, *args, **kwargs): |
| 81 |
return lambda self: method(self, *args, **kwargs) |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
def generate_test_methods( |
| 86 |
test_case_class, for_decompression=False, variants=None |
| 87 |
): |
| 88 |
"""Adds test methods for each test data file and each variant. |
| 89 |
|
| 90 |
This makes identifying problems with specific compression scenarios easier. |
| 91 |
|
| 92 |
Args: |
| 93 |
test_case_class: The test class to add methods to. |
| 94 |
for_decompression: If True, uses compressed test data files. |
| 95 |
variants: A dictionary where keys are option names and values are lists of |
| 96 |
possible values for that option. Each combination of variants will |
| 97 |
generate a separate test method. |
| 98 |
""" |
| 99 |
if for_decompression: |
| 100 |
paths = [ |
| 101 |
path for path in TESTDATA_PATHS_FOR_DECOMPRESSION |
| 102 |
if os.path.exists(path.replace('.compressed', '')) |
| 103 |
] |
| 104 |
else: |
| 105 |
paths = TESTDATA_PATHS |
| 106 |
opts = [] |
| 107 |
if variants: |
| 108 |
opts_list = [] |
| 109 |
for k, v in variants.items(): |
| 110 |
opts_list.append([r for r in itertools.product([k], v)]) |
| 111 |
for o in itertools.product(*opts_list): |
| 112 |
opts_name = '_'.join([str(i) for i in itertools.chain(*o)]) |
| 113 |
opts_dict = dict(o) |
| 114 |
opts.append([opts_name, opts_dict]) |
| 115 |
else: |
| 116 |
opts.append(['', {}]) |
| 117 |
for method in [m for m in dir(test_case_class) if m.startswith('_test')]: |
| 118 |
for testdata in paths: |
| 119 |
for opts_name, opts_dict in opts: |
| 120 |
f = os.path.splitext(os.path.basename(testdata))[0] |
| 121 |
name = 'test_{method}_{options}_{file}'.format( |
| 122 |
method=method, options=opts_name, file=f |
| 123 |
) |
| 124 |
func = bind_method_args( |
| 125 |
getattr(test_case_class, method), testdata, **opts_dict |
| 126 |
) |
| 127 |
setattr(test_case_class, name, func) |
| 128 |
|
| 129 |
|
| 130 |
class TestCase(unittest.TestCase): |
| 131 |
"""Base class for Brotli test cases. |
| 132 |
|
| 133 |
Provides common setup and teardown logic, including cleaning up temporary |
| 134 |
files and a utility for comparing file contents. |
| 135 |
""" |
| 136 |
|
| 137 |
def tearDown(self): |
| 138 |
for f in TESTDATA_PATHS: |
| 139 |
try: |
| 140 |
os.unlink(get_temp_compressed_name(f)) |
| 141 |
except OSError: |
| 142 |
pass |
| 143 |
try: |
| 144 |
os.unlink(get_temp_uncompressed_name(f)) |
| 145 |
except OSError: |
| 146 |
pass |
| 147 |
|
| 148 |
|
| 149 |
def assert_files_match(self, first, second): |
| 150 |
self.assertTrue( |
| 151 |
filecmp.cmp(first, second, shallow=False), |
| 152 |
'File {} differs from {}'.format(first, second), |
| 153 |
) |