|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# --- BEGIN_HEADER --- |
| 5 | +# |
| 6 | +# configsupp - configuration helpers for unit tests |
| 7 | +# Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH |
| 8 | +# |
| 9 | +# This file is part of MiG. |
| 10 | +# |
| 11 | +# MiG is free software: you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU General Public License as published by |
| 13 | +# the Free Software Foundation; either version 2 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# MiG is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program; if not, write to the Free Software |
| 23 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 24 | +# |
| 25 | +# -- END_HEADER --- |
| 26 | +# |
| 27 | + |
| 28 | +"""Fixture related details within the test support library.""" |
| 29 | + |
| 30 | +from configparser import ConfigParser |
| 31 | +import io |
| 32 | +import json |
| 33 | +import os |
| 34 | +import shutil |
| 35 | + |
| 36 | +from tests.support.suppconst import MIG_BASE, TEST_FIXTURE_DIR |
| 37 | + |
| 38 | + |
| 39 | +_FIXTUREFILE_HINTAPPLIERS = { |
| 40 | + 'array_of_tuples': lambda value: [tuple(x) for x in value] |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +def _to_display_path(value): |
| 45 | + display_path = os.path.relpath(value, MIG_BASE) |
| 46 | + if not display_path.startswith('.'): |
| 47 | + return "./" + display_path |
| 48 | + return display_path |
| 49 | + |
| 50 | + |
| 51 | +def _fixturefile(relative_path, fixture_format=None): |
| 52 | + """Support function for loading fixtures from their serialised format. |
| 53 | +
|
| 54 | + Doing so is a little more involved than it may seem because serialisation |
| 55 | + formats may not capture various nuances of the python data they represent. |
| 56 | + For this reason each supported format defers to a format specific function |
| 57 | + which can then, for example, load hints about deserialization. |
| 58 | + """ |
| 59 | + |
| 60 | + assert fixture_format is not None, "fixture format must be specified" |
| 61 | + assert not os.path.isabs( |
| 62 | + relative_path), "fixture is not relative to fixture folder" |
| 63 | + relative_path_with_ext = "%s.%s" % (relative_path, fixture_format) |
| 64 | + tmp_path = os.path.join(TEST_FIXTURE_DIR, relative_path_with_ext) |
| 65 | + assert os.path.isfile(tmp_path), \ |
| 66 | + "fixture file for format is not present: %s" % \ |
| 67 | + (relative_path_with_ext,) |
| 68 | + |
| 69 | + data = None |
| 70 | + |
| 71 | + if fixture_format == 'binary': |
| 72 | + with open(tmp_path, 'rb') as binfile: |
| 73 | + data = binfile.read() |
| 74 | + elif fixture_format == 'json': |
| 75 | + data = _fixturefile_json(tmp_path) |
| 76 | + else: |
| 77 | + raise AssertionError( |
| 78 | + "unsupported fixture format: %s" % (fixture_format,)) |
| 79 | + |
| 80 | + return data, tmp_path |
| 81 | + |
| 82 | + |
| 83 | +def _fixturefile_json(json_path): |
| 84 | + hints = ConfigParser() |
| 85 | + |
| 86 | + # let's see if there are loading hints |
| 87 | + try: |
| 88 | + hints_path = "%s.ini" % (json_path,) |
| 89 | + with open(hints_path) as hints_file: |
| 90 | + hints.read_file(hints_file) |
| 91 | + except FileNotFoundError: |
| 92 | + pass |
| 93 | + |
| 94 | + with io.open(json_path) as json_file: |
| 95 | + json_object = json.load(json_file) |
| 96 | + |
| 97 | + for item_name, item_hint in hints['DEFAULT'].items(): |
| 98 | + loaded_value = json_object[item_name] |
| 99 | + value_from_loaded_value = _FIXTUREFILE_HINTAPPLIERS[item_hint] |
| 100 | + json_object[item_name] = value_from_loaded_value(loaded_value) |
| 101 | + |
| 102 | + return json_object |
| 103 | + |
| 104 | + |
| 105 | +def _fixturefile_normname(relative_path, prefix=''): |
| 106 | + """Grab normname from relative_path and optionally add a path prefix""" |
| 107 | + normname, _ = relative_path.split('--') |
| 108 | + if prefix: |
| 109 | + return os.path.join(prefix, normname) |
| 110 | + return normname |
| 111 | + |
| 112 | + |
| 113 | +class _AssertAgainstFixture: |
| 114 | + def __init__(self, testcase, fixture_format, fixture_data, fixture_path): |
| 115 | + self._testcase = testcase |
| 116 | + self._fixture_format = fixture_format |
| 117 | + self._fixture_data = fixture_data |
| 118 | + self._fixture_path = fixture_path |
| 119 | + |
| 120 | + def assertAgainstFixture(self, value=None): |
| 121 | + assert value is not None |
| 122 | + originalMaxDiff = self._testcase.maxDiff |
| 123 | + self._testcase.maxDiff = None |
| 124 | + try: |
| 125 | + self._testcase.assertEqual(value, self._fixture_data) |
| 126 | + except AssertionError as diffexc: |
| 127 | + message = "value differed from fixture stored at %s\n\n%s" % ( |
| 128 | + _to_display_path(self._fixture_path), diffexc) |
| 129 | + raise AssertionError(message) |
| 130 | + finally: |
| 131 | + self._testcase.maxDiff = originalMaxDiff |
| 132 | + |
| 133 | + def copy_as_temp(self, prefix=None): |
| 134 | + assert prefix is not None |
| 135 | + fixture_basename = os.path.basename(self._fixture_path) |
| 136 | + fixture_name = fixture_basename[0:-len(self._fixture_format) - 1] |
| 137 | + normalised_path = _fixturefile_normname(fixture_name, prefix=prefix) |
| 138 | + copied_fixture_file = self._testcase.temppath(normalised_path) |
| 139 | + shutil.copyfile(self._fixture_path, copied_fixture_file) |
| 140 | + return copied_fixture_file |
| 141 | + |
| 142 | + |
| 143 | +class FixtureAssertMixin: |
| 144 | + def prepareFixtureAssert(self, fixture_relpath, fixture_format=None): |
| 145 | + fixture_data, fixture_path = _fixturefile( |
| 146 | + fixture_relpath, fixture_format) |
| 147 | + return _AssertAgainstFixture(self, fixture_format, fixture_data, fixture_path) |
| 148 | + |
| 149 | + |
| 150 | +def fixturepath(relative_path): |
| 151 | + """Get absolute fixture path for relative_path""" |
| 152 | + tmp_path = os.path.join(TEST_FIXTURE_DIR, relative_path) |
| 153 | + return tmp_path |
0 commit comments