Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/common/extensions/docs/server2/test_util.py

Issue 22824042: Docserver: SidenavDataSource refactor, transition to DataSourceRegistry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleanup, deleted unused files/import Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/common/extensions/docs/server2/test_data/sidenav_data_source/test_sidenav.json ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from __future__ import print_function 5 from __future__ import print_function
6 6
7 import logging 7 import logging
8 import os 8 import os
9 import sys 9 import sys
10 10
11
12 def CaptureLogging(f):
13 '''Call the function |f|, capturing any logging output generated. |f| must
14 take no arguments. Returns a list of LogRecords that were emitted.
15 '''
16 output = []
17 class Capture(object):
18 def filter(self, record):
19 output.append(record)
20
21 cf = Capture()
22 logging.getLogger('').addFilter(cf)
23 f()
24 logging.getLogger('').removeFilter(cf)
25
26 return output
27
28
11 def EnableLogging(name): 29 def EnableLogging(name):
12 '''Returns the output of the log with |name| to stdout. 30 '''Returns the output of the log with |name| to stdout.
13 ''' 31 '''
32
14 return _ReplaceLogging(name, lambda message, *args: print(message % args)) 33 return _ReplaceLogging(name, lambda message, *args: print(message % args))
15 34
35
16 def DisableLogging(name): 36 def DisableLogging(name):
17 '''Disables the log with |name| for the duration of the decorated function. 37 '''Disables the log with |name| for the duration of the decorated function.
18 ''' 38 '''
19 return _ReplaceLogging(name, lambda _, *args: None) 39 return _ReplaceLogging(name, lambda _, *args: None)
20 40
41
21 def _ReplaceLogging(name, replacement): 42 def _ReplaceLogging(name, replacement):
22 def decorator(fn): 43 def decorator(fn):
23 def impl(*args, **optargs): 44 def impl(*args, **optargs):
24 saved = getattr(logging, name) 45 saved = getattr(logging, name)
25 setattr(logging, name, replacement) 46 setattr(logging, name, replacement)
26 try: 47 try:
27 return fn(*args, **optargs) 48 return fn(*args, **optargs)
28 finally: 49 finally:
29 setattr(logging, name, saved) 50 setattr(logging, name, saved)
30 return impl 51 return impl
31 return decorator 52 return decorator
32 53
54
33 # TODO(kalman): Use this everywhere. A lot of tests are doing this. 55 # TODO(kalman): Use this everywhere. A lot of tests are doing this.
34 def ReadFile(name): 56 def ReadFile(name):
35 with open(os.path.join(sys.path[0], os.pardir, os.pardir, name)) as f: 57 with open(os.path.join(sys.path[0], os.pardir, os.pardir, name)) as f:
36 return f.read() 58 return f.read()
OLDNEW
« no previous file with comments | « chrome/common/extensions/docs/server2/test_data/sidenav_data_source/test_sidenav.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698