blob: 1cb2b30d67341fb30f59b320333635ac0bf149d3 [file] [log] [blame]
Alex Gaynorc37feed2014-03-08 08:32:56 -08001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14from __future__ import absolute_import, division, print_function
15
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060016import pytest
17
Alex Stapleton01de3ef2014-03-16 16:42:47 +000018from cryptography.hazmat.backends import _available_backends
Alex Gaynor7aab8b42014-10-23 11:01:25 -070019
20from .utils import check_backend_support, select_backends
Paul Kehrer34c075e2014-01-13 21:52:08 -050021
22
23def pytest_generate_tests(metafunc):
Paul Kehrerc421e632014-01-18 09:22:21 -060024 names = metafunc.config.getoption("--backend")
Alex Stapleton01de3ef2014-03-16 16:42:47 +000025 selected_backends = select_backends(names, _available_backends())
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060026
Paul Kehreraed9e172014-01-19 12:09:27 -060027 if "backend" in metafunc.fixturenames:
Paul Kehrerf93d8242014-10-23 12:07:20 -070028 filtered_backends = []
29 for backend in selected_backends:
30 try:
31 required = metafunc.function.requires_backend_interface
Paul Kehrerdaefd3f2014-10-24 07:48:37 -070032 except AttributeError:
33 # function does not have requires_backend_interface decorator
34 filtered_backends.append(backend)
35 else:
Paul Kehrerf93d8242014-10-23 12:07:20 -070036 required_interfaces = tuple(
37 mark.kwargs["interface"] for mark in required
38 )
39 if isinstance(backend, required_interfaces):
40 filtered_backends.append(backend)
Paul Kehrerf93d8242014-10-23 12:07:20 -070041
42 if not filtered_backends:
Paul Kehrer56f1a0a2014-10-24 10:30:25 -070043 # If you pass an empty list to parametrize Bad Things(tm) happen
44 # as of pytest 2.6.4 when the test also has a parametrize decorator
Paul Kehrerf93d8242014-10-23 12:07:20 -070045 pytest.skip(
46 "No backends provided supply the interface: {0}".format(
47 ", ".join(iface.__name__ for iface in required_interfaces)
48 )
49 )
50 else:
51 metafunc.parametrize("backend", filtered_backends)
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060052
53
54@pytest.mark.trylast
55def pytest_runtest_setup(item):
Paul Kehrer60fc8da2013-12-26 20:19:34 -060056 check_backend_support(item)
Paul Kehrer34c075e2014-01-13 21:52:08 -050057
58
59def pytest_addoption(parser):
60 parser.addoption(
61 "--backend", action="store", metavar="NAME",
62 help="Only run tests matching the backend NAME."
63 )