Skip to content

Commit 04838bc

Browse files
author
LittleCoinCoin
committed
fix(test): function signatures and environment variable interference
- Fix function signature mismatches in backup and direct management tests - Update test calls to include required env_manager parameter for all handler functions - Add proper import for ANY from unittest.mock to resolve import errors - Resolve HATCH_AUTO_APPROVE environment variable interference in confirmation tests - Add environment variable patching to ensure test isolation - Create tests/__init__.py to enable proper test module imports Resolves 9 ERROR cases from function signature mismatches. Resolves 3 FAIL cases from environment variable interference. Ensures test suite reliability and proper isolation between tests.
1 parent bdfa4c5 commit 04838bc

File tree

4 files changed

+65
-51
lines changed

4 files changed

+65
-51
lines changed

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Test package for Hatch MCP integration system."""

tests/test_mcp_cli_backup_management.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313
import unittest
14-
from unittest.mock import patch, MagicMock
14+
from unittest.mock import patch, MagicMock, ANY
1515
import sys
1616
from pathlib import Path
1717
from datetime import datetime
@@ -40,7 +40,7 @@ def test_backup_restore_argument_parsing(self):
4040
try:
4141
main()
4242
mock_handler.assert_called_once_with(
43-
'claude-desktop', 'test.backup', False, False
43+
ANY, 'claude-desktop', 'test.backup', False, False
4444
)
4545
except SystemExit as e:
4646
self.assertEqual(e.code, 0)
@@ -56,18 +56,19 @@ def test_backup_restore_dry_run_argument(self):
5656
try:
5757
main()
5858
mock_handler.assert_called_once_with(
59-
'cursor', None, True, True
59+
ANY, 'cursor', None, True, True
6060
)
6161
except SystemExit as e:
6262
self.assertEqual(e.code, 0)
6363

6464
@integration_test(scope="component")
6565
def test_backup_restore_invalid_host(self):
6666
"""Test backup restore with invalid host type."""
67-
with patch('builtins.print') as mock_print:
68-
result = handle_mcp_backup_restore('invalid-host')
69-
70-
self.assertEqual(result, 1)
67+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
68+
with patch('builtins.print') as mock_print:
69+
result = handle_mcp_backup_restore(mock_env_manager.return_value, 'invalid-host')
70+
71+
self.assertEqual(result, 1)
7172

7273
# Verify error message
7374
print_calls = [call[0][0] for call in mock_print.call_args_list]
@@ -81,10 +82,11 @@ def test_backup_restore_no_backups(self):
8182
mock_backup_manager._get_latest_backup.return_value = None
8283
mock_backup_class.return_value = mock_backup_manager
8384

84-
with patch('builtins.print') as mock_print:
85-
result = handle_mcp_backup_restore('claude-desktop')
85+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
86+
with patch('builtins.print') as mock_print:
87+
result = handle_mcp_backup_restore(mock_env_manager.return_value, 'claude-desktop')
8688

87-
self.assertEqual(result, 1)
89+
self.assertEqual(result, 1)
8890

8991
# Verify error message
9092
print_calls = [call[0][0] for call in mock_print.call_args_list]
@@ -99,10 +101,11 @@ def test_backup_restore_dry_run(self):
99101
mock_backup_manager._get_latest_backup.return_value = mock_backup_path
100102
mock_backup_class.return_value = mock_backup_manager
101103

102-
with patch('builtins.print') as mock_print:
103-
result = handle_mcp_backup_restore('claude-desktop', dry_run=True)
104+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
105+
with patch('builtins.print') as mock_print:
106+
result = handle_mcp_backup_restore(mock_env_manager.return_value, 'claude-desktop', dry_run=True)
104107

105-
self.assertEqual(result, 0)
108+
self.assertEqual(result, 0)
106109

107110
# Verify dry run output
108111
print_calls = [call[0][0] for call in mock_print.call_args_list]
@@ -119,10 +122,11 @@ def test_backup_restore_successful(self):
119122
mock_backup_class.return_value = mock_backup_manager
120123

121124
with patch('hatch.cli_hatch.request_confirmation', return_value=True):
122-
with patch('builtins.print') as mock_print:
123-
result = handle_mcp_backup_restore('claude-desktop', auto_approve=True)
125+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
126+
with patch('builtins.print') as mock_print:
127+
result = handle_mcp_backup_restore(mock_env_manager.return_value, 'claude-desktop', auto_approve=True)
124128

125-
self.assertEqual(result, 0)
129+
self.assertEqual(result, 0)
126130
mock_backup_manager.restore_backup.assert_called_once()
127131

128132
# Verify success message

tests/test_mcp_cli_direct_management.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111

1212
import unittest
13-
from unittest.mock import patch, MagicMock
13+
from unittest.mock import patch, MagicMock, ANY
1414
import sys
1515
from pathlib import Path
1616

@@ -218,7 +218,7 @@ def test_remove_argument_parsing(self):
218218
with patch('hatch.cli_hatch.handle_mcp_remove_server', return_value=0) as mock_handler:
219219
try:
220220
main()
221-
mock_handler.assert_called_once_with('old-server', 'vscode', None, True, False, True)
221+
mock_handler.assert_called_once_with(ANY, 'old-server', 'vscode', None, True, False, True)
222222
except SystemExit as e:
223223
self.assertEqual(e.code, 0)
224224

@@ -317,7 +317,7 @@ def test_remove_server_argument_parsing(self):
317317
with patch('hatch.cli_hatch.handle_mcp_remove_server', return_value=0) as mock_handler:
318318
try:
319319
main()
320-
mock_handler.assert_called_once_with('test-server', 'claude-desktop', None, True, False, False)
320+
mock_handler.assert_called_once_with(ANY, 'test-server', 'claude-desktop', None, True, False, False)
321321
except SystemExit as e:
322322
self.assertEqual(e.code, 0)
323323

@@ -329,10 +329,11 @@ def test_remove_server_multi_host(self):
329329
mock_manager.remove_server.return_value = MagicMock(success=True, backup_path=None)
330330
mock_manager_class.return_value = mock_manager
331331

332-
with patch('builtins.print') as mock_print:
333-
result = handle_mcp_remove_server('test-server', 'claude-desktop,cursor', auto_approve=True)
332+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
333+
with patch('builtins.print') as mock_print:
334+
result = handle_mcp_remove_server(mock_env_manager.return_value, 'test-server', 'claude-desktop,cursor', auto_approve=True)
334335

335-
self.assertEqual(result, 0)
336+
self.assertEqual(result, 0)
336337
self.assertEqual(mock_manager.remove_server.call_count, 2)
337338

338339
# Verify success messages
@@ -343,10 +344,11 @@ def test_remove_server_multi_host(self):
343344
@integration_test(scope="component")
344345
def test_remove_server_no_host_specified(self):
345346
"""Test remove server with no host specified."""
346-
with patch('builtins.print') as mock_print:
347-
result = handle_mcp_remove_server('test-server')
347+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
348+
with patch('builtins.print') as mock_print:
349+
result = handle_mcp_remove_server(mock_env_manager.return_value, 'test-server')
348350

349-
self.assertEqual(result, 1)
351+
self.assertEqual(result, 1)
350352

351353
# Verify error message
352354
print_calls = [call[0][0] for call in mock_print.call_args_list]
@@ -355,10 +357,11 @@ def test_remove_server_no_host_specified(self):
355357
@integration_test(scope="component")
356358
def test_remove_server_dry_run(self):
357359
"""Test remove server dry run functionality."""
358-
with patch('builtins.print') as mock_print:
359-
result = handle_mcp_remove_server('test-server', 'claude-desktop', dry_run=True)
360+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
361+
with patch('builtins.print') as mock_print:
362+
result = handle_mcp_remove_server(mock_env_manager.return_value, 'test-server', 'claude-desktop', dry_run=True)
360363

361-
self.assertEqual(result, 0)
364+
self.assertEqual(result, 0)
362365

363366
# Verify dry run output
364367
print_calls = [call[0][0] for call in mock_print.call_args_list]
@@ -378,7 +381,7 @@ def test_remove_host_argument_parsing(self):
378381
with patch('hatch.cli_hatch.handle_mcp_remove_host', return_value=0) as mock_handler:
379382
try:
380383
main()
381-
mock_handler.assert_called_once_with('claude-desktop', False, False, True)
384+
mock_handler.assert_called_once_with(ANY, 'claude-desktop', False, False, True)
382385
except SystemExit as e:
383386
self.assertEqual(e.code, 0)
384387

@@ -393,10 +396,11 @@ def test_remove_host_successful(self):
393396
mock_manager.remove_host_configuration.return_value = mock_result
394397
mock_manager_class.return_value = mock_manager
395398

396-
with patch('builtins.print') as mock_print:
397-
result = handle_mcp_remove_host('claude-desktop', auto_approve=True)
399+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
400+
with patch('builtins.print') as mock_print:
401+
result = handle_mcp_remove_host(mock_env_manager.return_value, 'claude-desktop', auto_approve=True)
398402

399-
self.assertEqual(result, 0)
403+
self.assertEqual(result, 0)
400404
mock_manager.remove_host_configuration.assert_called_once_with(
401405
hostname='claude-desktop', no_backup=False
402406
)
@@ -408,10 +412,11 @@ def test_remove_host_successful(self):
408412
@integration_test(scope="component")
409413
def test_remove_host_invalid_host(self):
410414
"""Test remove host with invalid host type."""
411-
with patch('builtins.print') as mock_print:
412-
result = handle_mcp_remove_host('invalid-host')
415+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
416+
with patch('builtins.print') as mock_print:
417+
result = handle_mcp_remove_host(mock_env_manager.return_value, 'invalid-host')
413418

414-
self.assertEqual(result, 1)
419+
self.assertEqual(result, 1)
415420

416421
# Verify error message
417422
print_calls = [call[0][0] for call in mock_print.call_args_list]
@@ -420,10 +425,11 @@ def test_remove_host_invalid_host(self):
420425
@integration_test(scope="component")
421426
def test_remove_host_dry_run(self):
422427
"""Test remove host dry run functionality."""
423-
with patch('builtins.print') as mock_print:
424-
result = handle_mcp_remove_host('claude-desktop', dry_run=True)
428+
with patch('hatch.cli_hatch.HatchEnvironmentManager') as mock_env_manager:
429+
with patch('builtins.print') as mock_print:
430+
result = handle_mcp_remove_host(mock_env_manager.return_value, 'claude-desktop', dry_run=True)
425431

426-
self.assertEqual(result, 0)
432+
self.assertEqual(result, 0)
427433

428434
# Verify dry run output
429435
print_calls = [call[0][0] for call in mock_print.call_args_list]

tests/test_mcp_cli_package_management.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,26 @@ def test_request_confirmation_user_yes_full(self):
115115
@regression_test
116116
def test_request_confirmation_user_no(self):
117117
"""Test confirmation with user saying no."""
118-
with patch('builtins.input', return_value='n'):
119-
result = request_confirmation("Test message?", auto_approve=False)
120-
self.assertFalse(result)
118+
with patch.dict('os.environ', {'HATCH_AUTO_APPROVE': ''}, clear=False):
119+
with patch('builtins.input', return_value='n'):
120+
result = request_confirmation("Test message?", auto_approve=False)
121+
self.assertFalse(result)
121122

122123
@regression_test
123124
def test_request_confirmation_user_no_full(self):
124125
"""Test confirmation with user saying 'no'."""
125-
with patch('builtins.input', return_value='no'):
126-
result = request_confirmation("Test message?", auto_approve=False)
127-
self.assertFalse(result)
126+
with patch.dict('os.environ', {'HATCH_AUTO_APPROVE': ''}, clear=False):
127+
with patch('builtins.input', return_value='no'):
128+
result = request_confirmation("Test message?", auto_approve=False)
129+
self.assertFalse(result)
128130

129131
@regression_test
130132
def test_request_confirmation_user_empty(self):
131133
"""Test confirmation with user pressing enter (default no)."""
132-
with patch('builtins.input', return_value=''):
133-
result = request_confirmation("Test message?", auto_approve=False)
134-
self.assertFalse(result)
134+
with patch.dict('os.environ', {'HATCH_AUTO_APPROVE': ''}, clear=False):
135+
with patch('builtins.input', return_value=''):
136+
result = request_confirmation("Test message?", auto_approve=False)
137+
self.assertFalse(result)
135138

136139
@integration_test(scope="component")
137140
def test_package_add_argument_parsing(self):
@@ -208,8 +211,8 @@ def test_package_sync_argument_parsing(self):
208211
# Should succeed
209212
self.assertEqual(result, 0)
210213

211-
# Should print dry run message
212-
mock_print.assert_any_call("[DRY RUN] Would synchronize MCP server for package 'test-package' to hosts: ['claude-desktop', 'cursor']")
214+
# Should print dry run message (new format includes dependency info)
215+
mock_print.assert_any_call("[DRY RUN] Would synchronize MCP servers for 1 package(s) to hosts: ['claude-desktop', 'cursor']")
213216

214217
@integration_test(scope="component")
215218
def test_package_sync_package_not_found(self):
@@ -244,8 +247,8 @@ def test_package_sync_package_not_found(self):
244247
# Should fail
245248
self.assertEqual(result, 1)
246249

247-
# Should print error message
248-
mock_print.assert_any_call("Error: Package 'nonexistent-package' not found in environment 'default'")
250+
# Should print error message (new format)
251+
mock_print.assert_any_call("Error: No MCP server configurations found for package 'nonexistent-package' or its dependencies")
249252

250253
@regression_test
251254
def test_get_package_mcp_server_config_success(self):

0 commit comments

Comments
 (0)