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 Stapleton | 01de3ef | 2014-03-16 16:42:47 +0000 | [diff] [blame] | 9 | from cryptography.hazmat.backends import _available_backends |
Alex Gaynor | 7aab8b4 | 2014-10-23 11:01:25 -0700 | [diff] [blame] | 10 | |
Paul Kehrer | 902d8cf | 2014-10-25 12:22:10 -0700 | [diff] [blame] | 11 | from .utils import check_backend_support, select_backends, skip_if_empty |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 12 | |
| 13 | |
| 14 | def pytest_generate_tests(metafunc): |
Paul Kehrer | aed9e17 | 2014-01-19 12:09:27 -0600 | [diff] [blame] | 15 | if "backend" in metafunc.fixturenames: |
Alex Gaynor | 3adb839 | 2015-07-12 11:44:04 -0500 | [diff] [blame] | 16 | names = metafunc.config.getoption("--backend") |
| 17 | selected_backends = select_backends(names, _available_backends()) |
| 18 | |
Paul Kehrer | f93d824 | 2014-10-23 12:07:20 -0700 | [diff] [blame] | 19 | filtered_backends = [] |
Alex Gaynor | 52c1564 | 2014-12-18 08:08:36 -0800 | [diff] [blame] | 20 | required = metafunc.function.requires_backend_interface |
Alex Gaynor | 9bbd490 | 2015-07-01 22:26:28 -0400 | [diff] [blame] | 21 | required_interfaces = [ |
Alex Gaynor | 52c1564 | 2014-12-18 08:08:36 -0800 | [diff] [blame] | 22 | mark.kwargs["interface"] for mark in required |
Alex Gaynor | 9bbd490 | 2015-07-01 22:26:28 -0400 | [diff] [blame] | 23 | ] |
Paul Kehrer | f93d824 | 2014-10-23 12:07:20 -0700 | [diff] [blame] | 24 | for backend in selected_backends: |
Alex Gaynor | 9bbd490 | 2015-07-01 22:26:28 -0400 | [diff] [blame] | 25 | if all( |
| 26 | isinstance(backend, iface) for iface in required_interfaces |
| 27 | ): |
Paul Kehrer | daefd3f | 2014-10-24 07:48:37 -0700 | [diff] [blame] | 28 | filtered_backends.append(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 | ) |