Changeset View
Changeset View
Standalone View
Standalone View
swh/core/pytest_plugin.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from functools import partial | from functools import partial | ||||
import logging | import logging | ||||
from os import path | from os import path | ||||
import re | import re | ||||
from typing import Dict, List, Optional | from typing import Dict, List, Optional | ||||
from urllib.parse import unquote, urlparse | from urllib.parse import unquote, urlparse | ||||
ardumont: no need for the comment | |||||
from _pytest.fixtures import FixtureRequest | |||||
import pytest | import pytest | ||||
import requests | import requests | ||||
from requests.adapters import BaseAdapter | from requests.adapters import BaseAdapter | ||||
from requests.structures import CaseInsensitiveDict | from requests.structures import CaseInsensitiveDict | ||||
from requests.utils import get_encoding_from_headers | from requests.utils import get_encoding_from_headers | ||||
logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||
▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | if not path.isfile(filepath): | ||||
context.status_code = 404 | context.status_code = 404 | ||||
return None | return None | ||||
fd = open(filepath, "rb") | fd = open(filepath, "rb") | ||||
context.headers["content-length"] = str(path.getsize(filepath)) | context.headers["content-length"] = str(path.getsize(filepath)) | ||||
return fd | return fd | ||||
@pytest.fixture | @pytest.fixture | ||||
def datadir(request): | def datadir(request: FixtureRequest) -> str: | ||||
"""By default, returns the test directory's data directory. | """By default, returns the test directory's data directory. | ||||
This can be overridden on a per file tree basis. Add an override | This can be overridden on a per file tree basis. Add an override | ||||
definition in the local conftest, for example:: | definition in the local conftest, for example:: | ||||
import pytest | import pytest | ||||
from os import path | from os import path | ||||
▲ Show 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | |||||
def flask_app_client(app): | def flask_app_client(app): | ||||
with app.test_client() as client: | with app.test_client() as client: | ||||
yield client | yield client | ||||
# stolen from pytest-flask, required to have url_for() working within tests | # stolen from pytest-flask, required to have url_for() working within tests | ||||
# using flask_app_client fixture. | # using flask_app_client fixture. | ||||
@pytest.fixture(autouse=True) | @pytest.fixture(autouse=True) | ||||
def _push_request_context(request): | def _push_request_context(request: FixtureRequest): | ||||
"""During tests execution request context has been pushed, e.g. `url_for`, | """During tests execution request context has been pushed, e.g. `url_for`, | ||||
`session`, etc. can be used in tests as is:: | `session`, etc. can be used in tests as is:: | ||||
def test_app(app, client): | def test_app(app, client): | ||||
assert client.get(url_for('myview')).status_code == 200 | assert client.get(url_for('myview')).status_code == 200 | ||||
""" | """ | ||||
if "app" not in request.fixturenames: | if "app" not in request.fixturenames: | ||||
Show All 9 Lines |
no need for the comment