prole/tests/test_install_logic.py

77 lines
2.8 KiB
Python

import sys
from unittest.mock import patch, MagicMock
# Mock tkinter and other GUI/macOS specific imports before they are imported in install.py
mock_tk = MagicMock()
mock_tk.Entry.return_value.get.return_value.strip.return_value = "mock_val"
mock_tk.StringVar.return_value.get.return_value.strip.return_value = "mock_val"
sys.modules["tkinter"] = mock_tk
sys.modules["tkinter.ttk"] = MagicMock()
sys.modules["tkinter.scrolledtext"] = MagicMock()
sys.modules["tkinter.messagebox"] = MagicMock()
sys.modules["Foundation"] = MagicMock()
sys.modules["objc"] = MagicMock()
import os
import pytest
from pathlib import Path
# Now import after mocking
from knoe.ui.screens import KnoeInstaller
@pytest.fixture
def installer(tmp_path):
# Mocking os.environ to avoid messing with real env
with patch.dict(os.environ, {}, clear=True):
# We need to mock KnoeInstaller.__init__ because it creates GUI elements
with patch.object(KnoeInstaller, "__init__", return_value=None):
ins = KnoeInstaller()
# Core helpers expect `inputs` to exist; bypassing `__init__` means
# we must seed minimal state for method calls.
ins.inputs = {}
return ins
def test_resolve_knoe_home_default(installer):
with patch.dict(os.environ, {}, clear=True):
# Path.home() might vary, so we just check it ends with .knoe if no env var
home = installer.resolve_knoe_home()
assert home.name == ".knoe"
def test_resolve_knoe_home_env(installer):
custom_home = "/tmp/custom_knoe"
with patch.dict(os.environ, {"KNOE_HOME": custom_home}):
home = installer.resolve_knoe_home()
assert str(home) == custom_home
def test_env_defaults(installer):
with patch.dict(os.environ, {}, clear=True), patch.object(
installer, "_read_existing_env", return_value={}
):
defaults = installer._env_defaults()
assert "KNOE_HOME" in defaults
assert defaults["KNOE_HOME"].endswith(".knoe")
assert defaults["KNOE_CONF"].endswith(".knoe/conf")
def test_read_existing_env_no_file(installer, tmp_path):
# Ensure neither KNOE_HOME nor the default location has an env.sh for this test
with patch.dict(os.environ, {"KNOE_HOME": str(tmp_path)}):
with patch("pathlib.Path.home", return_value=tmp_path / "fake_home"):
env = installer._read_existing_env()
assert env == {}
def test_read_existing_env_with_file(installer, tmp_path):
env_sh = tmp_path / "env.sh"
env_sh.write_text('export VAR1="val1"\nVAR2=val2\n# comment\n')
with patch.dict(os.environ, {"KNOE_HOME": str(tmp_path)}):
with patch("pathlib.Path.home", return_value=tmp_path / "fake_home"):
env = installer._read_existing_env()
assert env.get("VAR1") == "val1"
assert env.get("VAR2") == "val2"