Skip to content

Commit

Permalink
Fix leftover directories after tests (#2360)
Browse files Browse the repository at this point in the history
* Fix osfs:/ directory leftover in CWD after tests

* Fix leftover directories in /tmp after tests

The way wms_cache was set previously made it so that after each tests
run there would be one or more directories leftover in /tmp. This also
happened for each start of MSUI. This change makes MSUI use the
platform-specific cache directory of the user instead and sets
XDG_CACHE_HOME to isolate those between test runs.
  • Loading branch information
matrss authored May 16, 2024
1 parent c265c0b commit 027cff4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
7 changes: 6 additions & 1 deletion mslib/msui/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@
import fs
import os
import logging
import platformdirs

# ToDo refactor to generic functions, keep only constants
HOME = os.path.expanduser(f"~{os.path.sep}")
MSUI_CONFIG_PATH = os.getenv("MSUI_CONFIG_PATH", os.path.join(HOME, ".config", "msui"))
# Make sure that MSUI_CONFIG_PATH exists
_ = fs.open_fs(MSUI_CONFIG_PATH, create=True)
_fs = fs.open_fs(MSUI_CONFIG_PATH, create=True)
# MSUI does not actually support any PyFilesystem2 fs that is not available as a local path
MSUI_CONFIG_SYSPATH = _fs.getsyspath("")

MSUI_CACHE_PATH = platformdirs.user_cache_path("msui", "mss")

GRAVATAR_DIR_PATH = fs.path.join(MSUI_CONFIG_PATH, "gravatars")

Expand Down
18 changes: 5 additions & 13 deletions mslib/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@
See the License for the specific language governing permissions and
limitations under the License.
"""

import sys
from PyQt5 import QtCore

import copy
import json
import logging
import fs
import os
import tempfile

from mslib.utils import FatalUserError
from mslib.msui import constants
Expand Down Expand Up @@ -151,8 +148,7 @@ class MSUIDefaultConfig:
WMS_preload = []

# WMS image cache settings:
# this changes on any start of msui, use ths msui_settings.json when you want a persistent path
wms_cache = os.path.join(tempfile.TemporaryDirectory().name, "msui_wms_cache")
wms_cache = str(constants.MSUI_CACHE_PATH / "wms_cache")

# Maximum size of the cache in bytes.
wms_cache_max_size_bytes = 20 * 1024 * 1024
Expand Down Expand Up @@ -457,7 +453,7 @@ def config_loader(dataset=None, default=False):
return user_options


def save_settings_qsettings(tag, settings, ignore_test=False):
def save_settings_qsettings(tag, settings):
"""
Saves a dictionary settings to disk.
Expand All @@ -467,10 +463,8 @@ def save_settings_qsettings(tag, settings, ignore_test=False):
"""
assert isinstance(tag, str)
assert isinstance(settings, dict)
if not ignore_test and ("pytest" in sys.modules):
return settings
# ToDo we have to verify if we can all switch to this definition, not having 3 different
q_settings = QtCore.QSettings(os.path.join(constants.MSUI_CONFIG_PATH, "msui-core.conf"),
q_settings = QtCore.QSettings(os.path.join(constants.MSUI_CONFIG_SYSPATH, "msui-core.conf"),
QtCore.QSettings.IniFormat)

file_path = q_settings.fileName()
Expand All @@ -482,7 +476,7 @@ def save_settings_qsettings(tag, settings, ignore_test=False):
return settings


def load_settings_qsettings(tag, default_settings=None, ignore_test=False):
def load_settings_qsettings(tag, default_settings=None):
"""
Loads a dictionary of settings from disk. May supply a dictionary of default settings
to return in case the settings file is not present or damaged. The default_settings one will
Expand All @@ -496,12 +490,10 @@ def load_settings_qsettings(tag, default_settings=None, ignore_test=False):
if default_settings is None:
default_settings = {}
assert isinstance(default_settings, dict)
if not ignore_test and "pytest" in sys.modules:
return default_settings

settings = {}

q_settings = QtCore.QSettings(os.path.join(constants.MSUI_CONFIG_PATH, "msui-core.conf"),
q_settings = QtCore.QSettings(os.path.join(constants.MSUI_CONFIG_SYSPATH, "msui-core.conf"),
QtCore.QSettings.IniFormat)
file_path = q_settings.fileName()
logging.debug("loading settings for %s from %s", tag, file_path)
Expand Down
6 changes: 3 additions & 3 deletions tests/_test_utils/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ class TestSettingsSave:

def test_save_settings(self):
settings = {'foo': 'bar'}
config.save_settings_qsettings(self.tag, settings, ignore_test=True)
config.save_settings_qsettings(self.tag, settings)

def test_load_settings(self):
settings = {'foo': 'bar'}
config.save_settings_qsettings(self.tag, settings, ignore_test=True)
settings = config.load_settings_qsettings(self.tag, ignore_test=True)
config.save_settings_qsettings(self.tag, settings)
settings = config.load_settings_qsettings(self.tag)
assert isinstance(settings, dict)
assert settings["foo"] == "bar"

Expand Down
4 changes: 4 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import os
import fs
import tempfile
from fs.tempfs import TempFS

try:
Expand Down Expand Up @@ -60,6 +61,9 @@
os.environ["MSUI_CONFIG_PATH"] = MSUI_CONFIG_PATH
SERVER_CONFIG_FILE_PATH = fs.path.join(SERVER_CONFIG_FS.getsyspath(""), SERVER_CONFIG_FILE)

_xdg_cache_home_temporary_directory = tempfile.TemporaryDirectory()
os.environ["XDG_CACHE_HOME"] = _xdg_cache_home_temporary_directory.name

# we keep DATA_DIR until we move netCDF4 files to pyfilesystem2
DATA_DIR = DATA_FS.getsyspath("")

Expand Down

0 comments on commit 027cff4

Please sign in to comment.