Settings: change/fix tests behavior (#2053)
* Settings: disable saving and gui during tests * Tests: create a fresh host.yaml for TestHostYAML Now that host.yaml is .gitignored, testing the local host.yaml makes no sense anymore
This commit is contained in:
parent
a90825eac3
commit
7b8f8918fc
13
settings.py
13
settings.py
|
@ -21,6 +21,7 @@ __all__ = [
|
||||||
]
|
]
|
||||||
|
|
||||||
no_gui = False
|
no_gui = False
|
||||||
|
skip_autosave = False
|
||||||
_world_settings_name_cache: Dict[str, str] = {} # TODO: cache on disk and update when worlds change
|
_world_settings_name_cache: Dict[str, str] = {} # TODO: cache on disk and update when worlds change
|
||||||
_world_settings_name_cache_updated = False
|
_world_settings_name_cache_updated = False
|
||||||
_lock = Lock()
|
_lock = Lock()
|
||||||
|
@ -767,11 +768,17 @@ class Settings(Group):
|
||||||
self._filename = location
|
self._filename = location
|
||||||
|
|
||||||
def autosave() -> None:
|
def autosave() -> None:
|
||||||
if self._filename and self.changed:
|
if __debug__:
|
||||||
|
import __main__
|
||||||
|
main_file = getattr(__main__, "__file__", "")
|
||||||
|
assert "pytest" not in main_file and "unittest" not in main_file, \
|
||||||
|
f"Auto-saving {self._filename} during unittests"
|
||||||
|
if self._filename and self.changed and not skip_autosave:
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
import atexit
|
if not skip_autosave:
|
||||||
atexit.register(autosave)
|
import atexit
|
||||||
|
atexit.register(autosave)
|
||||||
|
|
||||||
def save(self, location: Optional[str] = None) -> None: # as above
|
def save(self, location: Optional[str] = None) -> None: # as above
|
||||||
location = location or self._filename
|
location = location or self._filename
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
import warnings
|
import warnings
|
||||||
warnings.simplefilter("always")
|
|
||||||
|
|
||||||
|
import settings
|
||||||
|
|
||||||
|
warnings.simplefilter("always")
|
||||||
|
settings.no_gui = True
|
||||||
|
settings.skip_autosave = True
|
||||||
|
|
|
@ -1,22 +1,27 @@
|
||||||
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
|
from tempfile import TemporaryFile
|
||||||
|
|
||||||
|
from settings import Settings
|
||||||
import Utils
|
import Utils
|
||||||
|
|
||||||
|
|
||||||
class TestIDs(unittest.TestCase):
|
class TestIDs(unittest.TestCase):
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpClass(cls) -> None:
|
def setUpClass(cls) -> None:
|
||||||
with open(Utils.local_path("host.yaml")) as f:
|
with TemporaryFile("w+", encoding="utf-8") as f:
|
||||||
|
Settings(None).dump(f)
|
||||||
|
f.seek(0, os.SEEK_SET)
|
||||||
cls.yaml_options = Utils.parse_yaml(f.read())
|
cls.yaml_options = Utils.parse_yaml(f.read())
|
||||||
|
|
||||||
def testUtilsHasHost(self):
|
def test_utils_in_yaml(self) -> None:
|
||||||
for option_key, option_set in Utils.get_default_options().items():
|
for option_key, option_set in Utils.get_default_options().items():
|
||||||
with self.subTest(option_key):
|
with self.subTest(option_key):
|
||||||
self.assertIn(option_key, self.yaml_options)
|
self.assertIn(option_key, self.yaml_options)
|
||||||
for sub_option_key in option_set:
|
for sub_option_key in option_set:
|
||||||
self.assertIn(sub_option_key, self.yaml_options[option_key])
|
self.assertIn(sub_option_key, self.yaml_options[option_key])
|
||||||
|
|
||||||
def testHostHasUtils(self):
|
def test_yaml_in_utils(self) -> None:
|
||||||
utils_options = Utils.get_default_options()
|
utils_options = Utils.get_default_options()
|
||||||
for option_key, option_set in self.yaml_options.items():
|
for option_key, option_set in self.yaml_options.items():
|
||||||
with self.subTest(option_key):
|
with self.subTest(option_key):
|
||||||
|
|
Loading…
Reference in New Issue