Christopher Wilcox | 7e15258 | 2020-09-28 15:27:20 -0700 | [diff] [blame] | 1 | # Copyright 2020 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | import os |
| 16 | import sys |
| 17 | |
| 18 | import mock |
| 19 | import pytest |
| 20 | |
| 21 | |
| 22 | def pytest_configure(): |
| 23 | """Load public certificate and private key.""" |
| 24 | pytest.data_dir = os.path.join( |
| 25 | os.path.abspath(os.path.join(__file__, "../..")), "tests/data" |
| 26 | ) |
| 27 | |
| 28 | with open(os.path.join(pytest.data_dir, "privatekey.pem"), "rb") as fh: |
| 29 | pytest.private_key_bytes = fh.read() |
| 30 | |
| 31 | with open(os.path.join(pytest.data_dir, "public_cert.pem"), "rb") as fh: |
| 32 | pytest.public_cert_bytes = fh.read() |
| 33 | |
| 34 | |
| 35 | @pytest.fixture |
| 36 | def mock_non_existent_module(monkeypatch): |
| 37 | """Mocks a non-existing module in sys.modules. |
| 38 | |
| 39 | Additionally mocks any non-existing modules specified in the dotted path. |
| 40 | """ |
| 41 | |
| 42 | def _mock_non_existent_module(path): |
| 43 | parts = path.split(".") |
| 44 | partial = [] |
| 45 | for part in parts: |
| 46 | partial.append(part) |
| 47 | current_module = ".".join(partial) |
| 48 | if current_module not in sys.modules: |
| 49 | monkeypatch.setitem(sys.modules, current_module, mock.MagicMock()) |
| 50 | |
| 51 | return _mock_non_existent_module |