From 5c58b3d65e370ebd3dadc1133157c73c6afc84af Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Thu, 27 Jan 2022 19:35:44 +0100 Subject: facilitate querying IndexedDB for script files of resource and its dependencies --- test/unit/utils.py | 78 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 22 deletions(-) (limited to 'test/unit/utils.py') diff --git a/test/unit/utils.py b/test/unit/utils.py index 6f0236d..56880d5 100644 --- a/test/unit/utils.py +++ b/test/unit/utils.py @@ -42,29 +42,51 @@ def sample_file(contents): 'contents': contents } -sample_files = { - 'report.spdx': sample_file(''), - 'LICENSES/somelicense.txt': sample_file('Permission is granted...'), - 'LICENSES/CC0-1.0.txt': sample_file('Dummy Commons...'), - 'hello.js': sample_file('console.log("uńićódę hello!");\n'), - 'bye.js': sample_file('console.log("bye!");\n'), - 'combined.js': sample_file('console.log("hello!\\nbye!");\n'), - 'README.md': sample_file('# Python Frobnicator\n...') -} - -sample_files_by_hash = dict([[file['hash_key'], file['contents']] - for file in sample_files.values()]) - -def sample_file_ref(file_name): - return {'file': file_name, 'hash_key': sample_files[file_name]['hash_key']} - -def make_sample_mapping(): +def make_sample_files(names_contents): + """ + Take a dict mapping file names to file contents. Return, as a tuple, dicts + mapping file names to file objects (dicts) and file hash keys to file + contents. + """ + sample_files = dict([(name, sample_file(contents)) + for name, contents in names_contents.items()]) + + sample_files_by_hash = dict([[file['hash_key'], file['contents']] + for file in sample_files.values()]) + + return sample_files, sample_files_by_hash + +sample_files, sample_files_by_hash = make_sample_files({ + 'report.spdx': '', + 'LICENSES/somelicense.txt': 'Permission is granted...', + 'LICENSES/CC0-1.0.txt': 'Dummy Commons...', + 'hello.js': 'console.log("uńićódę hello!");\n', + 'bye.js': 'console.log("bye!");\n', + 'combined.js': 'console.log("hello!\\nbye!");\n', + 'README.md': '# Python Frobnicator\n...' +}) + +def sample_file_ref(file_name, sample_files_dict=sample_files): + """ + Return a dictionary suitable for using as file reference in resource/mapping + definition. + """ + return { + 'file': file_name, + 'hash_key': sample_files_dict[file_name]['hash_key'] + } + +def make_sample_mapping(with_files=True): + """ + Procude a sample mapping definition that can be dumped to JSON and put into + Haketilo's IndexedDB. + """ return { 'source_name': 'example-org-fixes-new', 'source_copyright': [ sample_file_ref('report.spdx'), sample_file_ref('LICENSES/CC0-1.0.txt') - ], + ] if with_files else [], 'type': 'mapping', 'identifier': 'example-org-minimal', 'long_name': 'Example.org Minimal', @@ -81,13 +103,17 @@ def make_sample_mapping(): } } -def make_sample_resource(): +def make_sample_resource(with_files=True): + """ + Procude a sample resource definition that can be dumped to JSON and put into + Haketilo's IndexedDB. + """ return { 'source_name': 'hello', 'source_copyright': [ sample_file_ref('report.spdx'), sample_file_ref('LICENSES/CC0-1.0.txt') - ], + ] if with_files else [], 'type': 'resource', 'identifier': 'helloapple', 'long_name': 'Hello Apple', @@ -99,7 +125,7 @@ def make_sample_resource(): 'scripts': [ sample_file_ref('hello.js'), sample_file_ref('bye.js') - ] + ] if with_files else [] } def item_version_string(definition, include_revision=False): @@ -113,7 +139,7 @@ def item_version_string(definition, include_revision=False): def sample_data_dict(items): """ - Some indexeddb functions expect saved items to be provided in a nested dict + Some IndexedDB functions expect saved items to be provided in a nested dict that makes them queryable by identifier by version. This function converts items list to such dict. """ @@ -202,6 +228,14 @@ def are_scripts_allowed(driver, nonce=None): ''', nonce) +def mock_broadcast(execute_in_page): + """ + Make all broadcast operations no-ops (broadcast must be imported). + """ + execute_in_page( + 'Object.keys(broadcast).forEach(k => broadcast[k] = () => {});' + ) + def mock_cacher(execute_in_page): """ Some parts of code depend on content/repo_query_cacher.js and -- cgit v1.2.3