v1.2.0
Co-Authored-By: Quentin Torroba <quentin.torroba@mistral.ai> Co-Authored-By: Michel Thomazo <michel.thomazo@mistral.ai> Co-Authored-By: Kracekumar <kracethekingmaker@gmail.com>
This commit is contained in:
parent
661588de0c
commit
d8dbeeb31e
91 changed files with 4521 additions and 873 deletions
|
|
@ -4,28 +4,41 @@ from pathlib import Path
|
|||
|
||||
import pytest
|
||||
|
||||
from vibe.core.config_path import CONFIG_FILE, GLOBAL_CONFIG_FILE, VIBE_HOME
|
||||
from vibe.core.paths.config_paths import CONFIG_FILE
|
||||
from vibe.core.paths.global_paths import GLOBAL_CONFIG_FILE, VIBE_HOME
|
||||
from vibe.core.trusted_folders import trusted_folders_manager
|
||||
|
||||
|
||||
class TestResolveConfigFile:
|
||||
def test_resolves_local_config_when_exists(
|
||||
def test_resolves_local_config_when_exists_and_folder_is_trusted(
|
||||
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""Test that local .vibe/config.toml is found when it exists."""
|
||||
monkeypatch.chdir(tmp_path)
|
||||
local_config_dir = tmp_path / ".vibe"
|
||||
local_config_dir.mkdir()
|
||||
local_config = local_config_dir / "config.toml"
|
||||
local_config.write_text('active_model = "test"', encoding="utf-8")
|
||||
|
||||
monkeypatch.setattr(trusted_folders_manager, "is_trusted", lambda _: True)
|
||||
|
||||
assert CONFIG_FILE.path == local_config
|
||||
assert CONFIG_FILE.path.is_file()
|
||||
assert CONFIG_FILE.path.read_text(encoding="utf-8") == 'active_model = "test"'
|
||||
|
||||
def test_resolves_local_config_when_exists_and_folder_is_not_trusted(
|
||||
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.chdir(tmp_path)
|
||||
local_config_dir = tmp_path / ".vibe"
|
||||
local_config_dir.mkdir()
|
||||
local_config = local_config_dir / "config.toml"
|
||||
local_config.write_text('active_model = "test"', encoding="utf-8")
|
||||
|
||||
assert CONFIG_FILE.path == GLOBAL_CONFIG_FILE.path
|
||||
|
||||
def test_falls_back_to_global_config_when_local_missing(
|
||||
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""Test that global config is returned when local config doesn't exist."""
|
||||
monkeypatch.chdir(tmp_path)
|
||||
# Ensure no local config exists
|
||||
assert not (tmp_path / ".vibe" / "config.toml").exists()
|
||||
|
|
@ -35,7 +48,6 @@ class TestResolveConfigFile:
|
|||
def test_respects_vibe_home_env_var(
|
||||
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""Test that VIBE_HOME environment variable affects VIBE_HOME.path."""
|
||||
assert VIBE_HOME.path != tmp_path
|
||||
monkeypatch.setenv("VIBE_HOME", str(tmp_path))
|
||||
assert VIBE_HOME.path == tmp_path
|
||||
|
|
|
|||
205
tests/core/test_trusted_folders.py
Normal file
205
tests/core/test_trusted_folders.py
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import tomllib
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import tomli_w
|
||||
|
||||
from vibe.core.paths.global_paths import TRUSTED_FOLDERS_FILE
|
||||
from vibe.core.trusted_folders import TrustedFoldersManager
|
||||
|
||||
|
||||
class TestTrustedFoldersManager:
|
||||
def test_initializes_with_empty_lists_when_file_does_not_exist(
|
||||
self, tmp_path: Path
|
||||
) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
assert not trusted_file.is_file()
|
||||
|
||||
manager = TrustedFoldersManager()
|
||||
assert manager.is_trusted(tmp_path) is None
|
||||
assert trusted_file.is_file()
|
||||
|
||||
def test_loads_existing_file(self, tmp_path: Path) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
|
||||
data = {"trusted": [str(tmp_path.resolve())], "untrusted": []}
|
||||
with trusted_file.open("wb") as f:
|
||||
tomli_w.dump(data, f)
|
||||
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
|
||||
def test_handles_corrupted_file(self, tmp_path: Path) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
trusted_file.write_text("invalid toml content {[", encoding="utf-8")
|
||||
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
assert manager.is_trusted(tmp_path) is None
|
||||
assert trusted_file.is_file()
|
||||
|
||||
def test_normalizes_paths_to_absolute(
|
||||
self, tmp_working_directory, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
manager.add_trusted(Path("."))
|
||||
assert manager.is_trusted(tmp_working_directory) is True
|
||||
assert manager.is_trusted(Path(".")) is True
|
||||
|
||||
def test_expands_user_home_in_paths(
|
||||
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
manager.add_trusted(Path("~/test"))
|
||||
assert manager.is_trusted(tmp_path / "test") is True
|
||||
|
||||
def test_is_trusted_returns_true_for_trusted_path(self, tmp_path: Path) -> None:
|
||||
manager = TrustedFoldersManager()
|
||||
manager.add_trusted(tmp_path)
|
||||
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
|
||||
def test_is_trusted_returns_false_for_untrusted_path(self, tmp_path: Path) -> None:
|
||||
manager = TrustedFoldersManager()
|
||||
manager.add_untrusted(tmp_path)
|
||||
|
||||
assert manager.is_trusted(tmp_path) is False
|
||||
|
||||
def test_is_trusted_returns_none_for_unknown_path(self, tmp_path: Path) -> None:
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
assert manager.is_trusted(tmp_path) is None
|
||||
|
||||
def test_add_trusted_adds_path_to_trusted_list(self, tmp_path: Path) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
manager = TrustedFoldersManager()
|
||||
manager.add_trusted(tmp_path)
|
||||
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
with trusted_file.open("rb") as f:
|
||||
data = tomllib.load(f)
|
||||
assert str(tmp_path.resolve()) in data["trusted"]
|
||||
|
||||
def test_add_trusted_removes_path_from_untrusted(self, tmp_path: Path) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
manager.add_untrusted(tmp_path)
|
||||
assert manager.is_trusted(tmp_path) is False
|
||||
|
||||
manager.add_trusted(tmp_path)
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
|
||||
with trusted_file.open("rb") as f:
|
||||
data = tomllib.load(f)
|
||||
assert str(tmp_path.resolve()) not in data["untrusted"]
|
||||
assert str(tmp_path.resolve()) in data["trusted"]
|
||||
|
||||
def test_add_trusted_idempotent(self, tmp_path: Path) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
|
||||
manager = TrustedFoldersManager()
|
||||
manager.add_trusted(tmp_path)
|
||||
manager.add_trusted(tmp_path)
|
||||
manager.add_trusted(tmp_path)
|
||||
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
with trusted_file.open("rb") as f:
|
||||
data = tomllib.load(f)
|
||||
assert data["trusted"].count(str(tmp_path.resolve())) == 1
|
||||
|
||||
def test_add_untrusted_adds_path_to_untrusted_list(self, tmp_path: Path) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
manager = TrustedFoldersManager()
|
||||
manager.add_untrusted(tmp_path)
|
||||
|
||||
assert manager.is_trusted(tmp_path) is False
|
||||
with trusted_file.open("rb") as f:
|
||||
data = tomllib.load(f)
|
||||
assert str(tmp_path.resolve()) in data["untrusted"]
|
||||
|
||||
def test_add_untrusted_removes_path_from_trusted(self, tmp_path: Path) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
manager.add_trusted(tmp_path)
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
|
||||
manager.add_untrusted(tmp_path)
|
||||
assert manager.is_trusted(tmp_path) is False
|
||||
|
||||
with trusted_file.open("rb") as f:
|
||||
data = tomllib.load(f)
|
||||
assert str(tmp_path.resolve()) not in data["trusted"]
|
||||
assert str(tmp_path.resolve()) in data["untrusted"]
|
||||
|
||||
def test_add_untrusted_idempotent(self, tmp_path: Path) -> None:
|
||||
trusted_file = TRUSTED_FOLDERS_FILE.path
|
||||
|
||||
manager = TrustedFoldersManager()
|
||||
manager.add_untrusted(tmp_path)
|
||||
manager.add_untrusted(tmp_path)
|
||||
manager.add_untrusted(tmp_path)
|
||||
|
||||
assert manager.is_trusted(tmp_path) is False
|
||||
with trusted_file.open("rb") as f:
|
||||
data = tomllib.load(f)
|
||||
assert data["untrusted"].count(str(tmp_path.resolve())) == 1
|
||||
|
||||
def test_persistence_across_instances(self, tmp_path: Path) -> None:
|
||||
manager1 = TrustedFoldersManager()
|
||||
manager1.add_trusted(tmp_path)
|
||||
|
||||
manager2 = TrustedFoldersManager()
|
||||
assert manager2.is_trusted(tmp_path) is True
|
||||
|
||||
def test_handles_multiple_paths(self, tmp_path: Path) -> None:
|
||||
trusted1 = tmp_path / "trusted1"
|
||||
trusted2 = tmp_path / "trusted2"
|
||||
untrusted1 = tmp_path / "untrusted1"
|
||||
untrusted2 = tmp_path / "untrusted2"
|
||||
for p in [trusted1, trusted2, untrusted1, untrusted2]:
|
||||
p.mkdir()
|
||||
|
||||
manager = TrustedFoldersManager()
|
||||
manager.add_trusted(trusted1)
|
||||
manager.add_trusted(trusted2)
|
||||
manager.add_untrusted(untrusted1)
|
||||
manager.add_untrusted(untrusted2)
|
||||
|
||||
assert manager.is_trusted(trusted1) is True
|
||||
assert manager.is_trusted(trusted2) is True
|
||||
assert manager.is_trusted(untrusted1) is False
|
||||
assert manager.is_trusted(untrusted2) is False
|
||||
|
||||
def test_handles_switching_between_trusted_and_untrusted(
|
||||
self, tmp_path: Path
|
||||
) -> None:
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
manager.add_trusted(tmp_path)
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
|
||||
manager.add_untrusted(tmp_path)
|
||||
assert manager.is_trusted(tmp_path) is False
|
||||
|
||||
manager.add_trusted(tmp_path)
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
|
||||
def test_handles_missing_file_during_save(self, tmp_path: Path) -> None:
|
||||
manager = TrustedFoldersManager()
|
||||
|
||||
def mock_open(*args, **kwargs):
|
||||
raise OSError("Permission denied")
|
||||
|
||||
with patch("pathlib.Path.open", side_effect=mock_open):
|
||||
manager.add_trusted(tmp_path)
|
||||
|
||||
assert manager.is_trusted(tmp_path) is True
|
||||
Loading…
Add table
Add a link
Reference in a new issue