blob: 1952ec16445c89e56197a2bce17741c52de8f366 [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
Alex Gaynorc37feed2014-03-08 08:32:56 -08004
5from __future__ import absolute_import, division, print_function
6
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -06007import pytest
8
Alex Stapleton01de3ef2014-03-16 16:42:47 +00009from cryptography.hazmat.backends import _available_backends
Alex Gaynorb8b1d722016-03-19 16:47:30 -040010from cryptography.hazmat.backends.openssl import backend as openssl_backend
Alex Gaynor7aab8b42014-10-23 11:01:25 -070011
Paul Kehrer902d8cf2014-10-25 12:22:10 -070012from .utils import check_backend_support, select_backends, skip_if_empty
Paul Kehrer34c075e2014-01-13 21:52:08 -050013
14
Alex Gaynorb8b1d722016-03-19 16:47:30 -040015def pytest_report_header(config):
Alex Gaynore85797b2016-03-19 16:51:05 -040016 return "OpenSSL: {0}".format(openssl_backend.openssl_version_text())
Alex Gaynorb8b1d722016-03-19 16:47:30 -040017
Alex Gaynor59303052016-03-19 16:48:18 -040018
Paul Kehrer34c075e2014-01-13 21:52:08 -050019def pytest_generate_tests(metafunc):
Paul Kehreraed9e172014-01-19 12:09:27 -060020 if "backend" in metafunc.fixturenames:
Alex Gaynor3adb8392015-07-12 11:44:04 -050021 names = metafunc.config.getoption("--backend")
22 selected_backends = select_backends(names, _available_backends())
23
Paul Kehrerf93d8242014-10-23 12:07:20 -070024 filtered_backends = []
Alex Gaynor52c15642014-12-18 08:08:36 -080025 required = metafunc.function.requires_backend_interface
Alex Gaynor9bbd4902015-07-01 22:26:28 -040026 required_interfaces = [
Alex Gaynor52c15642014-12-18 08:08:36 -080027 mark.kwargs["interface"] for mark in required
Alex Gaynor9bbd4902015-07-01 22:26:28 -040028 ]
Paul Kehrerf93d8242014-10-23 12:07:20 -070029 for backend in selected_backends:
Alex Gaynor9bbd4902015-07-01 22:26:28 -040030 if all(
31 isinstance(backend, iface) for iface in required_interfaces
32 ):
Paul Kehrerdaefd3f2014-10-24 07:48:37 -070033 filtered_backends.append(backend)
Paul Kehrerf93d8242014-10-23 12:07:20 -070034
Paul Kehrer902d8cf2014-10-25 12:22:10 -070035 # If you pass an empty list to parametrize Bad Things(tm) happen
36 # as of pytest 2.6.4 when the test also has a parametrize decorator
37 skip_if_empty(filtered_backends, required_interfaces)
38
39 metafunc.parametrize("backend", filtered_backends)
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060040
41
42@pytest.mark.trylast
43def pytest_runtest_setup(item):
Paul Kehrer60fc8da2013-12-26 20:19:34 -060044 check_backend_support(item)
Paul Kehrer34c075e2014-01-13 21:52:08 -050045
46
47def pytest_addoption(parser):
48 parser.addoption(
49 "--backend", action="store", metavar="NAME",
50 help="Only run tests matching the backend NAME."
51 )