Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / test_util.py
blob95db17651fe45a8927e77ed5dc30ebfd2758b4a4
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
3 # found in the LICENSE file.
5 from __future__ import print_function
7 import logging
8 import os
9 import sys
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)
21 cf = Capture()
22 logging.getLogger('').addFilter(cf)
23 f()
24 logging.getLogger('').removeFilter(cf)
26 return output
29 def EnableLogging(name):
30 '''Returns the output of the log with |name| to stdout.
31 '''
33 return _ReplaceLogging(name, lambda message, *args: print(message % args))
36 def DisableLogging(name):
37 '''Disables the log with |name| for the duration of the decorated function.
38 '''
39 return _ReplaceLogging(name, lambda _, *args: None)
42 def _ReplaceLogging(name, replacement):
43 def decorator(fn):
44 def impl(*args, **optargs):
45 saved = getattr(logging, name)
46 setattr(logging, name, replacement)
47 try:
48 return fn(*args, **optargs)
49 finally:
50 setattr(logging, name, saved)
51 return impl
52 return decorator
55 def ChromiumPath(*path):
56 return os.path.join(
57 sys.path[0], '..', '..', '..', '..', '..', *path)
60 def ReadFile(*path, **read_args):
61 with open(ChromiumPath(*path), **read_args) as f:
62 return f.read()