1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#!/usr/bin/env python
#
# fixtures.py
r"""
Pytest fixtures.
To enable the fixtures add the following to ``conftest.py`` in your test directory:
.. code-block:: python
pytest_plugins = ("coincidence", )
See `the pytest documentation`_ for more information.
.. _the pytest documentation: https://pytest.org/en/latest/how-to/plugins.html
"""
#
# Copyright © 2020-2021 Dominic Davis-Foster <dominic@davis-foster.co.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
#
# stdlib
import datetime
import os
from pathlib import Path
from typing import Iterator
# 3rd party
import pytest
from domdf_python_tools.paths import PathPlus
# this package
from coincidence.utils import with_fixed_datetime
__import__("pytest_datadir")
__all__ = ("fixed_datetime", "original_datadir", "tmp_pathplus", "path_separator")
@pytest.fixture()
def tmp_pathplus(tmp_path: Path) -> PathPlus:
"""
Pytest fixture which returns a temporary directory in the form of a
:class:`~domdf_python_tools.paths.PathPlus` object.
The directory is unique to each test function invocation,
created as a sub directory of the base temporary directory.
Use it as follows:
.. code-block:: python
pytest_plugins = ("coincidence", )
def test_something(tmp_pathplus: PathPlus):
assert True
""" # noqa: D400
return PathPlus(tmp_path)
@pytest.fixture()
def original_datadir(request) -> Path: # noqa: D103
# Work around pycharm confusing datadir with test file.
return PathPlus(os.path.splitext(request.module.__file__)[0] + '_')
@pytest.fixture()
def fixed_datetime(monkeypatch) -> Iterator:
"""
Pytest fixture to pretend the current datetime is 2:20 AM on 13th October 2020.
.. seealso:: The :func:`~.with_fixed_datetime` contextmanager.
.. attention::
The monkeypatching only works when datetime is used and imported like:
.. code-block:: python
import datetime
print(datetime.datetime.now())
Using ``from datetime import datetime`` won't work.
"""
with with_fixed_datetime(datetime.datetime(2020, 10, 13, 2, 20)):
yield
_skip_forward_mark = pytest.mark.skipif(os.sep == '\\', reason=r"Output differs on platforms where os.sep == '\\'")
_skip_backward_mark = pytest.mark.skipif(os.sep == '/', reason="Output differs on platforms where os.sep == '/'")
@pytest.fixture(
params=[
pytest.param('/', id="forward", marks=_skip_forward_mark),
pytest.param('\\', id="backward", marks=_skip_backward_mark),
]
)
def path_separator(request) -> str:
r"""
Parametrized pytest fixture which returns the current filesystem path separator and skips the test for the other.
This is useful when the test output differs on platforms with ``\`` as the path separator, such as windows.
.. versionadded:: 0.4.0
:rtype:
.. clearpage::
"""
return request.param
|