GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — extractcode/tests/extractcode_assert_utils.py at main · aboutcode-org/extractcode Expand file tree/ Copy pathextractcode_assert_utils.pyMore file actions 259 lines (224 loc) · 8.72 KB / Copy pathextractcode_assert_utils.pyFile metadata and controls259 lines (224 loc) · 8.72 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 # # Copyright (c) nexB Inc. and others. All rights reserved. # ScanCode is a trademark of nexB Inc. # SPDX-License-Identifier: Apache-2.0 # See http://www.apache.org/licenses/LICENSE-2.0 for the license text. # See https://github.com/nexB/extractcode for support or download. # See https://aboutcode.org for more information about nexB OSS projects. # import json import os import ntpath import posixpath from commoncode import filetype from commoncode import fileutils from commoncode.testcase import FileBasedTesting from commoncode.system import on_windows """ Shared archiving test utils. """ def check_size(expected_size, location): assert expected_size == os.stat(location).st_size def check_results_with_expected_json(results, expected_loc, regen=False): if regen: with open(expected_loc, "w") as ex: json.dump(results, ex, indent=2, separators=(",", ":")) with open(expected_loc) as ex: expected = json.load(ex) try: assert expected == results except AssertionError: assert json.dumps(expected, indent=2) == json.dumps(results, indent=2) def check_files(test_dir, expected, regen=False): """ Walk test_dir. Check that all dirs are readable. Check that all files are: * non-special, * readable, * have a posix path that ends with one of the expected tuple paths. """ result = [] locs = [] if filetype.is_file(test_dir): test_dir = fileutils.parent_directory(test_dir) test_dir_path = fileutils.as_posixpath(test_dir) for top, _, files in os.walk(test_dir): for f in files: location = os.path.join(top, f) locs.append(location) path = fileutils.as_posixpath(location) path = path.replace(test_dir_path, "").strip("/") result.append(path) expected_is_json_file = False if not isinstance(expected, (list, tuple)) and expected.endswith(".json"): expected_is_json_file = True # this is a path to a JSON file if regen: with open(expected, "w") as ex: json.dump(result, ex, indent=2, separators=(",", ":")) expected_content = result else: with open(expected) as ex: expected_content = json.load(ex) else: expected_content = expected expected_content = sorted(expected_content) result = sorted(result) try: assert result == expected_content except AssertionError: files = [ "test_dir: file://{}".format(test_dir), "expected: file://{}".format(expected if expected_is_json_file else ""), ] assert result == files + expected_content for location in locs: assert filetype.is_file(location) assert not filetype.is_special(location) assert filetype.is_readable(location) def check_no_error(result): """ Check that every ExtractEvent in the `result` list has no error or warning. """ for r in result: assert not r.errors assert not r.warnings def is_posixpath(location): r""" Return True if the `location` path is likely a POSIX-like path using POSIX path separators (slash or "/")or has no path separator. Return False if the `location` path is likely a Windows-like path using backslash as path separators (e.g. "\"). """ has_slashes = "/" in location has_backslashes = "\\" in location # windows paths with drive if location: drive, _ = ntpath.splitdrive(location) if drive: return False # a path is always POSIX unless it contains ONLY backslahes # which is a rough approximation (it could still be posix) is_posix = True if has_backslashes and not has_slashes: is_posix = False return is_posix def to_posix(path): r""" Return a path using the posix path separator given a path that may contain posix or windows separators, converting \\ to /. NB: this path will still be valid in the windows explorer (except as a UNC or share name). It will be a valid path everywhere in Python. It will not be valid for windows command line operations. """ is_unicode = isinstance(path, str) ntpath_sep = is_unicode and "\\" or "\\" posixpath_sep = is_unicode and "/" or "/" if is_posixpath(path): if on_windows: return path.replace(ntpath_sep, posixpath_sep) else: return path return path.replace(ntpath_sep, posixpath_sep) class BaseArchiveTestCase(FileBasedTesting): test_data_dir = os.path.join(os.path.dirname(__file__), "data") def check_get_extractors(self, test_file, expected, kinds=()): from extractcode import archive test_loc = self.get_test_loc(test_file) if kinds: extractors = archive.get_extractors(test_loc, kinds) else: extractors = archive.get_extractors(test_loc) fe = fileutils.file_extension(test_loc).lower() em = ", ".join(e.__module__ + "." + e.__name__ for e in extractors) msg = ( "%(expected)r == %(extractors)r for %(test_file)s\nwith fe:%(fe)r, em:%(em)s" % locals() ) assert expected == extractors, msg def assertRaisesInstance(self, excInstance, callableObj, *args, **kwargs): """ Accept an instance instead of a class for refined exception testing. """ kwargs = kwargs or {} excClass = excInstance.__class__ try: callableObj(*args, **kwargs) except excClass as e: assert str(e).startswith(str(excInstance)) else: if hasattr(excClass, "__name__"): excName = excClass.__name__ else: excName = str(excClass) raise self.failureException("%s not raised" % excName) def check_extract( self, test_function, test_file, expected, expected_warnings=None, check_all=False, ): """ Run the extraction `test_function` on `test_file` checking that a map of expected paths --> size exist in the extracted target directory. Does not test the presence of all files unless `check_all` is True. """ from extractcode import archive test_file = self.get_test_loc(test_file) test_dir = self.get_temp_dir() warnings = test_function(test_file, test_dir) if expected_warnings is not None: assert expected_warnings == warnings if check_all: len_test_dir = len(test_dir) extracted = { path[len_test_dir:]: filetype.get_size(path) for path in fileutils.resource_iter(test_dir, with_dirs=False) } expected = { os.path.join(test_dir, exp_path): exp_size for exp_path, exp_size in expected.items() } assert sorted(expected.items()) == sorted(extracted.items()) else: for exp_path, exp_size in expected.items(): exp_loc = os.path.join(test_dir, exp_path) msg = """When extracting: %(test_file)s With function: %(test_function)r Failed to find expected path: %(exp_loc)s""" assert os.path.exists(exp_loc), msg % locals() if exp_size is not None: res_size = os.stat(exp_loc).st_size msg = """When extracting: %(test_file)s With function: %(test_function)r Failed to assert the correct size %(exp_size)d Got instead: %(res_size)d for expected path: %(exp_loc)s""" assert exp_size == res_size, msg % locals() def collect_extracted_path(self, test_dir): result = [] td = fileutils.as_posixpath(test_dir) for t, dirs, files in os.walk(test_dir): t = fileutils.as_posixpath(t) for d in dirs: nd = posixpath.join(t, d).replace(td, "") + "/" result.append(nd) for f in files: nf = posixpath.join(t, f).replace(td, "") result.append(nf) result = sorted(result) return result def assertExceptionContains(self, text, callableObj, *args, **kwargs): try: callableObj(*args, **kwargs) except Exception as e: if text not in str(e): raise self.failureException( "Exception %(e)r raised, " "it should contain the text %(text)r " "and does not" % locals() ) else: raise self.failureException("Exception containing %(text)r not raised" % locals()) |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |