0
This repository has been archived on 2025-04-25. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Minecraft-Overviewer/test/test_settings.py
Nicolas F 0d3d630104 Get rid of config global state, improve tests
While dicking around with the tests I noticed that you could make
them fail if you ran them twice. Many hours were spent investigating,
and it turns out that Overviewer's config stuff has global state that
gets modified by having parsed configurations actually modify the
default config values. Not good!

We can fix this by having settingsDefinition return a dict of the
defaults, instead of assigning it to module-level names.

We can also get rid of test_all.py, because what it would do with
pytest is run all tests *twice*. Instead, do the module path stuff
in __init__.py. Also, instead of throwing a non-specific Exception
if exmaple isn't checked out, just skip the test thank you very much.

This is the weirdest rabbit hole I've ever gone down and I haven't
slept in about 30 hours. I'm going to push this commit, and if it
breaks anything, I'll be blissfully asleep as the unwashed masses
begin to riot over exception this traceback that.
2019-07-24 17:02:39 +02:00

90 lines
3.1 KiB
Python

import unittest
from collections import OrderedDict
from overviewer_core import config_parser
from overviewer_core.settingsValidators import ValidationException
from overviewer_core import world
from overviewer_core import rendermodes
class SettingsTest(unittest.TestCase):
def setUp(self):
self.s = config_parser.MultiWorldParser()
def test_missing(self):
"Validates that a non-existant settings.py causes an exception"
self.assertRaises(config_parser.MissingConfigException, self.s.parse, "doesnotexist.py")
def test_existing_file(self):
self.s.parse("test/data/settings/settings_test_1.py")
things = self.s.get_validated_config()
# no exceptions so far. that's a good thing
# Test the default
self.assertEqual(things['renders']['myworld']['bgcolor'], (26,26,26,0))
# Test a non-default
self.assertEqual(things['renders']['otherworld']['bgcolor'], (255,255,255,0))
self.assertEqual(things['renders']['myworld']['northdirection'],
world.UPPER_LEFT)
def test_rendermode_validation(self):
self.s.parse("test/data/settings/settings_test_rendermode.py")
self.assertRaises(ValidationException,self.s.get_validated_config)
def test_manual(self):
"""Tests that manually setting the config parser works, you don't have
to do it from a file
"""
fromfile = config_parser.MultiWorldParser()
fromfile.parse("test/data/settings/settings_test_1.py")
self.s.set_config_item("worlds", {
'test': "test/data/settings/test_world",
})
self.s.set_config_item("renders", OrderedDict([
("myworld", {
"title": "myworld title",
"world": "test",
"rendermode": rendermodes.normal,
"northdirection": "upper-left",
}),
("otherworld", {
"title": "otherworld title",
"world": "test",
"rendermode": rendermodes.normal,
"bgcolor": "#ffffff"
}),
]))
self.s.set_config_item("outputdir", "/tmp/fictional/outputdir")
first = fromfile.get_validated_config()
del first["observer"]
second = self.s.get_validated_config()
del second["observer"]
self.assertEqual(first, second)
def test_rendermode_string(self):
self.s.set_config_item("worlds", {
'test': "test/data/settings/test_world",
})
self.s.set_config_item("outputdir", "/tmp/fictional/outputdir")
self.s.set_config_item("renders", {
"myworld": {
"title": "myworld title",
"world": "test",
"rendermode": "normal",
"northdirection": "upper-left",
},
})
p = self.s.get_validated_config()
self.assertEqual(p['renders']['myworld']['rendermode'], rendermodes.normal)
if __name__ == "__main__":
unittest.main()