forked from agentstack-ai/AgentStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_log.py
More file actions
180 lines (142 loc) · 6.1 KB
/
test_log.py
File metadata and controls
180 lines (142 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import unittest
import os, sys
import io
import logging
import shutil
from pathlib import Path
from agentstack import log, conf
from agentstack.log import SUCCESS, NOTIFY
BASE_PATH = Path(__file__).parent
class TestLog(unittest.TestCase):
def setUp(self):
self.framework = os.getenv('TEST_FRAMEWORK')
self.test_dir = BASE_PATH / 'tmp' / self.framework / 'test_log'
self.test_dir.mkdir(parents=True, exist_ok=True)
# Set log file to test directory
self.test_log_file = self.test_dir / 'test.log'
log.LOG_FILENAME = self.test_log_file
# Create string IO objects to capture stdout/stderr
self.stdout = io.StringIO()
self.stderr = io.StringIO()
# Set up clean logging instance
log.instance = None
log.set_stdout(self.stdout)
log.set_stderr(self.stderr)
def tearDown(self):
# Clean up test directory
if self.test_dir.exists():
shutil.rmtree(self.test_dir)
# Clear string IO buffers
self.stdout.close()
self.stderr.close()
def test_debug_message(self):
log.debug("Debug message")
self.assertIn("Debug message", self.stdout.getvalue())
self.assertIn("Debug message", self.test_log_file.read_text())
def test_success_message(self):
log.success("Success message")
self.assertIn("Success message", self.stdout.getvalue())
self.assertIn("Success message", self.test_log_file.read_text())
def test_notify_message(self):
log.notify("Notify message")
self.assertIn("Notify message", self.stdout.getvalue())
self.assertIn("Notify message", self.test_log_file.read_text())
def test_info_message(self):
log.info("Info message")
self.assertIn("Info message", self.stdout.getvalue())
self.assertIn("Info message", self.test_log_file.read_text())
def test_warning_message(self):
log.warning("Warning message")
self.assertIn("Warning message", self.stdout.getvalue())
self.assertIn("Warning message", self.test_log_file.read_text())
def test_error_message(self):
log.error("Error message")
self.assertIn("Error message", self.stderr.getvalue())
self.assertIn("Error message", self.test_log_file.read_text())
def test_multiple_messages(self):
log.info("First message")
log.error("Second message")
log.warning("Third message")
stdout_content = self.stdout.getvalue()
stderr_content = self.stderr.getvalue()
file_content = self.test_log_file.read_text()
self.assertIn("First message", stdout_content)
self.assertIn("Third message", stdout_content)
self.assertIn("Second message", stderr_content)
self.assertIn("First message", file_content)
self.assertIn("Second message", file_content)
self.assertIn("Third message", file_content)
def test_stream_redirection(self):
new_stdout = io.StringIO()
new_stderr = io.StringIO()
log.set_stdout(new_stdout)
log.set_stderr(new_stderr)
log.info("Test stdout")
log.error("Test stderr")
self.assertIn("Test stdout", new_stdout.getvalue())
self.assertIn("Test stderr", new_stderr.getvalue())
def test_debug_level_config(self):
# Test with debug disabled
conf.set_debug(False)
log.instance = None # Reset logger
log.debug("Hidden debug")
self.assertEqual("", self.stdout.getvalue())
# Test with debug enabled
conf.set_debug(True)
log.instance = None # Reset logger
log.debug("Visible debug")
self.assertIn("Visible debug", self.stdout.getvalue())
def test_log_file_creation(self):
# Delete log file if exists
if self.test_log_file.exists():
self.test_log_file.unlink()
# First log should create file
self.assertFalse(self.test_log_file.exists())
log.info("Create log file")
self.assertTrue(self.test_log_file.exists())
self.assertIn("Create log file", self.test_log_file.read_text())
def test_debug_mode_filtering(self):
# Test with debug mode off
conf.set_debug(False)
log.instance = None # Reset logger to apply new debug setting
log.debug("Debug message when off")
log.info("Info message when off")
stdout_off = self.stdout.getvalue()
self.assertNotIn("Debug message when off", stdout_off)
self.assertIn("Info message when off", stdout_off)
# Clear buffers
self.stdout.truncate(0)
self.stdout.seek(0)
# Test with debug mode on
conf.set_debug(True)
log.instance = None # Reset logger to apply new debug setting
log.debug("Debug message when on")
log.info("Info message when on")
stdout_on = self.stdout.getvalue()
self.assertIn("Debug message when on", stdout_on)
self.assertIn("Info message when on", stdout_on)
def test_custom_levels_visibility(self):
"""Custom levels should print below DEBUG level"""
# Test with debug mode off
conf.set_debug(False)
log.instance = None
log.debug("Debug message when debug off")
log.success("Success message when debug off")
log.notify("Notify message when debug off")
stdout_off = self.stdout.getvalue()
self.assertNotIn("Debug message when debug off", stdout_off)
self.assertIn("Success message when debug off", stdout_off)
self.assertIn("Notify message when debug off", stdout_off)
# Clear buffers
self.stdout.truncate(0)
self.stdout.seek(0)
# Test with debug mode on
conf.set_debug(True)
log.instance = None
log.debug("Debug message when debug on")
log.success("Success message when debug on")
log.notify("Notify message when debug on")
stdout_on = self.stdout.getvalue()
self.assertIn("Debug message when debug on", stdout_on)
self.assertIn("Success message when debug on", stdout_on)
self.assertIn("Notify message when debug on", stdout_on)