• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 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
15import os
16import sys
17
18import mock
19import pytest
20
21
22def pytest_configure():
23    """Load public certificate and private key."""
24    pytest.data_dir = os.path.join(os.path.dirname(__file__), "data")
25
26    with open(os.path.join(pytest.data_dir, "privatekey.pem"), "rb") as fh:
27        pytest.private_key_bytes = fh.read()
28
29    with open(os.path.join(pytest.data_dir, "public_cert.pem"), "rb") as fh:
30        pytest.public_cert_bytes = fh.read()
31
32
33@pytest.fixture
34def mock_non_existent_module(monkeypatch):
35    """Mocks a non-existing module in sys.modules.
36
37    Additionally mocks any non-existing modules specified in the dotted path.
38    """
39
40    def _mock_non_existent_module(path):
41        parts = path.split(".")
42        partial = []
43        for part in parts:
44            partial.append(part)
45            current_module = ".".join(partial)
46            if current_module not in sys.modules:
47                monkeypatch.setitem(sys.modules, current_module, mock.MagicMock())
48
49    return _mock_non_existent_module
50