Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update configuration messages #2

Merged
merged 11 commits into from
Dec 31, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Project
.aicmtrc
tmp

# Python
__pycache__/
Expand Down
9 changes: 6 additions & 3 deletions aicmt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ def _get_config_paths() -> Optional[str]:
1. Local configuration (./.aicmtrc)
2. XDG configuration ($XDG_CONFIG_HOME/aicmt/.aicmtrc or ~/.config/aicmt/.aicmtrc)
"""

xdg_config_path = _get_xdg_config_home() / "aicmt"
config_path = [
Path.cwd() / ".aicmtrc", # Local config
_get_xdg_config_home() / "aicmt" / ".aicmtrc", # XDG config
xdg_config_path / ".aicmtrc", # XDG config
Path.home() / ".aicmtrc", # Legacy global config
]

xdg_config_path.mkdir(parents=True, exist_ok=True)

for path in config_path:
if path.is_file():
return path
Expand Down Expand Up @@ -184,8 +186,9 @@ def _load_config_file() -> Dict[str, Any]:
"model = gpt-4o-mini\n"
"base_url = https://api.openai.com/v1\n"
)
CLIInterface.display_info(f"Auto created configuration file in {xdg_config_path}")

CLIInterface.display_warning(f"Auto created configuration file in {xdg_config_path}\n" "Please check and update your configuration file.")
CLIInterface.display_info("Please check and update your configuration file.")
return _parse_config_file(xdg_config_path)

# if not global_config_path.exists() and not local_config_path.exists() and not xdg_config_path.exists():
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Issues = "https://github.com/versun/aicmt/issues"
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
addopts = "--cov=aicmt --cov-report=term-missing --cov-report=html --cov-fail-under=80"
addopts = "--cov=aicmt --cov-report=term-missing --cov-report=html --cov-fail-under=80 -p no:cacheprovider --basetemp=tmp"

[tool.coverage.run]
source = ["aicmt"]
Expand Down
28 changes: 13 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
from unittest.mock import patch, MagicMock
from git import Repo
import json
from aicmt.cli import AiCommit

root_dir = Path(__file__).parent.parent

sys.path.insert(0, str(root_dir))


@pytest.fixture(params=["Linux", "Darwin", "Windows"], autouse=True)
def platform_system(request, monkeypatch):
monkeypatch.setattr(platform, "system", lambda: request.param)

@pytest.fixture(autouse=True)
def mock_argv():
"""Mock command line arguments"""
with patch.object(sys, "argv", ["aicmt"]):
yield

# @pytest.fixture(autouse=True)
# def mock_argv():
# """Mock command line arguments"""
# with patch.object(sys, "argv", ["aicmt"]):
# yield


@pytest.fixture
Expand All @@ -30,11 +31,14 @@ def mock_config():
with patch("aicmt.ai_analyzer.load_config", return_value=config), patch("aicmt.config.load_config", return_value=config):
yield config


@pytest.fixture
def mock_home_dir(tmp_path):
"""Mock home directory"""
with patch("pathlib.Path.home", return_value=tmp_path):
yield tmp_path
home = tmp_path / "home"
with patch("pathlib.Path.home", return_value=home):
yield home


@pytest.fixture
def mock_repo(tmp_path):
Expand All @@ -49,12 +53,6 @@ def mock_repo(tmp_path):
return str(repo_path)


@pytest.fixture
def ai_commit(mock_repo, mock_config):
"""Create an AiCommit instance with mocked dependencies"""
return AiCommit(mock_repo)


@pytest.fixture
def mock_openai():
"""Mock OpenAI API responses"""
Expand All @@ -74,4 +72,4 @@ def mock_openai():

with patch("openai.OpenAI", return_value=mock_client) as mock:
mock.return_value = mock_client
yield mock_client
yield mock_client
2 changes: 2 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def test_validate_config_error():
with pytest.raises(ValueError, match="Invalid API URL format"):
validate_config({"api_key": "test_key", "model": "gpt-4", "base_url": "not-a-url", "analysis_prompt": "test prompt"})


def test_load_config_file_local_only(tmp_path, monkeypatch):
work_dir = tmp_path / "work"
work_dir.mkdir()
Expand Down Expand Up @@ -224,6 +225,7 @@ def test_load_config_file_no_configs(tmp_path, monkeypatch):

assert result is not None


def test_load_cli_config(monkeypatch):
"""Test loading configuration from command line arguments."""
from aicmt.config import _load_cli_config
Expand Down
260 changes: 160 additions & 100 deletions tests/test_system.py
Original file line number Diff line number Diff line change
@@ -1,100 +1,160 @@
import os
import sys
import pytest
from unittest.mock import patch
import git
from aicmt.cli import AiCommit

def test_help_command(capsys, mock_repo):
"""Test help command output"""
with patch.object(sys, "argv", ["aicmt", "-h"]):
with pytest.raises(SystemExit) as e:
AiCommit(mock_repo)
assert e.value.code == 0

captured = capsys.readouterr()
assert "AICMT (AI Commit)" in captured.out
assert "usage: aicmt [-h] [-v] [-n N]" in captured.out
assert "-h, --help" in captured.out
assert "show this help message and exit" in captured.out


def test_version_command(capsys, mock_repo):
"""Test version command output"""
with patch.object(sys, "argv", ["aicmt", "-v"]):
with pytest.raises(SystemExit) as e:
AiCommit(mock_repo)
assert e.value.code == 0
from aicmt import __version__

captured = capsys.readouterr()
assert f"{__version__.VERSION}" in captured.out


def test_no_args_without_repo(tmp_path):
"""Test no arguments without a git repository"""
os.chdir(tmp_path)
with patch.object(sys, "argv", ["aicmt"]):
with pytest.raises(git.exc.InvalidGitRepositoryError) as exc_info:
AiCommit(tmp_path)
assert str(exc_info.value) == "Not a valid Git repository"


def test_no_config_file(capsys, mock_repo):
"""Test no configuration file"""
with patch.object(sys, "argv", ["aicmt"]):
with patch("aicmt.config._get_config_paths", return_value=None):
AiCommit(mock_repo)
captured = capsys.readouterr()
assert "Please check and update your configuration file." in captured.out

def test_auto_create_config(capsys, mock_repo, mock_home_dir):
"""Test no configuration file"""
config_file = mock_home_dir / ".config/aicmt/.aicmtrc"
assert not config_file.exists()
with patch.object(sys, "argv", ["aicmt"]):
AiCommit(mock_repo)
captured = capsys.readouterr()
assert "Please check and update your configuration file." in captured.out
assert "Auto created configuration file in" in captured.out
assert config_file.exists()


def test_no_changes(ai_commit, mock_repo, mock_openai, mock_config):
"""Test no changes detected"""
pass


def test_basic_workflow(ai_commit, mock_repo, mock_openai, mock_config):
"""Test basic workflow with unstaged changes"""
pass


def test_staged_changes_workflow(ai_commit, mock_repo, mock_openai, mock_config):
"""Test workflow with staged changes"""
pass


def test_error_handling(ai_commit, mock_repo, mock_config):
"""Test error handling when AI analysis fails"""
pass


def test_push_workflow(ai_commit, mock_repo, mock_openai, mock_config):
"""Test workflow with push operation"""
pass


def test_modified_file_workflow(ai_commit, mock_repo, mock_openai, mock_config):
"""Test workflow with modified files"""
pass


def test_deleted_file_workflow(ai_commit, mock_repo, mock_openai, mock_config):
"""Test workflow with deleted files"""
pass


def test_renamed_file_workflow(ai_commit, mock_repo, mock_openai, mock_config):
"""Test workflow with renamed files"""
pass
# import os
# import sys
# import pytest
# from unittest.mock import patch
# import git
# from aicmt.cli import AiCommit
# from pathlib import Path


# def test_help_command(capsys, mock_repo, mock_home_dir):
# """Test help command output"""
# config_file = mock_home_dir / ".config/aicmt/.aicmtrc"
# assert not config_file.exists()
# with patch.object(sys, "argv", ["aicmt", "-h"]):
# with pytest.raises(SystemExit) as e:
# AiCommit(mock_repo).run()
# assert e.value.code == 0

# captured = capsys.readouterr()
# assert "AICMT (AI Commit)" in captured.out
# assert "usage: aicmt [-h] [-v] [-n N]" in captured.out
# assert "-h, --help" in captured.out
# assert "show this help message and exit" in captured.out


# def test_version_command(capsys, mock_repo, mock_home_dir):
# """Test version command output"""
# config_file = mock_home_dir / ".config/aicmt/.aicmtrc"
# assert not config_file.exists()
# with patch.object(sys, "argv", ["aicmt", "-v"]):
# with pytest.raises(SystemExit) as e:
# AiCommit(mock_repo).run()
# assert e.value.code == 0
# from aicmt import __version__

# captured = capsys.readouterr()
# assert f"{__version__.VERSION}" in captured.out


# def test_no_args_without_repo(tmp_path, mock_home_dir):
# """Test no arguments without a git repository"""
# config_file = mock_home_dir / ".config/aicmt/.aicmtrc"
# assert not config_file.exists()
# os.chdir(tmp_path)
# with patch.object(sys, "argv", ["aicmt"]):
# with pytest.raises(git.exc.InvalidGitRepositoryError) as exc_info:
# AiCommit(tmp_path).run()
# assert str(exc_info.value) == "Not a valid Git repository"


# def test_no_config_file(capsys, mock_repo, mock_home_dir):
# """Test no configuration file"""
# config_file = mock_home_dir / ".config/aicmt/.aicmtrc"
# assert not config_file.exists()
# with patch.object(sys, "argv", ["aicmt"]):
# with pytest.raises(SystemExit) as e:
# AiCommit(mock_repo).run()
# assert e.value.code == 0
# captured = capsys.readouterr()
# assert "Please check and update your configuration file." in captured.out
# assert "Auto created configuration file in" in captured.out


# def test_auto_create_config(capsys, mock_repo, mock_home_dir):
# """Test no configuration file"""
# config_file = mock_home_dir / ".config/aicmt/.aicmtrc"
# assert not config_file.exists()
# with patch.object(sys, "argv", ["aicmt"]):
# with pytest.raises(SystemExit) as e:
# AiCommit(mock_repo).run()
# assert e.value.code == 0

# assert config_file.exists()
# assert "[openai]" in config_file.read_text()
# assert "api_key = your-api-key-here" in config_file.read_text()
# assert "model = gpt-4o-mini" in config_file.read_text()
# assert "base_url = https://api.openai.com/v1" in config_file.read_text()


# def test_read_global_config(capsys, mock_repo, mock_home_dir):
# """Test reading global configuration file"""
# config_file = mock_home_dir / ".config/aicmt/.aicmtrc"
# assert not config_file.exists()

# config_dir = mock_home_dir / ".config/aicmt"
# config_content = """
# [openai]
# api_key = global-key
# model = global-model
# base_url = https://api.openai.com/v1
# """
# config_dir.mkdir(parents=True, exist_ok=True)
# (config_dir / ".aicmtrc").write_text(config_content)
# assert (config_dir / ".aicmtrc").exists()
# (Path(mock_repo) / "test.py").write_text("test content")
# with patch.object(sys, "argv", ["aicmt"]):
# with pytest.raises(SystemExit) as e:
# AiCommit(mock_repo).run()
# assert e.value.code == 1

# captured = capsys.readouterr()
# assert "https://api.openai.com/v1" in captured.out
# assert "global-model" in captured.out


# def test_no_changes(capsys, mock_repo, mock_home_dir):
# """Test no changes detected"""
# config_dir = mock_home_dir / ".config/aicmt"
# config_content = """
# [openai]
# api_key = global-key
# model = global-model
# base_url = https://api.openai.com/v1
# """
# config_dir.mkdir(parents=True, exist_ok=True)
# (config_dir / ".aicmtrc").write_text(config_content)
# assert (config_dir / ".aicmtrc").exists()

# with patch.object(sys, "argv", ["aicmt"]):
# with pytest.raises(SystemExit) as e:
# AiCommit(mock_repo).run()
# assert e.value.code == 0

# captured = capsys.readouterr()
# assert "No changes found" in captured.out


# def test_basic_workflow(mock_repo, mock_openai, mock_config):
# """Test basic workflow with unstaged changes"""
# pass


# def test_staged_changes_workflow(mock_repo, mock_openai, mock_config):
# """Test workflow with staged changes"""
# pass


# def test_error_handling(mock_repo, mock_config):
# """Test error handling when AI analysis fails"""
# pass


# def test_push_workflow(mock_repo, mock_openai, mock_config):
# """Test workflow with push operation"""
# pass


# def test_modified_file_workflow(mock_repo, mock_openai, mock_config):
# """Test workflow with modified files"""
# pass


# def test_deleted_file_workflow(mock_repo, mock_openai, mock_config):
# """Test workflow with deleted files"""
# pass


# def test_renamed_file_workflow(mock_repo, mock_openai, mock_config):
# """Test workflow with renamed files"""
# pass
Loading