about summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-07-05 14:54:38 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-07-05 14:54:38 +0300
commit7420ca85c6d5996cb6b47e569de1d34da91bfb92 (patch)
tree1d10c393cf78e833b59736b9f30ad90df0acb2eb /tests
parentb9c9e8d330a3d9652dc4564bc9c544f413a9161c (diff)
downloadfungus-launcher-7420ca85c6d5996cb6b47e569de1d34da91bfb92.tar.gz
fungus-launcher-7420ca85c6d5996cb6b47e569de1d34da91bfb92.tar.xz
deprecate and remove envar.txt and store global envs in config, add per-game env vars
Diffstat (limited to 'tests')
-rw-r--r--tests/test_run_manager.py119
1 files changed, 93 insertions, 26 deletions
diff --git a/tests/test_run_manager.py b/tests/test_run_manager.py
index f324b06..60e8a8a 100644
--- a/tests/test_run_manager.py
+++ b/tests/test_run_manager.py
@@ -229,41 +229,97 @@ class TestResolveCommandPath:
 
 
 # ---------------------------------------------------------------------------
-# Test load_env_from_file
+# Test load_global_env
 # ---------------------------------------------------------------------------
 
 
-class TestLoadEnvFromFile:
-    def test_loads_valid_env_file(self, fungus_run, env_file):
-        result = fungus_run.load_env_from_file(str(env_file))
-        assert result == {"FOO": "bar", "BAZ": "qux"}
+class TestLoadGlobalEnv:
+    def test_loads_envars_from_config(self, fungus_run):
+        """load_global_env should return env vars stored in ConfigManager."""
+        result = fungus_run.load_global_env()
+        assert result == {}  # No envars configured in fixture
 
-    def test_returns_empty_dict_for_missing_file(self, fungus_run, tmp_path):
-        result = fungus_run.load_env_file = fungus_run.load_env_from_file
-        result(tmp_path / "nonexistent.txt")
-        assert result(tmp_path / "nonexistent.txt") == {}
+    def test_ignores_empty_lines(self, tmp_path, fungus_run):
+        """Empty lines in the environment should be skipped."""
+        from fungus.config_manager import ConfigManager
 
-    def test_ignores_empty_lines_and_comments(self, tmp_path, fungus_run):
-        env_path = tmp_path / "env.txt"
-        env_path.write_text(
-            "FOO=bar\n\n# comment\nBAZ=qux\n\n",
-            encoding="utf-8",
-        )
-        result = fungus_run.load_env_from_file(str(env_path))
+        mock_cfg = MagicMock()
+        mock_cfg.get_envars.return_value = ["FOO=bar", "", "BAZ=qux", ""]
+        with patch.object(ConfigManager, "__new__", return_value=mock_cfg):
+            result = fungus_run.load_global_env()
         assert result == {"FOO": "bar", "BAZ": "qux"}
 
     def test_splits_on_first_equals_only(self, tmp_path, fungus_run):
         """Values containing '=' should be preserved."""
-        env_path = tmp_path / "env.txt"
-        env_path.write_text("KEY=value=with=equals\n", encoding="utf-8")
-        result = fungus_run.load_env_from_file(str(env_path))
+        from fungus.config_manager import ConfigManager
+
+        mock_cfg = MagicMock()
+        mock_cfg.get_envars.return_value = ["KEY=value=with=equals"]
+        with patch.object(ConfigManager, "__new__", return_value=mock_cfg):
+            result = fungus_run.load_global_env()
+        assert result == {"KEY": "value=with=equals"}
+
+    def test_ignores_malformed_lines(self, tmp_path, fungus_run):
+        """Lines without '=' should be silently skipped."""
+        from fungus.config_manager import ConfigManager
+
+        mock_cfg = MagicMock()
+        mock_cfg.get_envars.return_value = ["FOO=bar", "malformed", "BAZ=qux"]
+        with patch.object(ConfigManager, "__new__", return_value=mock_cfg):
+            result = fungus_run.load_global_env()
+        assert result == {"FOO": "bar", "BAZ": "qux"}
+
+    def test_strips_whitespace(self, tmp_path, fungus_run):
+        """Keys and values should be stripped of whitespace."""
+        from fungus.config_manager import ConfigManager
+
+        mock_cfg = MagicMock()
+        mock_cfg.get_envars.return_value = ["  KEY  =  value  "]
+        with patch.object(ConfigManager, "__new__", return_value=mock_cfg):
+            result = fungus_run.load_global_env()
+        assert result == {"KEY": "value"}
+
+
+# ---------------------------------------------------------------------------
+# Test _build_per_game_env
+# ---------------------------------------------------------------------------
+
+
+class TestBuildPerGameEnv:
+    def test_empty_environment(self, fungus_run):
+        """An empty environment list should return an empty dict."""
+        fungus_run.game.environment = []
+        assert fungus_run._build_per_game_env() == {}
+
+    def test_parses_env_vars(self, fungus_run):
+        """Valid KEY=VALUE lines should be parsed."""
+        fungus_run.game.environment = ["WINEDLLOVERRIDES=winmm=n,b", "CUSTOM=val"]
+        result = fungus_run._build_per_game_env()
+        assert result == {"WINEDLLOVERRIDES": "winmm=n,b", "CUSTOM": "val"}
+
+    def test_ignores_malformed_lines(self, fungus_run):
+        """Lines without '=' should be silently skipped."""
+        fungus_run.game.environment = ["FOO=bar", "malformed", "BAZ=qux"]
+        result = fungus_run._build_per_game_env()
+        assert result == {"FOO": "bar", "BAZ": "qux"}
+
+    def test_splits_on_first_equals_only(self, fungus_run):
+        """Values containing '=' should be preserved."""
+        fungus_run.game.environment = ["KEY=value=with=equals"]
+        result = fungus_run._build_per_game_env()
         assert result == {"KEY": "value=with=equals"}
 
-    def test_sets_os_environ(self, tmp_path, fungus_run):
-        env_path = tmp_path / "env.txt"
-        env_path.write_text("TEST_VAR=hello\n", encoding="utf-8")
-        fungus_run.load_env_from_file(str(env_path))
-        assert os.environ.get("TEST_VAR") == "hello"
+    def test_strips_whitespace(self, fungus_run):
+        """Keys and values should be stripped of whitespace."""
+        fungus_run.game.environment = ["  KEY  =  value  "]
+        result = fungus_run._build_per_game_env()
+        assert result == {"KEY": "value"}
+
+    def test_duplicate_keys_later_wins(self, fungus_run):
+        """If the same key appears twice, the later entry wins."""
+        fungus_run.game.environment = ["FOO=bar", "FOO=baz"]
+        result = fungus_run._build_per_game_env()
+        assert result == {"FOO": "baz"}
 
 
 # ---------------------------------------------------------------------------
@@ -325,15 +381,26 @@ class TestBuildBwrapCommand:
         assert bwrap_args[bind_idx + 1] == game_dir
         assert bwrap_args[bind_idx + 2] == game_dir
 
-    def test_env_from_file_merged(self, tmp_path, fungus_run, env_file):
+    def test_env_from_file_merged(self, fungus_run):
         """Environment variables from file should be in sandbox_env."""
-        env_data = fungus_run.load_env_from_file(str(env_file))
+        env_data = {"FOO": "bar", "BAZ": "qux"}
         _, sandbox_env, _ = fungus_run.build_bwrap_command(
             fungus_run.game, [], env_data,
         )
         assert sandbox_env["FOO"] == "bar"
         assert sandbox_env["BAZ"] == "qux"
 
+    def test_per_game_env_overrides_file_env(self, fungus_run):
+        """Per-game env should override global env for the same key."""
+        env_data = {"FOO": "bar", "BAZ": "qux"}
+        per_game_data = {"FOO": "override", "CUSTOM": "val"}
+        _, sandbox_env, _ = fungus_run.build_bwrap_command(
+            fungus_run.game, [], env_data, per_game_data,
+        )
+        assert sandbox_env["FOO"] == "override"
+        assert sandbox_env["BAZ"] == "qux"
+        assert sandbox_env["CUSTOM"] == "val"
+
     def test_ro_bind_entries_present(self, fungus_run):
         """Read-only system binds should be present."""
         bwrap_args, _, _ = fungus_run.build_bwrap_command(