Skip to content

Commit

Permalink
Use tempfile instead of protected TempPathFactory in QGIS config path…
Browse files Browse the repository at this point in the history
… creation (#34)

* chore: use tempfile instead of protected TempPathFactory

* docs: add mention about importing modules [#35]
  • Loading branch information
Joonalai committed Jun 9, 2023
1 parent bc9adc6 commit 58133fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ markers can be used.
to ensure that they are cleaned properly if they are used but not added to the `QgsProject`. This is only needed with
layers with other than memory provider.
```python
# conftest.py of start of a test file
# conftest.py or start of a test file
import pytest
from pytest_qgis.utils import clean_qgis_layer
from qgis.core import QgsVectorLayer
Expand All @@ -74,11 +74,13 @@ markers can be used.
configure [`QgsApplication`](https://qgis.org/pyqgis/master/core/QgsApplication.html). With QGIS >= 3.18 it is also
used to patch `qgis.utils.iface` with `qgis_iface` automatically.

> Be careful not to import modules importing `qgis.utils.iface` in the root of conftest, because the `pytest_configure` hook has not yet patched `iface` in that point. See [this issue](https://github.com/GispoCoding/pytest-qgis/issues/35) for details.

### Command line options

* `--qgis_disable_gui` can be used to disable graphical user interface in tests. This speeds up the tests that use Qt
widgets of the plugin.
* `--qgis_disable_init` can be used to prevent QGIS (QgsApllication) from initializing. Mainly used in internal testing.
* `--qgis_disable_init` can be used to prevent QGIS (QgsApplication) from initializing. Mainly used in internal testing.

### ini-options

Expand Down
12 changes: 7 additions & 5 deletions src/pytest_qgis/pytest_qgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os.path
import shutil
import sys
import tempfile
import time
import warnings
from collections import namedtuple
Expand All @@ -30,7 +31,6 @@
from unittest import mock

import pytest
from _pytest.tmpdir import TempPathFactory
from qgis.core import Qgis, QgsApplication, QgsProject, QgsRectangle, QgsVectorLayer
from qgis.gui import QgisInterface as QgisInterfaceOrig
from qgis.gui import QgsGui, QgsLayerTreeMapCanvasBridge, QgsMapCanvas
Expand Down Expand Up @@ -92,6 +92,7 @@
_IFACE: Optional[QgisInterface] = None
_PARENT: Optional[QtWidgets.QWidget] = None
_AUTOUSE_QGIS: Optional[bool] = None
_QGIS_CONFIG_PATH: Optional[Path] = None

try:
_QGIS_VERSION = Qgis.versionInt()
Expand Down Expand Up @@ -153,6 +154,8 @@ def qgis_app(request: "SubRequest") -> QgsApplication:
if not sip.isdeleted(_CANVAS) and _CANVAS is not None:
_CANVAS.deleteLater()
_APP.exitQgis()
if _QGIS_CONFIG_PATH and _QGIS_CONFIG_PATH.exists():
shutil.rmtree(_QGIS_CONFIG_PATH)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -280,13 +283,12 @@ def qgis_show_map(


def _start_and_configure_qgis_app(config: "Config") -> None:
global _APP, _CANVAS, _IFACE, _PARENT
global _APP, _CANVAS, _IFACE, _PARENT, _QGIS_CONFIG_PATH
settings: Settings = config._plugin_settings # type: ignore

# Use temporary path for QGIS config
tmp_path_factory = TempPathFactory.from_config(config, _ispytest=True)
config_path = tmp_path_factory.mktemp("qgis-test")
os.environ["QGIS_CUSTOM_CONFIG_PATH"] = str(config_path)
_QGIS_CONFIG_PATH = Path(tempfile.mkdtemp(prefix="pytest-qgis"))
os.environ["QGIS_CUSTOM_CONFIG_PATH"] = str(_QGIS_CONFIG_PATH)

if not settings.qgis_init_disabled:
_APP = QgsApplication([], GUIenabled=settings.gui_enabled)
Expand Down

0 comments on commit 58133fa

Please sign in to comment.