conftest.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. import os
  15. import sys
  16. import mock
  17. import pytest # type: ignore
  18. def pytest_configure():
  19. """Load public certificate and private key."""
  20. import yatest.common as yc
  21. pytest.data_dir = os.path.join(os.path.dirname(yc.source_path("contrib/python/google-auth/py3/tests/conftest.py")), "data")
  22. with open(os.path.join(pytest.data_dir, "privatekey.pem"), "rb") as fh:
  23. pytest.private_key_bytes = fh.read()
  24. with open(os.path.join(pytest.data_dir, "public_cert.pem"), "rb") as fh:
  25. pytest.public_cert_bytes = fh.read()
  26. @pytest.fixture
  27. def mock_non_existent_module(monkeypatch):
  28. """Mocks a non-existing module in sys.modules.
  29. Additionally mocks any non-existing modules specified in the dotted path.
  30. """
  31. def _mock_non_existent_module(path):
  32. parts = path.split(".")
  33. partial = []
  34. for part in parts:
  35. partial.append(part)
  36. current_module = ".".join(partial)
  37. if current_module not in sys.modules:
  38. monkeypatch.setitem(sys.modules, current_module, mock.MagicMock())
  39. return _mock_non_existent_module