Skip to content

Commit

Permalink
Improve: testing ini files against untracked files (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts authored Feb 19, 2024
2 parents 29f086b + 30525c4 commit 7a1df0c
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 95 deletions.
28 changes: 24 additions & 4 deletions qgis_deployment_toolbelt/profiles/qgis_ini_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ def __init__(
"""Instanciation.
Args:
ini_filepath: path to the QGIS3.ini configuration file
ini_filepath (Path): path to the QGIS3.ini configuration file
ini_type (Literal[ "profile_qgis3", \
"profile_qgis3customization", "plugin_metadata", \
None ], optional): type of ini file. None enables autodetection. \
Defaults to None.
strict (bool, optional): strict mode applied to ConfigParser. Defaults to \
False.
enable_environment_variables_interpolation (bool, optional): if enabled, \
values matching environment variables are interepreted. Defaults to True.
"""
if (
ini_filepath is not None
Expand Down Expand Up @@ -95,6 +103,16 @@ def __init__(
else:
logger.warning(f"Unrecognized ini type: {ini_filepath}")

# check if file exists
if not check_path(
input_path=ini_filepath,
must_be_a_file=True,
must_be_readable=True,
must_exists=True,
raise_error=False,
):
logger.info(f"The specified file does not exist: {ini_filepath.resolve()}.")

# store options
self.strict_mode = strict
self.enable_environment_variables_interpolation = (
Expand All @@ -109,9 +127,11 @@ def cfg_parser(self) -> CustomConfigParser:
"""
qgis_cfg_parser = CustomConfigParser(
strict=self.strict_mode,
interpolation=EnvironmentVariablesInterpolation()
if self.enable_environment_variables_interpolation
else None,
interpolation=(
EnvironmentVariablesInterpolation()
if self.enable_environment_variables_interpolation
else None
),
)
qgis_cfg_parser.optionxform = str
return qgis_cfg_parser
Expand Down
216 changes: 125 additions & 91 deletions tests/test_qgis_ini_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

# standard
import tempfile
import unittest
from pathlib import Path

Expand All @@ -37,139 +38,172 @@ def test_load_profile_config_default(self):
new_config_file = Path(
"tests/fixtures/qgis_ini/default_no_customization/QGIS3.ini"
)
ini_config = QgisIniHelper(ini_filepath=new_config_file)

with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
tmp_copy = Path(tmpdirname).joinpath(new_config_file.name)
tmp_copy.write_text(new_config_file.read_text())

# open temp copy
ini_config = QgisIniHelper(ini_filepath=tmp_copy)

self.assertEqual(ini_config.ini_type, "profile_qgis3")
self.assertFalse(ini_config.is_ui_customization_enabled())

def test_load_profile_customization_splash_screen(self):
"""Test profile QGIS/QGIS3.ini loader."""
new_config_file = Path(
fixture_ini_file = Path(
"tests/fixtures/qgis_ini/default_customization/QGISCUSTOMIZATION3.ini"
)
ini_customization = QgisIniHelper(ini_filepath=new_config_file)

self.assertEqual(ini_customization.ini_type, "profile_qgis3customization")
with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
tmp_copy = Path(tmpdirname).joinpath(fixture_ini_file.name)
tmp_copy.write_text(fixture_ini_file.read_text())

ini_config = QgisIniHelper(ini_filepath=ini_customization.profile_config_path)
ini_customization = QgisIniHelper(ini_filepath=tmp_copy)

self.assertEqual(ini_customization.ini_type, "profile_qgis3customization")
ini_config = QgisIniHelper(ini_filepath=ini_customization.profile_config_path)
self.assertEqual(ini_config.ini_type, "profile_qgis3")

def test_enable_customization(self):
"""Test profile QGIS/QGIS3.ini loader."""
new_config_file = Path(
fixture_ini_file = Path(
"tests/fixtures/qgis_ini/default_no_customization/QGIS3.ini"
)
ini_config = QgisIniHelper(ini_filepath=new_config_file)
self.assertFalse(ini_config.is_ui_customization_enabled())

ini_config.set_ui_customization_enabled(switch=True)
self.assertTrue(ini_config.is_ui_customization_enabled())
with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
tmp_copy = Path(tmpdirname).joinpath(fixture_ini_file.name)
tmp_copy.write_text(fixture_ini_file.read_text())

ini_config.set_ui_customization_enabled(switch=False)
self.assertFalse(ini_config.is_ui_customization_enabled())
ini_config = QgisIniHelper(ini_filepath=tmp_copy)
self.assertFalse(ini_config.is_ui_customization_enabled())

ini_config.set_ui_customization_enabled(switch=True)
self.assertTrue(ini_config.is_ui_customization_enabled())

ini_config.set_ui_customization_enabled(switch=False)
self.assertFalse(ini_config.is_ui_customization_enabled())

def test_enable_customization_on_unexisting_file(self):
"""Test profile QGIS/QGIS3.ini loader."""
unexisting_config_file = Path("Imaginary_QGIS3.ini")
self.assertFalse(unexisting_config_file.exists())
ini_config = QgisIniHelper(
ini_filepath=unexisting_config_file, ini_type="profile_qgis3"
)
self.assertFalse(ini_config.is_ui_customization_enabled())
self.assertFalse(unexisting_config_file.exists())

ini_config.set_ui_customization_enabled(switch=True)
self.assertTrue(ini_config.is_ui_customization_enabled())
self.assertTrue(unexisting_config_file.exists())
with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
unexisting_config_file = Path(tmpdirname).joinpath("Imaginary_QGIS3.ini")
self.assertFalse(unexisting_config_file.exists())

ini_config.set_ui_customization_enabled(switch=False)
self.assertFalse(ini_config.is_ui_customization_enabled())
self.assertTrue(unexisting_config_file.exists())
ini_config = QgisIniHelper(
ini_filepath=unexisting_config_file, ini_type="profile_qgis3"
)
self.assertFalse(ini_config.is_ui_customization_enabled())
self.assertFalse(unexisting_config_file.exists())

unexisting_config_file.unlink(missing_ok=True)
ini_config.set_ui_customization_enabled(switch=True)
self.assertTrue(ini_config.is_ui_customization_enabled())
self.assertTrue(unexisting_config_file.exists())

ini_config.set_ui_customization_enabled(switch=False)
self.assertFalse(ini_config.is_ui_customization_enabled())
self.assertTrue(unexisting_config_file.exists())

def test_splash_already_set(self):
fake_config = (
"[Customization]\nsplashpath=/home/$USER/.share/QGIS/QGIS3/images/"
)

tmp_ini_customization = Path(
"tests/fixtures/tmp/customization_with_splashpath.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization, ini_type="profile_qgis3customization"
)
self.assertTrue(qini_helper.is_splash_screen_set())

tmp_ini_customization.unlink()
with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
tmp_ini_customization = Path(tmpdirname).joinpath(
"customization_with_splashpath.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization,
ini_type="profile_qgis3customization",
)
self.assertTrue(qini_helper.is_splash_screen_set())

def test_splash_already_set_but_empty(self):
fake_config = "[Customization]\nsplashpath="

tmp_ini_customization = Path(
"tests/fixtures/tmp/customization_with_splashpath_empty.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization, ini_type="profile_qgis3customization"
)
self.assertFalse(qini_helper.is_splash_screen_set())

tmp_ini_customization.unlink()
with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
tmp_ini_customization = Path(tmpdirname).joinpath(
"customization_with_splashpath_empty.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization,
ini_type="profile_qgis3customization",
)
self.assertFalse(qini_helper.is_splash_screen_set())

def test_splash_not_set(self):
fake_config = "[Customization]\nMenu\\mDatabaseMenu=false"

tmp_ini_customization = Path(
"tests/fixtures/tmp/customization_with_no_splashpath_set.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization, ini_type="profile_qgis3customization"
)
self.assertFalse(qini_helper.is_splash_screen_set())

tmp_ini_customization.unlink()
with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
tmp_ini_customization = Path(tmpdirname).joinpath(
"customization_with_no_splashpath_set.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization,
ini_type="profile_qgis3customization",
)
self.assertFalse(qini_helper.is_splash_screen_set())

def test_disable_splash_already_not_set(self):
fake_config = "[Customization]\nMenu\\mDatabaseMenu=false"

tmp_ini_customization = Path(
"tests/fixtures/tmp/set_splash_path_on_customization_with_no_splashpath_set.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization, ini_type="profile_qgis3customization"
)
self.assertFalse(qini_helper.set_splash_screen(switch=False))

tmp_ini_customization.unlink()
with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
tmp_ini_customization = Path(tmpdirname).joinpath(
"set_splash_path_on_customization_with_no_splashpath_set.ini"
)
tmp_ini_customization.parent.mkdir(parents=True, exist_ok=True)
tmp_ini_customization.write_text(fake_config)

qini_helper = QgisIniHelper(
ini_filepath=tmp_ini_customization,
ini_type="profile_qgis3customization",
)
self.assertFalse(qini_helper.set_splash_screen(switch=False))

def test_disable_splash_on_unexisting_file(self):
not_existing_ini_customization = Path("not_existing_customization.ini")

qini_helper = QgisIniHelper(
ini_filepath=not_existing_ini_customization,
ini_type="profile_qgis3customization",
)
self.assertFalse(qini_helper.set_splash_screen(switch=False))

not_existing_ini_config = Path("QGIS3.ini")

qini_helper = QgisIniHelper(
ini_filepath=not_existing_ini_config, ini_type="profile_qgis3"
)
self.assertFalse(qini_helper.set_splash_screen(switch=False))
with tempfile.TemporaryDirectory(
prefix="qdt_test_ini_file_", ignore_cleanup_errors=True
) as tmpdirname:
not_existing_ini_customization = Path(tmpdirname).joinpath(
"not_existing_customization.ini"
)

qini_helper = QgisIniHelper(
ini_filepath=not_existing_ini_customization,
ini_type="profile_qgis3customization",
)
self.assertFalse(qini_helper.set_splash_screen(switch=False))

not_existing_ini_config = Path("QGIS3.ini")

qini_helper = QgisIniHelper(
ini_filepath=not_existing_ini_config, ini_type="profile_qgis3"
)
self.assertFalse(qini_helper.set_splash_screen(switch=False))


# ############################################################################
Expand Down

0 comments on commit 7a1df0c

Please sign in to comment.