git-commit-msg-ai 1.3.0__tar.gz → 1.4.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/PKG-INFO +6 -6
  2. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/README.md +4 -4
  3. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai/editor.py +10 -1
  4. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai/git_ops.py +10 -2
  5. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai.egg-info/PKG-INFO +6 -6
  6. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/pyproject.toml +4 -4
  7. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/tests/test_ai_client.py +17 -11
  8. git_commit_msg_ai-1.4.1/tests/test_cli.py +149 -0
  9. git_commit_msg_ai-1.4.1/tests/test_editor.py +137 -0
  10. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/tests/test_exceptions.py +6 -6
  11. git_commit_msg_ai-1.4.1/tests/test_git_ops.py +81 -0
  12. git_commit_msg_ai-1.3.0/tests/test_cli.py +0 -136
  13. git_commit_msg_ai-1.3.0/tests/test_editor.py +0 -96
  14. git_commit_msg_ai-1.3.0/tests/test_git_ops.py +0 -70
  15. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai/__init__.py +0 -0
  16. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai/ai_client.py +0 -0
  17. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai/cli.py +0 -0
  18. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai/exceptions.py +0 -0
  19. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai.egg-info/SOURCES.txt +0 -0
  20. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai.egg-info/dependency_links.txt +0 -0
  21. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai.egg-info/entry_points.txt +0 -0
  22. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai.egg-info/requires.txt +0 -0
  23. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/git_commit_msg_ai.egg-info/top_level.txt +0 -0
  24. {git_commit_msg_ai-1.3.0 → git_commit_msg_ai-1.4.1}/setup.cfg +0 -0
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: git-commit-msg-ai
3
- Version: 1.3.0
3
+ Version: 1.4.1
4
4
  Summary: AI-powered git commit message generator following Conventional Commits
5
5
  License-Expression: MIT
6
- Requires-Python: >=3.9
6
+ Requires-Python: >=3.10
7
7
  Description-Content-Type: text/markdown
8
8
  Requires-Dist: anthropic
9
9
  Provides-Extra: dev
@@ -18,7 +18,7 @@ AI-powered git commit message generator that follows the [Conventional Commits](
18
18
 
19
19
  ## Prerequisites
20
20
 
21
- - Python 3.9+
21
+ - Python 3.10+
22
22
  - An Anthropic API key set as an environment variable:
23
23
 
24
24
  ```sh
@@ -53,9 +53,9 @@ The tool will:
53
53
  [a]ccept / [e]dit / [r]eject:
54
54
  ```
55
55
 
56
- - **a** commits immediately with the generated message
57
- - **e** opens the message in your `$EDITOR`, lets you modify it, then commits
58
- - **r** exits without committing
56
+ - **a** - commits immediately with the generated message
57
+ - **e** - opens the message in your `$EDITOR` (defaults to `notepad` on Windows, `vi` on Linux/macOS), lets you modify it, then commits
58
+ - **r** - exits without committing
59
59
 
60
60
  ## Commit message format
61
61
 
@@ -4,7 +4,7 @@ AI-powered git commit message generator that follows the [Conventional Commits](
4
4
 
5
5
  ## Prerequisites
6
6
 
7
- - Python 3.9+
7
+ - Python 3.10+
8
8
  - An Anthropic API key set as an environment variable:
9
9
 
10
10
  ```sh
@@ -39,9 +39,9 @@ The tool will:
39
39
  [a]ccept / [e]dit / [r]eject:
40
40
  ```
41
41
 
42
- - **a** commits immediately with the generated message
43
- - **e** opens the message in your `$EDITOR`, lets you modify it, then commits
44
- - **r** exits without committing
42
+ - **a** - commits immediately with the generated message
43
+ - **e** - opens the message in your `$EDITOR` (defaults to `notepad` on Windows, `vi` on Linux/macOS), lets you modify it, then commits
44
+ - **r** - exits without committing
45
45
 
46
46
  ## Commit message format
47
47
 
@@ -1,10 +1,18 @@
1
1
  import os
2
+ import platform
2
3
  import subprocess
3
4
  import tempfile
4
5
 
5
6
  from git_commit_msg_ai.exceptions import EditorError
6
7
 
7
8
 
9
+ def get_default_editor() -> str:
10
+ current_platform = platform.system()
11
+ is_windows = current_platform == 'Windows'
12
+
13
+ return 'notepad' if is_windows else 'vi'
14
+
15
+
8
16
  def open_in_editor(initial_text: str) -> str:
9
17
  try:
10
18
  with tempfile.NamedTemporaryFile(suffix='.txt', mode='w', delete=False) as temp_file:
@@ -13,7 +21,8 @@ def open_in_editor(initial_text: str) -> str:
13
21
  except OSError:
14
22
  raise EditorError('Could not create a temporary file.')
15
23
 
16
- editor_command = os.environ.get('EDITOR', 'notepad')
24
+ platform_default_editor = get_default_editor()
25
+ editor_command = os.environ.get('EDITOR', platform_default_editor)
17
26
 
18
27
  try:
19
28
  try:
@@ -1,11 +1,19 @@
1
1
  import subprocess
2
+ from typing import Final
2
3
 
3
4
  from git_commit_msg_ai.exceptions import GitError
4
5
 
6
+ GIT_COMMAND: Final[str] = 'git'
7
+ DIFF_SUBCOMMAND: Final[str] = 'diff'
8
+ CACHED_FLAG: Final[str] = '--cached'
9
+ COMMIT_SUBCOMMAND: Final[str] = 'commit'
10
+ MESSAGE_FLAG: Final[str] = '-m'
11
+ UTF8_ENCODING: Final[str] = 'utf-8'
12
+
5
13
 
6
14
  def get_staged_diff() -> str:
7
15
  try:
8
- staged_diff = subprocess.check_output(['git', 'diff', '--cached']).decode()
16
+ staged_diff = subprocess.check_output([GIT_COMMAND, DIFF_SUBCOMMAND, CACHED_FLAG]).decode(UTF8_ENCODING)
9
17
  except FileNotFoundError:
10
18
  raise GitError('git is not installed or not on PATH.')
11
19
  except subprocess.CalledProcessError:
@@ -21,7 +29,7 @@ def get_staged_diff() -> str:
21
29
 
22
30
  def commit(message: str) -> None:
23
31
  try:
24
- subprocess.run(['git', 'commit', '-m', message], check=True)
32
+ subprocess.run([GIT_COMMAND, COMMIT_SUBCOMMAND, MESSAGE_FLAG, message], check=True)
25
33
  except FileNotFoundError:
26
34
  raise GitError('git is not installed or not on PATH.')
27
35
  except subprocess.CalledProcessError:
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: git-commit-msg-ai
3
- Version: 1.3.0
3
+ Version: 1.4.1
4
4
  Summary: AI-powered git commit message generator following Conventional Commits
5
5
  License-Expression: MIT
6
- Requires-Python: >=3.9
6
+ Requires-Python: >=3.10
7
7
  Description-Content-Type: text/markdown
8
8
  Requires-Dist: anthropic
9
9
  Provides-Extra: dev
@@ -18,7 +18,7 @@ AI-powered git commit message generator that follows the [Conventional Commits](
18
18
 
19
19
  ## Prerequisites
20
20
 
21
- - Python 3.9+
21
+ - Python 3.10+
22
22
  - An Anthropic API key set as an environment variable:
23
23
 
24
24
  ```sh
@@ -53,9 +53,9 @@ The tool will:
53
53
  [a]ccept / [e]dit / [r]eject:
54
54
  ```
55
55
 
56
- - **a** commits immediately with the generated message
57
- - **e** opens the message in your `$EDITOR`, lets you modify it, then commits
58
- - **r** exits without committing
56
+ - **a** - commits immediately with the generated message
57
+ - **e** - opens the message in your `$EDITOR` (defaults to `notepad` on Windows, `vi` on Linux/macOS), lets you modify it, then commits
58
+ - **r** - exits without committing
59
59
 
60
60
  ## Commit message format
61
61
 
@@ -4,11 +4,11 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "git-commit-msg-ai"
7
- version = "1.3.0"
7
+ version = "1.4.1"
8
8
  description = "AI-powered git commit message generator following Conventional Commits"
9
9
  readme = "README.md"
10
10
  license = "MIT"
11
- requires-python = ">=3.9"
11
+ requires-python = ">=3.10"
12
12
  dependencies = ["anthropic"]
13
13
 
14
14
  [project.scripts]
@@ -18,14 +18,14 @@ git-commit-msg-ai = "git_commit_msg_ai.cli:main"
18
18
  dev = ["mypy", "ruff", "pytest", "pytest-cov"]
19
19
 
20
20
  [tool.ruff]
21
- target-version = "py39"
21
+ target-version = "py310"
22
22
  line-length = 320
23
23
 
24
24
  [tool.ruff.lint]
25
25
  select = ["E", "F", "I"]
26
26
 
27
27
  [tool.mypy]
28
- python_version = "3.9"
28
+ python_version = "3.10"
29
29
  strict = true
30
30
 
31
31
  [tool.pytest.ini_options]
@@ -1,3 +1,4 @@
1
+ from contextlib import ExitStack
1
2
  from unittest.mock import MagicMock, patch
2
3
 
3
4
  import anthropic
@@ -17,7 +18,7 @@ def _make_api_response(text: str) -> MagicMock:
17
18
  return mock_response
18
19
 
19
20
 
20
- def _make_status_error(exception_class: type, status_code: int) -> anthropic.APIStatusError:
21
+ def _make_status_error(exception_class: type[anthropic.APIStatusError], status_code: int) -> anthropic.APIStatusError:
21
22
  mock_response = MagicMock()
22
23
  mock_response.status_code = status_code
23
24
 
@@ -25,8 +26,9 @@ def _make_status_error(exception_class: type, status_code: int) -> anthropic.API
25
26
 
26
27
 
27
28
  class TestGenerateCommitMessage:
28
- def test_returns_stripped_commit_message(self):
29
- with patch('git_commit_msg_ai.ai_client.anthropic.Anthropic') as mock_anthropic_class:
29
+ def test_returns_stripped_commit_message(self) -> None:
30
+ with ExitStack() as stack:
31
+ mock_anthropic_class = stack.enter_context(patch('git_commit_msg_ai.ai_client.anthropic.Anthropic'))
30
32
  mock_client = MagicMock()
31
33
  mock_client.messages.create.return_value = _make_api_response(' feat: add feature ')
32
34
  mock_anthropic_class.return_value = mock_client
@@ -35,8 +37,9 @@ class TestGenerateCommitMessage:
35
37
 
36
38
  assert result == 'feat: add feature'
37
39
 
38
- def test_raises_ai_error_on_authentication_error(self):
39
- with patch('git_commit_msg_ai.ai_client.anthropic.Anthropic') as mock_anthropic_class:
40
+ def test_raises_ai_error_on_authentication_error(self) -> None:
41
+ with ExitStack() as stack:
42
+ mock_anthropic_class = stack.enter_context(patch('git_commit_msg_ai.ai_client.anthropic.Anthropic'))
40
43
  mock_client = MagicMock()
41
44
  mock_client.messages.create.side_effect = _make_status_error(anthropic.AuthenticationError, 401)
42
45
  mock_anthropic_class.return_value = mock_client
@@ -44,8 +47,9 @@ class TestGenerateCommitMessage:
44
47
  with pytest.raises(AIError, match='ANTHROPIC_API_KEY'):
45
48
  generate_commit_message('diff content')
46
49
 
47
- def test_raises_ai_error_on_rate_limit_error(self):
48
- with patch('git_commit_msg_ai.ai_client.anthropic.Anthropic') as mock_anthropic_class:
50
+ def test_raises_ai_error_on_rate_limit_error(self) -> None:
51
+ with ExitStack() as stack:
52
+ mock_anthropic_class = stack.enter_context(patch('git_commit_msg_ai.ai_client.anthropic.Anthropic'))
49
53
  mock_client = MagicMock()
50
54
  mock_client.messages.create.side_effect = _make_status_error(anthropic.RateLimitError, 429)
51
55
  mock_anthropic_class.return_value = mock_client
@@ -53,10 +57,11 @@ class TestGenerateCommitMessage:
53
57
  with pytest.raises(AIError, match='rate limit'):
54
58
  generate_commit_message('diff content')
55
59
 
56
- def test_raises_ai_error_on_api_connection_error(self):
60
+ def test_raises_ai_error_on_api_connection_error(self) -> None:
57
61
  mock_request = MagicMock()
58
62
 
59
- with patch('git_commit_msg_ai.ai_client.anthropic.Anthropic') as mock_anthropic_class:
63
+ with ExitStack() as stack:
64
+ mock_anthropic_class = stack.enter_context(patch('git_commit_msg_ai.ai_client.anthropic.Anthropic'))
60
65
  mock_client = MagicMock()
61
66
  mock_client.messages.create.side_effect = anthropic.APIConnectionError(request=mock_request)
62
67
  mock_anthropic_class.return_value = mock_client
@@ -64,8 +69,9 @@ class TestGenerateCommitMessage:
64
69
  with pytest.raises(AIError, match='network'):
65
70
  generate_commit_message('diff content')
66
71
 
67
- def test_raises_ai_error_on_api_status_error_with_status_code(self):
68
- with patch('git_commit_msg_ai.ai_client.anthropic.Anthropic') as mock_anthropic_class:
72
+ def test_raises_ai_error_on_api_status_error_with_status_code(self) -> None:
73
+ with ExitStack() as stack:
74
+ mock_anthropic_class = stack.enter_context(patch('git_commit_msg_ai.ai_client.anthropic.Anthropic'))
69
75
  mock_client = MagicMock()
70
76
  mock_client.messages.create.side_effect = _make_status_error(anthropic.APIStatusError, 500)
71
77
  mock_anthropic_class.return_value = mock_client
@@ -0,0 +1,149 @@
1
+ from contextlib import ExitStack
2
+ from unittest.mock import MagicMock, patch
3
+
4
+ import pytest
5
+
6
+ from git_commit_msg_ai.cli import main
7
+ from git_commit_msg_ai.exceptions import AIError, GitError
8
+
9
+ COMMIT_MESSAGE = 'feat: add feature'
10
+ EDITED_COMMIT_MESSAGE = 'edited commit message'
11
+ STAGED_DIFF = 'diff content'
12
+ GENERIC_COMMIT_MESSAGE = 'generic commit message'
13
+
14
+
15
+ class TestMain:
16
+ def _run_with_input(self, user_input: str, commit_message: str = COMMIT_MESSAGE) -> tuple[MagicMock, MagicMock, MagicMock]:
17
+ mock_git_ops = MagicMock()
18
+ mock_git_ops.get_staged_diff.return_value = STAGED_DIFF
19
+
20
+ mock_ai_client = MagicMock()
21
+ mock_ai_client.generate_commit_message.return_value = commit_message
22
+
23
+ mock_editor = MagicMock()
24
+ mock_editor.open_in_editor.return_value = EDITED_COMMIT_MESSAGE
25
+
26
+ with ExitStack() as stack:
27
+ stack.enter_context(patch('git_commit_msg_ai.cli.git_ops', mock_git_ops))
28
+ stack.enter_context(patch('git_commit_msg_ai.cli.ai_client', mock_ai_client))
29
+ stack.enter_context(patch('git_commit_msg_ai.cli.editor', mock_editor))
30
+ stack.enter_context(patch('builtins.input', return_value=user_input))
31
+ main()
32
+
33
+ return mock_git_ops, mock_ai_client, mock_editor
34
+
35
+ def test_accept_calls_commit_with_generated_message(self) -> None:
36
+ mock_git_ops, _, _ = self._run_with_input('a', COMMIT_MESSAGE)
37
+
38
+ mock_git_ops.commit.assert_called_once_with(COMMIT_MESSAGE)
39
+
40
+ def test_edit_opens_editor_then_commits_edited_message(self) -> None:
41
+ mock_git_ops, _, mock_editor = self._run_with_input('e', COMMIT_MESSAGE)
42
+
43
+ mock_editor.open_in_editor.assert_called_once_with(COMMIT_MESSAGE)
44
+ mock_git_ops.commit.assert_called_once_with(EDITED_COMMIT_MESSAGE)
45
+
46
+ def test_reject_does_not_commit(self) -> None:
47
+ mock_git_ops, _, _ = self._run_with_input('r')
48
+
49
+ mock_git_ops.commit.assert_not_called()
50
+
51
+ def test_invalid_input_exits_with_code_1(self) -> None:
52
+ mock_git_ops = MagicMock()
53
+ mock_git_ops.get_staged_diff.return_value = STAGED_DIFF
54
+
55
+ mock_ai_client = MagicMock()
56
+ mock_ai_client.generate_commit_message.return_value = GENERIC_COMMIT_MESSAGE
57
+
58
+ with ExitStack() as stack:
59
+ stack.enter_context(patch('git_commit_msg_ai.cli.git_ops', mock_git_ops))
60
+ stack.enter_context(patch('git_commit_msg_ai.cli.ai_client', mock_ai_client))
61
+ stack.enter_context(patch('git_commit_msg_ai.cli.editor'))
62
+ stack.enter_context(patch('builtins.input', return_value='x'))
63
+ with pytest.raises(SystemExit) as exc_info:
64
+ main()
65
+
66
+ assert exc_info.value.code == 1
67
+
68
+ def test_git_error_from_get_staged_diff_exits_with_message(self, capsys: pytest.CaptureFixture[str]) -> None:
69
+ mock_git_ops = MagicMock()
70
+ mock_git_ops.get_staged_diff.side_effect = GitError('staged error')
71
+
72
+ with ExitStack() as stack:
73
+ stack.enter_context(patch('git_commit_msg_ai.cli.git_ops', mock_git_ops))
74
+ with pytest.raises(SystemExit) as exc_info:
75
+ main()
76
+
77
+ assert exc_info.value.code == 1
78
+ assert 'staged error' in capsys.readouterr().out
79
+
80
+ def test_ai_error_from_generate_commit_message_exits_with_message(self, capsys: pytest.CaptureFixture[str]) -> None:
81
+ mock_git_ops = MagicMock()
82
+ mock_git_ops.get_staged_diff.return_value = STAGED_DIFF
83
+
84
+ mock_ai_client = MagicMock()
85
+ mock_ai_client.generate_commit_message.side_effect = AIError('ai error')
86
+
87
+ with ExitStack() as stack:
88
+ stack.enter_context(patch('git_commit_msg_ai.cli.git_ops', mock_git_ops))
89
+ stack.enter_context(patch('git_commit_msg_ai.cli.ai_client', mock_ai_client))
90
+ with pytest.raises(SystemExit) as exc_info:
91
+ main()
92
+
93
+ assert exc_info.value.code == 1
94
+ assert 'ai error' in capsys.readouterr().out
95
+
96
+ def test_git_error_from_commit_exits_with_message(self, capsys: pytest.CaptureFixture[str]) -> None:
97
+ mock_git_ops = MagicMock()
98
+ mock_git_ops.get_staged_diff.return_value = STAGED_DIFF
99
+ mock_git_ops.commit.side_effect = GitError('commit error')
100
+
101
+ mock_ai_client = MagicMock()
102
+ mock_ai_client.generate_commit_message.return_value = GENERIC_COMMIT_MESSAGE
103
+
104
+ with ExitStack() as stack:
105
+ stack.enter_context(patch('git_commit_msg_ai.cli.git_ops', mock_git_ops))
106
+ stack.enter_context(patch('git_commit_msg_ai.cli.ai_client', mock_ai_client))
107
+ stack.enter_context(patch('git_commit_msg_ai.cli.editor'))
108
+ stack.enter_context(patch('builtins.input', return_value='a'))
109
+ with pytest.raises(SystemExit) as exc_info:
110
+ main()
111
+
112
+ assert exc_info.value.code == 1
113
+ assert 'commit error' in capsys.readouterr().out
114
+
115
+ def test_keyboard_interrupt_exits_with_aborted_message(self, capsys: pytest.CaptureFixture[str]) -> None:
116
+ mock_git_ops = MagicMock()
117
+ mock_git_ops.get_staged_diff.return_value = STAGED_DIFF
118
+
119
+ mock_ai_client = MagicMock()
120
+ mock_ai_client.generate_commit_message.return_value = GENERIC_COMMIT_MESSAGE
121
+
122
+ with ExitStack() as stack:
123
+ stack.enter_context(patch('git_commit_msg_ai.cli.git_ops', mock_git_ops))
124
+ stack.enter_context(patch('git_commit_msg_ai.cli.ai_client', mock_ai_client))
125
+ stack.enter_context(patch('git_commit_msg_ai.cli.editor'))
126
+ stack.enter_context(patch('builtins.input', side_effect=KeyboardInterrupt))
127
+ with pytest.raises(SystemExit) as exc_info:
128
+ main()
129
+
130
+ assert exc_info.value.code == 1
131
+ assert 'Aborted.' in capsys.readouterr().out
132
+
133
+ def test_eof_error_exits_with_aborted_message(self, capsys: pytest.CaptureFixture[str]) -> None:
134
+ mock_git_ops = MagicMock()
135
+ mock_git_ops.get_staged_diff.return_value = STAGED_DIFF
136
+
137
+ mock_ai_client = MagicMock()
138
+ mock_ai_client.generate_commit_message.return_value = GENERIC_COMMIT_MESSAGE
139
+
140
+ with ExitStack() as stack:
141
+ stack.enter_context(patch('git_commit_msg_ai.cli.git_ops', mock_git_ops))
142
+ stack.enter_context(patch('git_commit_msg_ai.cli.ai_client', mock_ai_client))
143
+ stack.enter_context(patch('git_commit_msg_ai.cli.editor'))
144
+ stack.enter_context(patch('builtins.input', side_effect=EOFError))
145
+ with pytest.raises(SystemExit) as exc_info:
146
+ main()
147
+
148
+ assert exc_info.value.code == 1
149
+ assert 'Aborted.' in capsys.readouterr().out
@@ -0,0 +1,137 @@
1
+ import os
2
+ import subprocess
3
+ import tempfile
4
+ from contextlib import ExitStack
5
+ from unittest.mock import MagicMock, patch
6
+
7
+ import pytest
8
+
9
+ from git_commit_msg_ai.editor import get_default_editor, open_in_editor
10
+ from git_commit_msg_ai.exceptions import EditorError
11
+
12
+ TEMP_FILE_PATH = os.path.join(tempfile.gettempdir(), 'tmpfile.txt')
13
+ INITIAL_TEXT = 'initial text'
14
+ EDITED_TEXT_WITH_WHITESPACE = ' edited text '
15
+ EDITED_TEXT = 'edited text'
16
+ GENERIC_TEXT = 'text'
17
+ VIM_EDITOR = 'vim'
18
+
19
+
20
+ def _make_named_temporary_file_mock() -> MagicMock:
21
+ mock_temp_file = MagicMock()
22
+ mock_temp_file.name = TEMP_FILE_PATH
23
+ mock_temp_file.__enter__ = MagicMock(return_value=mock_temp_file)
24
+ mock_temp_file.__exit__ = MagicMock(return_value=False)
25
+
26
+ return mock_temp_file
27
+
28
+
29
+ def _make_open_mock(text: str) -> MagicMock:
30
+ mock_file = MagicMock()
31
+ mock_file.read.return_value = text
32
+ mock_file.__enter__ = MagicMock(return_value=mock_file)
33
+ mock_file.__exit__ = MagicMock(return_value=False)
34
+
35
+ return mock_file
36
+
37
+
38
+ class TestOpenInEditor:
39
+ def test_returns_stripped_edited_text(self) -> None:
40
+ with ExitStack() as stack:
41
+ stack.enter_context(patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()))
42
+ stack.enter_context(patch('git_commit_msg_ai.editor.subprocess.run'))
43
+ stack.enter_context(patch('git_commit_msg_ai.editor.open', return_value=_make_open_mock(EDITED_TEXT_WITH_WHITESPACE)))
44
+ stack.enter_context(patch('git_commit_msg_ai.editor.os.unlink'))
45
+
46
+ result = open_in_editor(INITIAL_TEXT)
47
+
48
+ assert result == EDITED_TEXT
49
+
50
+ def test_uses_editor_env_var(self) -> None:
51
+ with ExitStack() as stack:
52
+ stack.enter_context(patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()))
53
+ mock_run = stack.enter_context(patch('git_commit_msg_ai.editor.subprocess.run'))
54
+ stack.enter_context(patch('git_commit_msg_ai.editor.open', return_value=_make_open_mock(GENERIC_TEXT)))
55
+ stack.enter_context(patch('git_commit_msg_ai.editor.os.unlink'))
56
+ stack.enter_context(patch('git_commit_msg_ai.editor.os.environ.get', return_value=VIM_EDITOR))
57
+
58
+ open_in_editor(INITIAL_TEXT)
59
+
60
+ mock_run.assert_called_once_with([VIM_EDITOR, TEMP_FILE_PATH], check=True)
61
+
62
+ def test_falls_back_to_platform_default_when_editor_not_set(self) -> None:
63
+ sentinel_editor = 'test-sentinel-editor'
64
+
65
+ with ExitStack() as stack:
66
+ stack.enter_context(patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()))
67
+ mock_run = stack.enter_context(patch('git_commit_msg_ai.editor.subprocess.run'))
68
+ stack.enter_context(patch('git_commit_msg_ai.editor.open', return_value=_make_open_mock(GENERIC_TEXT)))
69
+ stack.enter_context(patch('git_commit_msg_ai.editor.os.unlink'))
70
+ stack.enter_context(patch('git_commit_msg_ai.editor.get_default_editor', return_value=sentinel_editor))
71
+ stack.enter_context(patch.dict('os.environ', {}, clear=True))
72
+
73
+ open_in_editor(INITIAL_TEXT)
74
+
75
+ mock_run.assert_called_once_with([sentinel_editor, TEMP_FILE_PATH], check=True)
76
+
77
+ def test_raises_editor_error_on_temp_file_creation_failure(self) -> None:
78
+ with ExitStack() as stack:
79
+ stack.enter_context(patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', side_effect=OSError))
80
+ mock_unlink = stack.enter_context(patch('git_commit_msg_ai.editor.os.unlink'))
81
+
82
+ with pytest.raises(EditorError, match='temporary file'):
83
+ open_in_editor(INITIAL_TEXT)
84
+
85
+ mock_unlink.assert_not_called()
86
+
87
+ def test_raises_editor_error_when_editor_not_found(self) -> None:
88
+ with ExitStack() as stack:
89
+ stack.enter_context(patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()))
90
+ stack.enter_context(patch('git_commit_msg_ai.editor.subprocess.run', side_effect=FileNotFoundError))
91
+ mock_unlink = stack.enter_context(patch('git_commit_msg_ai.editor.os.unlink'))
92
+ stack.enter_context(patch('git_commit_msg_ai.editor.os.environ.get', return_value=VIM_EDITOR))
93
+
94
+ with pytest.raises(EditorError, match='was not found'):
95
+ open_in_editor(INITIAL_TEXT)
96
+
97
+ mock_unlink.assert_called_once_with(TEMP_FILE_PATH)
98
+
99
+ def test_raises_editor_error_when_editor_exits_with_error(self) -> None:
100
+ with ExitStack() as stack:
101
+ stack.enter_context(patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()))
102
+ stack.enter_context(patch('git_commit_msg_ai.editor.subprocess.run', side_effect=subprocess.CalledProcessError(1, VIM_EDITOR)))
103
+ mock_unlink = stack.enter_context(patch('git_commit_msg_ai.editor.os.unlink'))
104
+ stack.enter_context(patch('git_commit_msg_ai.editor.os.environ.get', return_value=VIM_EDITOR))
105
+
106
+ with pytest.raises(EditorError, match='exited with an error'):
107
+ open_in_editor(INITIAL_TEXT)
108
+
109
+ mock_unlink.assert_called_once_with(TEMP_FILE_PATH)
110
+
111
+ def test_raises_editor_error_when_file_read_fails(self) -> None:
112
+ with ExitStack() as stack:
113
+ stack.enter_context(patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()))
114
+ stack.enter_context(patch('git_commit_msg_ai.editor.subprocess.run'))
115
+ stack.enter_context(patch('git_commit_msg_ai.editor.open', side_effect=OSError))
116
+ mock_unlink = stack.enter_context(patch('git_commit_msg_ai.editor.os.unlink'))
117
+
118
+ with pytest.raises(EditorError, match='Could not read'):
119
+ open_in_editor(INITIAL_TEXT)
120
+
121
+ mock_unlink.assert_called_once_with(TEMP_FILE_PATH)
122
+
123
+
124
+ class TestGetDefaultEditor:
125
+ def test_returns_notepad_on_windows(self) -> None:
126
+ with ExitStack() as stack:
127
+ stack.enter_context(patch('git_commit_msg_ai.editor.platform.system', return_value='Windows'))
128
+ result = get_default_editor()
129
+
130
+ assert result == 'notepad'
131
+
132
+ def test_returns_vi_on_non_windows(self) -> None:
133
+ with ExitStack() as stack:
134
+ stack.enter_context(patch('git_commit_msg_ai.editor.platform.system', return_value='Linux'))
135
+ result = get_default_editor()
136
+
137
+ assert result == 'vi'
@@ -1,25 +1,25 @@
1
1
  from git_commit_msg_ai.exceptions import AIError, EditorError, GitCommitAIError, GitError
2
2
 
3
3
 
4
- def test_git_error_is_subclass_of_base():
4
+ def test_git_error_is_subclass_of_base() -> None:
5
5
  assert issubclass(GitError, GitCommitAIError)
6
6
 
7
7
 
8
- def test_ai_error_is_subclass_of_base():
8
+ def test_ai_error_is_subclass_of_base() -> None:
9
9
  assert issubclass(AIError, GitCommitAIError)
10
10
 
11
11
 
12
- def test_editor_error_is_subclass_of_base():
12
+ def test_editor_error_is_subclass_of_base() -> None:
13
13
  assert issubclass(EditorError, GitCommitAIError)
14
14
 
15
15
 
16
- def test_git_error_carries_message():
16
+ def test_git_error_carries_message() -> None:
17
17
  assert str(GitError('git failed')) == 'git failed'
18
18
 
19
19
 
20
- def test_ai_error_carries_message():
20
+ def test_ai_error_carries_message() -> None:
21
21
  assert str(AIError('api failed')) == 'api failed'
22
22
 
23
23
 
24
- def test_editor_error_carries_message():
24
+ def test_editor_error_carries_message() -> None:
25
25
  assert str(EditorError('editor failed')) == 'editor failed'
@@ -0,0 +1,81 @@
1
+ import subprocess
2
+ from contextlib import ExitStack
3
+ from unittest.mock import MagicMock, patch
4
+
5
+ import pytest
6
+
7
+ from git_commit_msg_ai.exceptions import GitError
8
+ from git_commit_msg_ai.git_ops import COMMIT_SUBCOMMAND, GIT_COMMAND, MESSAGE_FLAG, commit, get_staged_diff
9
+
10
+ COMMIT_MESSAGE = 'my message'
11
+
12
+
13
+ class TestGetStagedDiff:
14
+ def test_returns_decoded_diff(self) -> None:
15
+ with ExitStack() as stack:
16
+ mock_check_output = stack.enter_context(patch('git_commit_msg_ai.git_ops.subprocess.check_output'))
17
+ mock_check_output.return_value = b'diff content'
18
+
19
+ result = get_staged_diff()
20
+
21
+ assert result == 'diff content'
22
+
23
+ def test_raises_git_error_when_git_not_found(self) -> None:
24
+ with ExitStack() as stack:
25
+ mock_check_output = stack.enter_context(patch('git_commit_msg_ai.git_ops.subprocess.check_output'))
26
+ mock_check_output.side_effect = FileNotFoundError
27
+
28
+ with pytest.raises(GitError, match='not installed'):
29
+ get_staged_diff()
30
+
31
+ def test_raises_git_error_on_called_process_error(self) -> None:
32
+ with ExitStack() as stack:
33
+ mock_check_output = stack.enter_context(patch('git_commit_msg_ai.git_ops.subprocess.check_output'))
34
+ mock_check_output.side_effect = subprocess.CalledProcessError(1, 'git')
35
+
36
+ with pytest.raises(GitError, match='git repository'):
37
+ get_staged_diff()
38
+
39
+ def test_raises_git_error_on_unicode_decode_error(self) -> None:
40
+ mock_bytes = MagicMock()
41
+ mock_bytes.decode.side_effect = UnicodeDecodeError('utf-8', b'', 0, 1, 'reason')
42
+
43
+ with ExitStack() as stack:
44
+ mock_check_output = stack.enter_context(patch('git_commit_msg_ai.git_ops.subprocess.check_output'))
45
+ mock_check_output.return_value = mock_bytes
46
+
47
+ with pytest.raises(GitError, match='UTF-8'):
48
+ get_staged_diff()
49
+
50
+ def test_raises_git_error_when_diff_is_empty(self) -> None:
51
+ with ExitStack() as stack:
52
+ mock_check_output = stack.enter_context(patch('git_commit_msg_ai.git_ops.subprocess.check_output'))
53
+ mock_check_output.return_value = b''
54
+
55
+ with pytest.raises(GitError, match='No staged changes'):
56
+ get_staged_diff()
57
+
58
+
59
+ class TestCommit:
60
+ def test_calls_subprocess_run_with_correct_args(self) -> None:
61
+ with ExitStack() as stack:
62
+ mock_run = stack.enter_context(patch('git_commit_msg_ai.git_ops.subprocess.run'))
63
+ commit(COMMIT_MESSAGE)
64
+
65
+ mock_run.assert_called_once_with([GIT_COMMAND, COMMIT_SUBCOMMAND, MESSAGE_FLAG, COMMIT_MESSAGE], check=True)
66
+
67
+ def test_raises_git_error_when_git_not_found(self) -> None:
68
+ with ExitStack() as stack:
69
+ mock_run = stack.enter_context(patch('git_commit_msg_ai.git_ops.subprocess.run'))
70
+ mock_run.side_effect = FileNotFoundError
71
+
72
+ with pytest.raises(GitError, match='not installed'):
73
+ commit(COMMIT_MESSAGE)
74
+
75
+ def test_raises_git_error_on_called_process_error(self) -> None:
76
+ with ExitStack() as stack:
77
+ mock_run = stack.enter_context(patch('git_commit_msg_ai.git_ops.subprocess.run'))
78
+ mock_run.side_effect = subprocess.CalledProcessError(1, GIT_COMMAND)
79
+
80
+ with pytest.raises(GitError, match='git commit failed'):
81
+ commit(COMMIT_MESSAGE)
@@ -1,136 +0,0 @@
1
- from unittest.mock import MagicMock, patch
2
-
3
- import pytest
4
-
5
- from git_commit_msg_ai.cli import main
6
- from git_commit_msg_ai.exceptions import AIError, GitError
7
-
8
-
9
- class TestMain:
10
- def _run_with_input(self, user_input: str, commit_message: str = 'feat: add feature') -> tuple[MagicMock, MagicMock, MagicMock]:
11
- mock_git_ops = MagicMock()
12
- mock_git_ops.get_staged_diff.return_value = 'diff content'
13
-
14
- mock_ai_client = MagicMock()
15
- mock_ai_client.generate_commit_message.return_value = commit_message
16
-
17
- mock_editor = MagicMock()
18
- mock_editor.open_in_editor.return_value = 'edited message'
19
-
20
- with patch('git_commit_msg_ai.cli.git_ops', mock_git_ops):
21
- with patch('git_commit_msg_ai.cli.ai_client', mock_ai_client):
22
- with patch('git_commit_msg_ai.cli.editor', mock_editor):
23
- with patch('builtins.input', return_value=user_input):
24
- main()
25
-
26
- return mock_git_ops, mock_ai_client, mock_editor
27
-
28
- def test_accept_calls_commit_with_generated_message(self):
29
- mock_git_ops, _, _ = self._run_with_input('a', 'feat: add feature')
30
-
31
- mock_git_ops.commit.assert_called_once_with('feat: add feature')
32
-
33
- def test_edit_opens_editor_then_commits_edited_message(self):
34
- mock_git_ops, _, mock_editor = self._run_with_input('e', 'feat: add feature')
35
-
36
- mock_editor.open_in_editor.assert_called_once_with('feat: add feature')
37
- mock_git_ops.commit.assert_called_once_with('edited message')
38
-
39
- def test_reject_does_not_commit(self):
40
- mock_git_ops, _, _ = self._run_with_input('r')
41
-
42
- mock_git_ops.commit.assert_not_called()
43
-
44
- def test_invalid_input_exits_with_code_1(self):
45
- mock_git_ops = MagicMock()
46
- mock_git_ops.get_staged_diff.return_value = 'diff content'
47
-
48
- mock_ai_client = MagicMock()
49
- mock_ai_client.generate_commit_message.return_value = 'msg'
50
-
51
- with patch('git_commit_msg_ai.cli.git_ops', mock_git_ops):
52
- with patch('git_commit_msg_ai.cli.ai_client', mock_ai_client):
53
- with patch('git_commit_msg_ai.cli.editor'):
54
- with patch('builtins.input', return_value='x'):
55
- with pytest.raises(SystemExit) as exc_info:
56
- main()
57
-
58
- assert exc_info.value.code == 1
59
-
60
- def test_git_error_from_get_staged_diff_exits_with_message(self, capsys: pytest.CaptureFixture[str]):
61
- mock_git_ops = MagicMock()
62
- mock_git_ops.get_staged_diff.side_effect = GitError('staged error')
63
-
64
- with patch('git_commit_msg_ai.cli.git_ops', mock_git_ops):
65
- with pytest.raises(SystemExit) as exc_info:
66
- main()
67
-
68
- assert exc_info.value.code == 1
69
- assert 'staged error' in capsys.readouterr().out
70
-
71
- def test_ai_error_from_generate_commit_message_exits_with_message(self, capsys: pytest.CaptureFixture[str]):
72
- mock_git_ops = MagicMock()
73
- mock_git_ops.get_staged_diff.return_value = 'diff content'
74
-
75
- mock_ai_client = MagicMock()
76
- mock_ai_client.generate_commit_message.side_effect = AIError('ai error')
77
-
78
- with patch('git_commit_msg_ai.cli.git_ops', mock_git_ops):
79
- with patch('git_commit_msg_ai.cli.ai_client', mock_ai_client):
80
- with pytest.raises(SystemExit) as exc_info:
81
- main()
82
-
83
- assert exc_info.value.code == 1
84
- assert 'ai error' in capsys.readouterr().out
85
-
86
- def test_git_error_from_commit_exits_with_message(self, capsys: pytest.CaptureFixture[str]):
87
- mock_git_ops = MagicMock()
88
- mock_git_ops.get_staged_diff.return_value = 'diff content'
89
- mock_git_ops.commit.side_effect = GitError('commit error')
90
-
91
- mock_ai_client = MagicMock()
92
- mock_ai_client.generate_commit_message.return_value = 'msg'
93
-
94
- with patch('git_commit_msg_ai.cli.git_ops', mock_git_ops):
95
- with patch('git_commit_msg_ai.cli.ai_client', mock_ai_client):
96
- with patch('git_commit_msg_ai.cli.editor'):
97
- with patch('builtins.input', return_value='a'):
98
- with pytest.raises(SystemExit) as exc_info:
99
- main()
100
-
101
- assert exc_info.value.code == 1
102
- assert 'commit error' in capsys.readouterr().out
103
-
104
- def test_keyboard_interrupt_exits_with_aborted_message(self, capsys: pytest.CaptureFixture[str]):
105
- mock_git_ops = MagicMock()
106
- mock_git_ops.get_staged_diff.return_value = 'diff content'
107
-
108
- mock_ai_client = MagicMock()
109
- mock_ai_client.generate_commit_message.return_value = 'msg'
110
-
111
- with patch('git_commit_msg_ai.cli.git_ops', mock_git_ops):
112
- with patch('git_commit_msg_ai.cli.ai_client', mock_ai_client):
113
- with patch('git_commit_msg_ai.cli.editor'):
114
- with patch('builtins.input', side_effect=KeyboardInterrupt):
115
- with pytest.raises(SystemExit) as exc_info:
116
- main()
117
-
118
- assert exc_info.value.code == 1
119
- assert 'Aborted.' in capsys.readouterr().out
120
-
121
- def test_eof_error_exits_with_aborted_message(self, capsys: pytest.CaptureFixture[str]):
122
- mock_git_ops = MagicMock()
123
- mock_git_ops.get_staged_diff.return_value = 'diff content'
124
-
125
- mock_ai_client = MagicMock()
126
- mock_ai_client.generate_commit_message.return_value = 'msg'
127
-
128
- with patch('git_commit_msg_ai.cli.git_ops', mock_git_ops):
129
- with patch('git_commit_msg_ai.cli.ai_client', mock_ai_client):
130
- with patch('git_commit_msg_ai.cli.editor'):
131
- with patch('builtins.input', side_effect=EOFError):
132
- with pytest.raises(SystemExit) as exc_info:
133
- main()
134
-
135
- assert exc_info.value.code == 1
136
- assert 'Aborted.' in capsys.readouterr().out
@@ -1,96 +0,0 @@
1
- import subprocess
2
- from unittest.mock import MagicMock, patch
3
-
4
- import pytest
5
-
6
- from git_commit_msg_ai.editor import open_in_editor
7
- from git_commit_msg_ai.exceptions import EditorError
8
-
9
- TEMP_FILE_PATH = '/tmp/tmpfile.txt'
10
-
11
-
12
- def _make_named_temporary_file_mock() -> MagicMock:
13
- mock_temp_file = MagicMock()
14
- mock_temp_file.name = TEMP_FILE_PATH
15
- mock_temp_file.__enter__ = MagicMock(return_value=mock_temp_file)
16
- mock_temp_file.__exit__ = MagicMock(return_value=False)
17
-
18
- return mock_temp_file
19
-
20
-
21
- def _make_open_mock(text: str) -> MagicMock:
22
- mock_file = MagicMock()
23
- mock_file.read.return_value = text
24
- mock_file.__enter__ = MagicMock(return_value=mock_file)
25
- mock_file.__exit__ = MagicMock(return_value=False)
26
-
27
- return mock_file
28
-
29
-
30
- class TestOpenInEditor:
31
- def test_returns_stripped_edited_text(self):
32
- with patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()):
33
- with patch('git_commit_msg_ai.editor.subprocess.run'):
34
- with patch('git_commit_msg_ai.editor.open', return_value=_make_open_mock(' edited text ')):
35
- with patch('git_commit_msg_ai.editor.os.unlink'):
36
- result = open_in_editor('initial text')
37
-
38
- assert result == 'edited text'
39
-
40
- def test_uses_editor_env_var(self):
41
- with patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()):
42
- with patch('git_commit_msg_ai.editor.subprocess.run') as mock_run:
43
- with patch('git_commit_msg_ai.editor.open', return_value=_make_open_mock('text')):
44
- with patch('git_commit_msg_ai.editor.os.unlink'):
45
- with patch('git_commit_msg_ai.editor.os.environ.get', return_value='vim'):
46
- open_in_editor('initial text')
47
-
48
- mock_run.assert_called_once_with(['vim', TEMP_FILE_PATH], check=True)
49
-
50
- def test_falls_back_to_notepad_when_editor_not_set(self):
51
- with patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()):
52
- with patch('git_commit_msg_ai.editor.subprocess.run') as mock_run:
53
- with patch('git_commit_msg_ai.editor.open', return_value=_make_open_mock('text')):
54
- with patch('git_commit_msg_ai.editor.os.unlink'):
55
- with patch('git_commit_msg_ai.editor.os.environ.get', return_value='notepad'):
56
- open_in_editor('initial text')
57
-
58
- mock_run.assert_called_once_with(['notepad', TEMP_FILE_PATH], check=True)
59
-
60
- def test_raises_editor_error_on_temp_file_creation_failure(self):
61
- with patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', side_effect=OSError):
62
- with patch('git_commit_msg_ai.editor.os.unlink') as mock_unlink:
63
- with pytest.raises(EditorError, match='temporary file'):
64
- open_in_editor('initial text')
65
-
66
- mock_unlink.assert_not_called()
67
-
68
- def test_raises_editor_error_when_editor_not_found(self):
69
- with patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()):
70
- with patch('git_commit_msg_ai.editor.subprocess.run', side_effect=FileNotFoundError):
71
- with patch('git_commit_msg_ai.editor.os.unlink') as mock_unlink:
72
- with patch('git_commit_msg_ai.editor.os.environ.get', return_value='vim'):
73
- with pytest.raises(EditorError, match='was not found'):
74
- open_in_editor('initial text')
75
-
76
- mock_unlink.assert_called_once_with(TEMP_FILE_PATH)
77
-
78
- def test_raises_editor_error_when_editor_exits_with_error(self):
79
- with patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()):
80
- with patch('git_commit_msg_ai.editor.subprocess.run', side_effect=subprocess.CalledProcessError(1, 'vim')):
81
- with patch('git_commit_msg_ai.editor.os.unlink') as mock_unlink:
82
- with patch('git_commit_msg_ai.editor.os.environ.get', return_value='vim'):
83
- with pytest.raises(EditorError, match='exited with an error'):
84
- open_in_editor('initial text')
85
-
86
- mock_unlink.assert_called_once_with(TEMP_FILE_PATH)
87
-
88
- def test_raises_editor_error_when_file_read_fails(self):
89
- with patch('git_commit_msg_ai.editor.tempfile.NamedTemporaryFile', return_value=_make_named_temporary_file_mock()):
90
- with patch('git_commit_msg_ai.editor.subprocess.run'):
91
- with patch('git_commit_msg_ai.editor.open', side_effect=OSError):
92
- with patch('git_commit_msg_ai.editor.os.unlink') as mock_unlink:
93
- with pytest.raises(EditorError, match='Could not read'):
94
- open_in_editor('initial text')
95
-
96
- mock_unlink.assert_called_once_with(TEMP_FILE_PATH)
@@ -1,70 +0,0 @@
1
- import subprocess
2
- from unittest.mock import MagicMock, patch
3
-
4
- import pytest
5
-
6
- from git_commit_msg_ai.exceptions import GitError
7
- from git_commit_msg_ai.git_ops import commit, get_staged_diff
8
-
9
-
10
- class TestGetStagedDiff:
11
- def test_returns_decoded_diff(self):
12
- with patch('git_commit_msg_ai.git_ops.subprocess.check_output') as mock_check_output:
13
- mock_check_output.return_value = b'diff content'
14
-
15
- result = get_staged_diff()
16
-
17
- assert result == 'diff content'
18
-
19
- def test_raises_git_error_when_git_not_found(self):
20
- with patch('git_commit_msg_ai.git_ops.subprocess.check_output') as mock_check_output:
21
- mock_check_output.side_effect = FileNotFoundError
22
-
23
- with pytest.raises(GitError, match='not installed'):
24
- get_staged_diff()
25
-
26
- def test_raises_git_error_on_called_process_error(self):
27
- with patch('git_commit_msg_ai.git_ops.subprocess.check_output') as mock_check_output:
28
- mock_check_output.side_effect = subprocess.CalledProcessError(1, 'git')
29
-
30
- with pytest.raises(GitError, match='git repository'):
31
- get_staged_diff()
32
-
33
- def test_raises_git_error_on_unicode_decode_error(self):
34
- mock_bytes = MagicMock()
35
- mock_bytes.decode.side_effect = UnicodeDecodeError('utf-8', b'', 0, 1, 'reason')
36
-
37
- with patch('git_commit_msg_ai.git_ops.subprocess.check_output') as mock_check_output:
38
- mock_check_output.return_value = mock_bytes
39
-
40
- with pytest.raises(GitError, match='UTF-8'):
41
- get_staged_diff()
42
-
43
- def test_raises_git_error_when_diff_is_empty(self):
44
- with patch('git_commit_msg_ai.git_ops.subprocess.check_output') as mock_check_output:
45
- mock_check_output.return_value = b''
46
-
47
- with pytest.raises(GitError, match='No staged changes'):
48
- get_staged_diff()
49
-
50
-
51
- class TestCommit:
52
- def test_calls_subprocess_run_with_correct_args(self):
53
- with patch('git_commit_msg_ai.git_ops.subprocess.run') as mock_run:
54
- commit('my message')
55
-
56
- mock_run.assert_called_once_with(['git', 'commit', '-m', 'my message'], check=True)
57
-
58
- def test_raises_git_error_when_git_not_found(self):
59
- with patch('git_commit_msg_ai.git_ops.subprocess.run') as mock_run:
60
- mock_run.side_effect = FileNotFoundError
61
-
62
- with pytest.raises(GitError, match='not installed'):
63
- commit('my message')
64
-
65
- def test_raises_git_error_on_called_process_error(self):
66
- with patch('git_commit_msg_ai.git_ops.subprocess.run') as mock_run:
67
- mock_run.side_effect = subprocess.CalledProcessError(1, 'git')
68
-
69
- with pytest.raises(GitError, match='git commit failed'):
70
- commit('my message')