blob: d872d7c1f1c7dcf3ea3c7c5fab0aa1343ee9ccc8 [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):
16 return "OpenSSL: {}".format(openssl_backend.openssl_version_text())
17
Paul Kehrer34c075e2014-01-13 21:52:08 -050018def pytest_generate_tests(metafunc):
Paul Kehreraed9e172014-01-19 12:09:27 -060019 if "backend" in metafunc.fixturenames:
Alex Gaynor3adb8392015-07-12 11:44:04 -050020 names = metafunc.config.getoption("--backend")
21 selected_backends = select_backends(names, _available_backends())
22
Paul Kehrerf93d8242014-10-23 12:07:20 -070023 filtered_backends = []
Alex Gaynor52c15642014-12-18 08:08:36 -080024 required = metafunc.function.requires_backend_interface
Alex Gaynor9bbd4902015-07-01 22:26:28 -040025 required_interfaces = [
Alex Gaynor52c15642014-12-18 08:08:36 -080026 mark.kwargs["interface"] for mark in required
Alex Gaynor9bbd4902015-07-01 22:26:28 -040027 ]
Paul Kehrerf93d8242014-10-23 12:07:20 -070028 for backend in selected_backends:
Alex Gaynor9bbd4902015-07-01 22:26:28 -040029 if all(
30 isinstance(backend, iface) for iface in required_interfaces
31 ):
Paul Kehrerdaefd3f2014-10-24 07:48:37 -070032 filtered_backends.append(backend)
Paul Kehrerf93d8242014-10-23 12:07:20 -070033
Paul Kehrer902d8cf2014-10-25 12:22:10 -070034 # If you pass an empty list to parametrize Bad Things(tm) happen
35 # as of pytest 2.6.4 when the test also has a parametrize decorator
36 skip_if_empty(filtered_backends, required_interfaces)
37
38 metafunc.parametrize("backend", filtered_backends)
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060039
40
41@pytest.mark.trylast
42def pytest_runtest_setup(item):
Paul Kehrer60fc8da2013-12-26 20:19:34 -060043 check_backend_support(item)
Paul Kehrer34c075e2014-01-13 21:52:08 -050044
45
46def pytest_addoption(parser):
47 parser.addoption(
48 "--backend", action="store", metavar="NAME",
49 help="Only run tests matching the backend NAME."
50 )