Skip to content

Commit 9de72c9

Browse files
black-sliverkl3cks7r
authored and
kl3cks7r
committed
Settings: change/fix tests behavior (ArchipelagoMW#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
1 parent 6d4439e commit 9de72c9

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

settings.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
]
2222

2323
no_gui = False
24+
skip_autosave = False
2425
_world_settings_name_cache: Dict[str, str] = {} # TODO: cache on disk and update when worlds change
2526
_world_settings_name_cache_updated = False
2627
_lock = Lock()
@@ -767,11 +768,17 @@ def __init__(self, location: Optional[str]): # change to PathLike[str] once we
767768
self._filename = location
768769

769770
def autosave() -> None:
770-
if self._filename and self.changed:
771+
if __debug__:
772+
import __main__
773+
main_file = getattr(__main__, "__file__", "")
774+
assert "pytest" not in main_file and "unittest" not in main_file, \
775+
f"Auto-saving {self._filename} during unittests"
776+
if self._filename and self.changed and not skip_autosave:
771777
self.save()
772778

773-
import atexit
774-
atexit.register(autosave)
779+
if not skip_autosave:
780+
import atexit
781+
atexit.register(autosave)
775782

776783
def save(self, location: Optional[str] = None) -> None: # as above
777784
location = location or self._filename

test/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
import warnings
2-
warnings.simplefilter("always")
32

3+
import settings
4+
5+
warnings.simplefilter("always")
6+
settings.no_gui = True
7+
settings.skip_autosave = True

test/general/TestHostYAML.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1+
import os
12
import unittest
3+
from tempfile import TemporaryFile
24

5+
from settings import Settings
36
import Utils
47

58

69
class TestIDs(unittest.TestCase):
710
@classmethod
811
def setUpClass(cls) -> None:
9-
with open(Utils.local_path("host.yaml")) as f:
12+
with TemporaryFile("w+", encoding="utf-8") as f:
13+
Settings(None).dump(f)
14+
f.seek(0, os.SEEK_SET)
1015
cls.yaml_options = Utils.parse_yaml(f.read())
1116

12-
def testUtilsHasHost(self):
17+
def test_utils_in_yaml(self) -> None:
1318
for option_key, option_set in Utils.get_default_options().items():
1419
with self.subTest(option_key):
1520
self.assertIn(option_key, self.yaml_options)
1621
for sub_option_key in option_set:
1722
self.assertIn(sub_option_key, self.yaml_options[option_key])
1823

19-
def testHostHasUtils(self):
24+
def test_yaml_in_utils(self) -> None:
2025
utils_options = Utils.get_default_options()
2126
for option_key, option_set in self.yaml_options.items():
2227
with self.subTest(option_key):

0 commit comments

Comments
 (0)