Skip to content

Commit

Permalink
improve: test should not modify tracked files
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Feb 19, 2024
1 parent 54183d9 commit 30525c4
Showing 1 changed file with 125 additions and 91 deletions.
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 30525c4

Please sign in to comment.