Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame] | 1 | # 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 Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 4 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Paul Kehrer | 7e4bc6d | 2013-12-24 22:23:53 -0600 | [diff] [blame] | 7 | import pytest |
| 8 | |
Alex Gaynor | b8b1d72 | 2016-03-19 16:47:30 -0400 | [diff] [blame] | 9 | from cryptography.hazmat.backends.openssl import backend as openssl_backend |
Alex Gaynor | 7aab8b4 | 2014-10-23 11:01:25 -0700 | [diff] [blame] | 10 | |
Alex Gaynor | f9b3b15 | 2017-05-20 18:36:19 -0700 | [diff] [blame] | 11 | from .utils import check_backend_support, skip_if_empty |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 12 | |
| 13 | |
Alex Gaynor | b8b1d72 | 2016-03-19 16:47:30 -0400 | [diff] [blame] | 14 | def pytest_report_header(config): |
Alex Gaynor | e85797b | 2016-03-19 16:51:05 -0400 | [diff] [blame] | 15 | return "OpenSSL: {0}".format(openssl_backend.openssl_version_text()) |
Alex Gaynor | b8b1d72 | 2016-03-19 16:47:30 -0400 | [diff] [blame] | 16 | |
Alex Gaynor | 5930305 | 2016-03-19 16:48:18 -0400 | [diff] [blame] | 17 | |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 18 | def pytest_generate_tests(metafunc): |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 19 | if "backend" in metafunc.fixturenames: |
Paul Kehrer | f93d824 | 2014-10-23 12:07:20 -0700 | [diff] [blame] | 20 | filtered_backends = [] |
Alex Gaynor | 9bbd490 | 2015-07-01 22:26:28 -0400 | [diff] [blame] | 21 | required_interfaces = [ |
Alex Gaynor | f9b3b15 | 2017-05-20 18:36:19 -0700 | [diff] [blame] | 22 | mark.kwargs["interface"] |
| 23 | for mark in metafunc.function.requires_backend_interface |
Alex Gaynor | 9bbd490 | 2015-07-01 22:26:28 -0400 | [diff] [blame] | 24 | ] |
Alex Gaynor | f9b3b15 | 2017-05-20 18:36:19 -0700 | [diff] [blame] | 25 | if all( |
| 26 | isinstance(openssl_backend, iface) for iface in required_interfaces |
| 27 | ): |
| 28 | filtered_backends.append(openssl_backend) |
Paul Kehrer | f93d824 | 2014-10-23 12:07:20 -0700 | [diff] [blame] | 29 | |
Paul Kehrer | 902d8cf | 2014-10-25 12:22:10 -0700 | [diff] [blame] | 30 | # If you pass an empty list to parametrize Bad Things(tm) happen |
| 31 | # as of pytest 2.6.4 when the test also has a parametrize decorator |
| 32 | skip_if_empty(filtered_backends, required_interfaces) |
| 33 | |
| 34 | metafunc.parametrize("backend", filtered_backends) |
Paul Kehrer | 7e4bc6d | 2013-12-24 22:23:53 -0600 | [diff] [blame] | 35 | |
| 36 | |
| 37 | @pytest.mark.trylast |
| 38 | def pytest_runtest_setup(item): |
Paul Kehrer | 60fc8da | 2013-12-26 20:19:34 -0600 | [diff] [blame] | 39 | check_backend_support(item) |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def pytest_addoption(parser): |
| 43 | parser.addoption( |
| 44 | "--backend", action="store", metavar="NAME", |
| 45 | help="Only run tests matching the backend NAME." |
| 46 | ) |