From c390801c4c03f83a7aba47912fe40bc4220d1a2c Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Tue, 23 Aug 2022 01:07:17 +0200 Subject: [PATCH] Test: verify file webhost file creations work to some degree (#953) WebHost: fix some file creation paths --- WebHostLib/options.py | 13 ++++++++----- test/webhost/TestFileGeneration.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 test/webhost/TestFileGeneration.py diff --git a/WebHostLib/options.py b/WebHostLib/options.py index e2d362a5..daa742d9 100644 --- a/WebHostLib/options.py +++ b/WebHostLib/options.py @@ -1,6 +1,6 @@ import logging import os -from Utils import __version__ +from Utils import __version__, local_path from jinja2 import Template import yaml import json @@ -9,14 +9,13 @@ import typing from worlds.AutoWorld import AutoWorldRegister import Options -target_folder = os.path.join("WebHostLib", "static", "generated") - handled_in_js = {"start_inventory", "local_items", "non_local_items", "start_hints", "start_location_hints", "exclude_locations"} def create(): - os.makedirs(os.path.join(target_folder, 'configs'), exist_ok=True) + target_folder = local_path("WebHostLib", "static", "generated") + os.makedirs(os.path.join(target_folder, "configs"), exist_ok=True) def dictify_range(option: typing.Union[Options.Range, Options.SpecialRange]): data = {} @@ -66,12 +65,16 @@ def create(): for game_name, world in AutoWorldRegister.world_types.items(): all_options = {**Options.per_game_common_options, **world.option_definitions} - res = Template(open(os.path.join("WebHostLib", "templates", "options.yaml")).read()).render( + with open(local_path("WebHostLib", "templates", "options.yaml")) as f: + file_data = f.read() + res = Template(file_data).render( options=all_options, __version__=__version__, game=game_name, yaml_dump=yaml.dump, dictify_range=dictify_range, default_converter=default_converter, ) + del file_data + with open(os.path.join(target_folder, 'configs', game_name + ".yaml"), "w") as f: f.write(res) diff --git a/test/webhost/TestFileGeneration.py b/test/webhost/TestFileGeneration.py new file mode 100644 index 00000000..7f56864e --- /dev/null +++ b/test/webhost/TestFileGeneration.py @@ -0,0 +1,23 @@ +"""Tests for successful generation of WebHost cached files. Can catch some other deeper errors.""" + +import os +import unittest + +import WebHost + + +class TestFileGeneration(unittest.TestCase): + def setUp(self) -> None: + self.correct_path = os.path.join(os.path.dirname(WebHost.__file__), "WebHostLib") + # should not create the folder *here* + self.incorrect_path = os.path.join(os.path.split(os.path.dirname(__file__))[0], "WebHostLib") + + def testOptions(self): + WebHost.create_options_files() + self.assertTrue(os.path.exists(os.path.join(self.correct_path, "static", "generated", "configs"))) + self.assertFalse(os.path.exists(os.path.join(self.incorrect_path, "static", "generated", "configs"))) + + def testTutorial(self): + WebHost.create_ordered_tutorials_file() + self.assertTrue(os.path.exists(os.path.join(self.correct_path, "static", "generated", "tutorials.json"))) + self.assertFalse(os.path.exists(os.path.join(self.incorrect_path, "static", "generated", "tutorials.json")))