Skip to content

Settings: change/fix tests behavior #2053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
]

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_updated = False
_lock = Lock()
Expand Down Expand Up @@ -767,11 +768,17 @@ def __init__(self, location: Optional[str]): # change to PathLike[str] once we
self._filename = location

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()

import atexit
atexit.register(autosave)
if not skip_autosave:
import atexit
atexit.register(autosave)

def save(self, location: Optional[str] = None) -> None: # as above
location = location or self._filename
Expand Down
6 changes: 5 additions & 1 deletion test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import warnings
warnings.simplefilter("always")

import settings

warnings.simplefilter("always")
settings.no_gui = True
settings.skip_autosave = True
11 changes: 8 additions & 3 deletions test/general/TestHostYAML.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import os
import unittest
from tempfile import TemporaryFile

from settings import Settings
import Utils


class TestIDs(unittest.TestCase):
@classmethod
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())

def testUtilsHasHost(self):
def test_utils_in_yaml(self) -> None:
for option_key, option_set in Utils.get_default_options().items():
with self.subTest(option_key):
self.assertIn(option_key, self.yaml_options)
for sub_option_key in option_set:
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()
for option_key, option_set in self.yaml_options.items():
with self.subTest(option_key):
Expand Down