8
8
9
9
import pytest
10
10
11
- from mcpcat .modules .logging import write_to_log
11
+ from mcpcat .modules .logging import write_to_log , set_debug_mode
12
12
13
13
14
14
class TestLogging :
@@ -31,6 +31,38 @@ def cleanup_log_file(self):
31
31
32
32
def test_write_to_log_creates_file (self , tmp_path ):
33
33
"""Test that write_to_log creates the log file if it doesn't exist."""
34
+ # Enable debug mode
35
+ set_debug_mode (True )
36
+
37
+ # Use a unique file name for this test
38
+ unique_id = str (uuid .uuid4 ())
39
+ log_file = tmp_path / f"test_mcpcat_{ unique_id } .log"
40
+
41
+ # Mock os.path.expanduser to use our temp file
42
+ with patch (
43
+ "mcpcat.modules.logging.os.path.expanduser" , return_value = str (log_file )
44
+ ):
45
+ # Write a test message
46
+ test_message = f"Test log message { unique_id } "
47
+ write_to_log (test_message )
48
+
49
+ # Check that the file was created
50
+ assert log_file .exists (), "Log file was not created"
51
+
52
+ # Read the file content
53
+ content = log_file .read_text ()
54
+
55
+ # Verify the message is in the file
56
+ assert test_message in content , "Log message not found in file"
57
+
58
+ # Verify timestamp format (ISO format)
59
+ assert "T" in content , "Timestamp not in ISO format"
60
+
61
+ def test_write_to_log_checks_debug_mode (self , tmp_path ):
62
+ """Test that write_to_log writes to file when debug mode is enabled."""
63
+ # Enable debug mode
64
+ set_debug_mode (True )
65
+
34
66
# Use a unique file name for this test
35
67
unique_id = str (uuid .uuid4 ())
36
68
log_file = tmp_path / f"test_mcpcat_{ unique_id } .log"
@@ -55,8 +87,29 @@ def test_write_to_log_creates_file(self, tmp_path):
55
87
# Verify timestamp format (ISO format)
56
88
assert "T" in content , "Timestamp not in ISO format"
57
89
90
+ # Check that log file is not created when debug mode is disabled
91
+ set_debug_mode (False )
92
+
93
+ # Use a unique file name for this test
94
+ unique_id = str (uuid .uuid4 ())
95
+ log_file = tmp_path / f"test_mcpcat_{ unique_id } .log"
96
+
97
+ # Mock os.path.expanduser to use our temp file
98
+ with patch (
99
+ "mcpcat.modules.logging.os.path.expanduser" , return_value = str (log_file )
100
+ ):
101
+ # Write a test message
102
+ test_message = f"Test log message { unique_id } "
103
+ write_to_log (test_message )
104
+
105
+ # Check that the file was created
106
+ assert not log_file .exists (), "Log file was wrongly created"
107
+
58
108
def test_write_to_log_appends_messages (self , tmp_path ):
59
109
"""Test that write_to_log appends to existing log file."""
110
+ # Enable debug mode
111
+ set_debug_mode (True )
112
+
60
113
# Use a unique file name for this test
61
114
unique_id = str (uuid .uuid4 ())
62
115
log_file = tmp_path / f"test_mcpcat_{ unique_id } .log"
@@ -105,6 +158,9 @@ def test_write_to_log_appends_messages(self, tmp_path):
105
158
106
159
def test_write_to_log_handles_directory_creation (self , tmp_path ):
107
160
"""Test that write_to_log creates parent directories if needed."""
161
+ # Enable debug mode
162
+ set_debug_mode (True )
163
+
108
164
# Use a unique file name for this test
109
165
unique_id = str (uuid .uuid4 ())
110
166
log_file = tmp_path / f"test_mcpcat_{ unique_id } .log"
@@ -123,6 +179,9 @@ def test_write_to_log_handles_directory_creation(self, tmp_path):
123
179
124
180
def test_write_to_log_silently_handles_errors (self , tmp_path , monkeypatch ):
125
181
"""Test that write_to_log doesn't raise exceptions on errors."""
182
+ # Enable debug mode
183
+ set_debug_mode (True )
184
+
126
185
# Use a unique file name for this test
127
186
unique_id = str (uuid .uuid4 ())
128
187
log_file = tmp_path / f"test_mcpcat_{ unique_id } .log"
@@ -146,6 +205,9 @@ def test_write_to_log_silently_handles_errors(self, tmp_path, monkeypatch):
146
205
147
206
def test_log_format (self , tmp_path ):
148
207
"""Test the format of log entries."""
208
+ # Enable debug mode
209
+ set_debug_mode (True )
210
+
149
211
# Use a unique file name for this test
150
212
unique_id = str (uuid .uuid4 ())
151
213
log_file = tmp_path / f"test_mcpcat_{ unique_id } .log"
0 commit comments