|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from click.testing import CliRunner |
| 7 | +from tgit.cli import app |
| 8 | +from tgit.utils import ( |
| 9 | + load_global_settings, |
| 10 | + load_workspace_settings, |
| 11 | + set_global_settings, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class TestUtilsCoverage: |
| 16 | + """Additional tests to increase coverage for utils and cli.""" |
| 17 | + |
| 18 | + def test_load_global_settings_empty_json(self, tmp_path): |
| 19 | + """Test load_global_settings with empty JSON (returns None).""" |
| 20 | + settings_path = tmp_path / ".tgit" / "settings.json" |
| 21 | + settings_path.parent.mkdir(parents=True) |
| 22 | + settings_path.write_text("null") |
| 23 | + |
| 24 | + with patch("tgit.utils.Path.home", return_value=tmp_path): |
| 25 | + settings = load_global_settings() |
| 26 | + assert settings == {} |
| 27 | + |
| 28 | + def test_load_workspace_settings_empty_json(self, tmp_path): |
| 29 | + """Test load_workspace_settings with empty JSON (returns None).""" |
| 30 | + settings_path = tmp_path / ".tgit" / "settings.json" |
| 31 | + settings_path.parent.mkdir(parents=True) |
| 32 | + settings_path.write_text("null") |
| 33 | + |
| 34 | + with patch("tgit.utils.Path.cwd", return_value=tmp_path): |
| 35 | + settings = load_workspace_settings() |
| 36 | + assert settings == {} |
| 37 | + |
| 38 | + def test_set_global_settings(self, tmp_path): |
| 39 | + """Test set_global_settings.""" |
| 40 | + with patch("tgit.utils.Path.home", return_value=tmp_path): |
| 41 | + set_global_settings("test_key", "test_value") |
| 42 | + |
| 43 | + settings_path = tmp_path / ".tgit" / "settings.json" |
| 44 | + assert settings_path.exists() |
| 45 | + content = json.loads(settings_path.read_text()) |
| 46 | + assert content["test_key"] == "test_value" |
| 47 | + |
| 48 | + # Test updating existing settings |
| 49 | + set_global_settings("test_key_2", "test_value_2") |
| 50 | + content = json.loads(settings_path.read_text()) |
| 51 | + assert content["test_key"] == "test_value" |
| 52 | + assert content["test_key_2"] == "test_value_2" |
| 53 | + |
| 54 | + def test_set_global_settings_with_null_file(self, tmp_path): |
| 55 | + """Test set_global_settings when file exists but is null.""" |
| 56 | + settings_path = tmp_path / ".tgit" / "settings.json" |
| 57 | + settings_path.parent.mkdir(parents=True) |
| 58 | + settings_path.write_text("null") |
| 59 | + |
| 60 | + with patch("tgit.utils.Path.home", return_value=tmp_path): |
| 61 | + set_global_settings("test_key", "test_value") |
| 62 | + |
| 63 | + content = json.loads(settings_path.read_text()) |
| 64 | + assert content["test_key"] == "test_value" |
| 65 | + |
| 66 | + def test_load_global_settings_missing_file(self, tmp_path): |
| 67 | + """Test load_global_settings when file is missing.""" |
| 68 | + with patch("tgit.utils.Path.home", return_value=tmp_path): |
| 69 | + settings = load_global_settings() |
| 70 | + assert settings == {} |
| 71 | + |
| 72 | + def test_load_workspace_settings_missing_file(self, tmp_path): |
| 73 | + """Test load_workspace_settings when file is missing.""" |
| 74 | + with patch("tgit.utils.Path.cwd", return_value=tmp_path): |
| 75 | + settings = load_workspace_settings() |
| 76 | + assert settings == {} |
| 77 | + |
| 78 | + @patch("tgit.cli.threading.Thread") |
| 79 | + def test_cli_app_import_openai(self, mock_thread): |
| 80 | + """Test cli app triggers openai import.""" |
| 81 | + # Mock Thread to run target immediately |
| 82 | + def run_target(target=None, **kwargs): |
| 83 | + target() |
| 84 | + return Mock() |
| 85 | + |
| 86 | + mock_thread.side_effect = run_target |
| 87 | + |
| 88 | + runner = CliRunner() |
| 89 | + # Invoke with a subcommand to ensure group function runs |
| 90 | + # We use a non-existent command to trigger group execution before error? |
| 91 | + # Or use 'version' command if available. |
| 92 | + # Let's use 'settings' command which is added. |
| 93 | + result = runner.invoke(app, ["settings", "--help"]) |
| 94 | + |
| 95 | + assert result.exit_code == 0 |
| 96 | + mock_thread.assert_called() |
| 97 | + |
| 98 | + @patch("tgit.cli.threading.Thread") |
| 99 | + def test_cli_app_import_openai_exception(self, mock_thread): |
| 100 | + """Test cli app handles openai import exception.""" |
| 101 | + def run_target(target=None, **kwargs): |
| 102 | + # Mock import to raise exception |
| 103 | + with patch("builtins.__import__", side_effect=ImportError("fail")): |
| 104 | + target() |
| 105 | + return Mock() |
| 106 | + |
| 107 | + mock_thread.side_effect = run_target |
| 108 | + |
| 109 | + runner = CliRunner() |
| 110 | + result = runner.invoke(app, ["settings", "--help"]) |
| 111 | + |
| 112 | + assert result.exit_code == 0 |
| 113 | + mock_thread.assert_called() |
| 114 | + |
| 115 | + def test_version_callback(self): |
| 116 | + """Test version callback.""" |
| 117 | + runner = CliRunner() |
| 118 | + result = runner.invoke(app, ["--version"]) |
| 119 | + assert result.exit_code == 0 |
| 120 | + assert "TGIT - ver." in result.output |
0 commit comments