OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2015 The Swarming Authors. All rights reserved. | 2 # Copyright 2015 The Swarming Authors. All rights reserved. |
3 # Use of this source code is governed under the Apache License, Version 2.0 that | 3 # Use of this source code is governed under the Apache License, Version 2.0 that |
4 # can be found in the LICENSE file. | 4 # can be found in the LICENSE file. |
5 | 5 |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import subprocess | 8 import subprocess |
9 import sys | 9 import sys |
10 import tempfile | 10 import tempfile |
11 import shutil | |
12 import unittest | 11 import unittest |
13 import re | 12 import re |
14 | 13 |
15 THIS_FILE = os.path.abspath(__file__) | 14 THIS_FILE = os.path.abspath(__file__) |
16 sys.path.insert(0, os.path.dirname(os.path.dirname(THIS_FILE))) | 15 sys.path.insert(0, os.path.dirname(os.path.dirname(THIS_FILE))) |
17 | 16 |
| 17 from third_party.depot_tools import fix_encoding |
| 18 from utils import file_path |
18 from utils import logging_utils | 19 from utils import logging_utils |
19 | 20 |
20 | 21 |
21 # PID YYYY-MM-DD HH:MM:SS.MMM | 22 # PID YYYY-MM-DD HH:MM:SS.MMM |
22 _LOG_HEADER = r'^%d \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d' % os.getpid() | 23 _LOG_HEADER = r'^%d \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d' % os.getpid() |
23 _LOG_HEADER_PID = r'^\d+ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d' | 24 _LOG_HEADER_PID = r'^\d+ \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d' |
24 | 25 |
25 | 26 |
26 _PHASE = 'LOGGING_UTILS_TESTS_PHASE' | 27 _PHASE = 'LOGGING_UTILS_TESTS_PHASE' |
27 | 28 |
28 | 29 |
29 def call(phase, cwd): | 30 def call(phase, cwd): |
30 """Calls itself back.""" | 31 """Calls itself back.""" |
31 env = os.environ.copy() | 32 env = os.environ.copy() |
32 env[_PHASE] = phase | 33 env[_PHASE] = phase |
33 return subprocess.call([sys.executable, '-u', THIS_FILE], env=env, cwd=cwd) | 34 return subprocess.call([sys.executable, '-u', THIS_FILE], env=env, cwd=cwd) |
34 | 35 |
35 | 36 |
36 class Test(unittest.TestCase): | 37 class Test(unittest.TestCase): |
37 def setUp(self): | 38 def setUp(self): |
38 super(Test, self).setUp() | 39 super(Test, self).setUp() |
39 self.tmp = tempfile.mkdtemp(prefix='logging_utils') | 40 self.tmp = tempfile.mkdtemp(prefix='logging_utils') |
40 | 41 |
41 def tearDown(self): | 42 def tearDown(self): |
42 try: | 43 try: |
43 shutil.rmtree(self.tmp) | 44 file_path.rmtree(self.tmp) |
44 finally: | 45 finally: |
45 super(Test, self).tearDown() | 46 super(Test, self).tearDown() |
46 | 47 |
47 def test_capture(self): | 48 def test_capture(self): |
48 root = logging.RootLogger(logging.DEBUG) | 49 root = logging.RootLogger(logging.DEBUG) |
49 with logging_utils.CaptureLogs('foo', root) as log: | 50 with logging_utils.CaptureLogs('foo', root) as log: |
50 root.debug('foo') | 51 root.debug('foo') |
51 result = log.read() | 52 result = log.read() |
52 expected = _LOG_HEADER + ': DEBUG foo\n$' | 53 expected = _LOG_HEADER + ': DEBUG foo\n$' |
53 if sys.platform == 'win32': | 54 if sys.platform == 'win32': |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 def main(): | 108 def main(): |
108 phase = os.environ.get(_PHASE) | 109 phase = os.environ.get(_PHASE) |
109 if phase: | 110 if phase: |
110 return getattr(sys.modules[__name__], phase)() | 111 return getattr(sys.modules[__name__], phase)() |
111 verbose = '-v' in sys.argv | 112 verbose = '-v' in sys.argv |
112 logging.basicConfig(level=logging.DEBUG if verbose else logging.ERROR) | 113 logging.basicConfig(level=logging.DEBUG if verbose else logging.ERROR) |
113 unittest.main() | 114 unittest.main() |
114 | 115 |
115 | 116 |
116 if __name__ == '__main__': | 117 if __name__ == '__main__': |
| 118 fix_encoding.fix_encoding() |
117 sys.exit(main()) | 119 sys.exit(main()) |
OLD | NEW |