diff options
Diffstat (limited to 'test/unit/utils.py')
-rw-r--r-- | test/unit/utils.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/test/unit/utils.py b/test/unit/utils.py new file mode 100644 index 0000000..e2d89b9 --- /dev/null +++ b/test/unit/utils.py @@ -0,0 +1,59 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +""" +Various functions and objects that can be reused between unit tests +""" + +# This file is part of Haketilo. +# +# Copyright (C) 2021,2022 Wojtek Kosior <koszko@koszko.org> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <https://www.gnu.org/licenses/>. +# +# I, Wojtek Kosior, thereby promise not to sue for violation of this file's +# license. Although I request that you do not make use of this code in a +# proprietary program, I am not going to enforce this in court. + +from hashlib import sha256 + +def sample_file(contents): + return { + 'hash_key': f'sha256-{sha256(contents.encode()).digest().hex()}', + 'contents': contents + } + +sample_files = { + 'report.spdx': sample_file('<!-- dummy report -->'), + '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 item_version_string(definition, include_revision=False): + """ + Given a resource or mapping definition, read its "version" property (and + also "revision" if applicable) and produce a corresponding version string. + """ + ver = '.'.join([str(num) for num in definition['version']]) + revision = definition.get('revision') if include_revision else None + return f'{ver}-{revision}' if revision is not None else ver |