fix(evennia): remove hardcoded /Users/apayne path from game engines #910
@@ -6,9 +6,10 @@ Not simulation. Story.
|
||||
"""
|
||||
import json, time, os, random
|
||||
from datetime import datetime
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
WORLD_DIR = Path('/Users/apayne/.timmy/evennia/timmy_world')
|
||||
WORLD_DIR = Path(os.getenv('TIMMY_WORLD_DIR', Path.home() / '.timmy' / 'evennia' / 'timmy_world'))
|
||||
STATE_FILE = WORLD_DIR / 'game_state.json'
|
||||
TIMMY_LOG = WORLD_DIR / 'timmy_log.md'
|
||||
|
||||
|
||||
@@ -6,9 +6,10 @@ Not simulation. Story.
|
||||
"""
|
||||
import json, time, os, random
|
||||
from datetime import datetime
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
WORLD_DIR = Path('/Users/apayne/.timmy/evennia/timmy_world')
|
||||
WORLD_DIR = Path(os.getenv('TIMMY_WORLD_DIR', Path.home() / '.timmy' / 'evennia' / 'timmy_world'))
|
||||
STATE_FILE = WORLD_DIR / 'game_state.json'
|
||||
TIMMY_LOG = WORLD_DIR / 'timmy_log.md'
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
from importlib.util import module_from_spec, spec_from_file_location
|
||||
from pathlib import Path
|
||||
import os
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
@@ -67,5 +69,34 @@ class TestEvenniaLocalWorldGame(unittest.TestCase):
|
||||
self.assertIn("The servers hum steady. The green LED pulses.", result["world_events"])
|
||||
|
||||
|
||||
def test_world_dir_is_overrideable_by_environment(self):
|
||||
"""Test that TIMMY_WORLD_DIR env var overrides the hardcoded default."""
|
||||
import importlib.util
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
# Create a temp directory to use as WORLD_DIR
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
override_path = Path(tmp)
|
||||
|
||||
# Patch the environment before loading the module
|
||||
env = os.environ.copy()
|
||||
env['TIMMY_WORLD_DIR'] = str(override_path)
|
||||
|
||||
# Load the module fresh in a new namespace with patched env
|
||||
spec = importlib.util.spec_from_file_location("evennia_local_world_game_override", GAME_PATH)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
assert spec.loader is not None
|
||||
# Execute with patched environ
|
||||
old_env = os.environ
|
||||
os.environ = env
|
||||
try:
|
||||
spec.loader.exec_module(module)
|
||||
finally:
|
||||
os.environ = old_env
|
||||
|
||||
# The WORLD_DIR should equal the override path
|
||||
self.assertEqual(module.WORLD_DIR, override_path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user