OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright 2014 The Swarming Authors. All rights reserved. | |
3 # Use of this source code is governed by the Apache v2.0 license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 import logging | |
7 import os | |
8 import sys | |
9 import tempfile | |
10 import shutil | |
11 import unittest | |
12 import re | |
13 | |
14 # Import this first before manipulating sys.path to ensure it can load fine. | |
15 import logging_utils | |
16 | |
17 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
18 sys.path.insert(0, ROOT_DIR) | |
19 | |
20 import test_env | |
21 test_env.setup_test_env() | |
22 | |
23 from depot_tools import auto_stub | |
24 | |
25 _LOG_HEADER = r'^%s \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d: ' % os.getpid() | |
26 | |
27 | |
28 class TestLoggingUtils(auto_stub.TestCase): | |
29 def test_Capture(self): | |
30 root = logging.RootLogger(logging.DEBUG) | |
31 with logging_utils.CaptureLogs('foo', root) as log: | |
32 root.debug('foo') | |
33 result = log.read() | |
34 self.assertTrue(re.match(_LOG_HEADER + 'DEBUG foo\n$', result), result) | |
35 | |
36 def test_prepare_logging(self): | |
37 root = logging.RootLogger(logging.DEBUG) | |
38 tmp_dir = tempfile.mkdtemp(prefix='logging_utils_test') | |
39 try: | |
40 filepath = os.path.join(tmp_dir, 'test.log') | |
41 logging_utils.prepare_logging(filepath, root) | |
42 root.debug('foo') | |
43 with open(filepath, 'rb') as f: | |
44 result = f.read() | |
45 finally: | |
46 shutil.rmtree(tmp_dir) | |
47 # It'd be nice to figure out a way to ensure it's properly in UTC but it's | |
48 # tricky to do reliably. | |
49 self.assertTrue(re.match(_LOG_HEADER + 'DEBUG foo\n$', result), result) | |
50 | |
51 | |
52 if __name__ == '__main__': | |
53 unittest.main() | |
OLD | NEW |