Commit 16ae8fd4 authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

logging: reformat fatal_test.py with black

Summary: Reformat fatal_test.py with black (https://github.com/ambv/black)

Reviewed By: mnv104

Differential Revision: D8817268

fbshipit-source-id: b642496ac61e3b2120b76d9b234c29bf51603651
parent 7af069c3
...@@ -23,90 +23,89 @@ import unittest ...@@ -23,90 +23,89 @@ import unittest
class FatalTests(unittest.TestCase): class FatalTests(unittest.TestCase):
def setUp(self): def setUp(self):
fatal_helper_env = os.environ.get('FOLLY_FATAL_HELPER') fatal_helper_env = os.environ.get("FOLLY_FATAL_HELPER")
if fatal_helper_env: if fatal_helper_env:
self.helper = fatal_helper_env self.helper = fatal_helper_env
else: else:
build_dir = os.path.join(os.getcwd(), 'buck-out', 'gen') build_dir = os.path.join(os.getcwd(), "buck-out", "gen")
self.helper = os.path.join( self.helper = os.path.join(
build_dir, 'folly', 'logging', 'test', 'fatal_helper' build_dir, "folly", "logging", "test", "fatal_helper"
) )
def run_helper(self, *args, **kwargs): def run_helper(self, *args, **kwargs):
''' """
Run the helper. Run the helper.
Check that it crashes with SIGABRT and prints nothing on stdout. Check that it crashes with SIGABRT and prints nothing on stdout.
Returns the data printed to stderr. Returns the data printed to stderr.
''' """
env = kwargs.pop('env', None) env = kwargs.pop("env", None)
if kwargs: if kwargs:
raise TypeError('unexpected keyword arguments: %r' % raise TypeError("unexpected keyword arguments: %r" % (list(kwargs.keys())))
(list(kwargs.keys())))
cmd = [self.helper] cmd = [self.helper]
cmd.extend(args) cmd.extend(args)
p = subprocess.Popen(cmd, p = subprocess.Popen(
stdout=subprocess.PIPE, cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
stderr=subprocess.PIPE, )
env=env)
out, err = p.communicate() out, err = p.communicate()
status = p.returncode status = p.returncode
self.assertEqual(status, -signal.SIGABRT) self.assertEqual(status, -signal.SIGABRT)
self.assertEqual(out, b'') self.assertEqual(out, b"")
return err return err
def get_crash_regex(self, msg=b'test program crashing!', glog=True): def get_crash_regex(self, msg=b"test program crashing!", glog=True):
if glog: if glog:
prefix = br'^C[0-9]{4} .* FatalHelper.cpp:[0-9]+\] ' prefix = br"^C[0-9]{4} .* FatalHelper.cpp:[0-9]+\] "
else: else:
prefix = br'^FATAL:.*FatalHelper.cpp:[0-9]+: ' prefix = br"^FATAL:.*FatalHelper.cpp:[0-9]+: "
regex = prefix + re.escape(msg) + b'$' regex = prefix + re.escape(msg) + b"$"
return re.compile(regex, re.MULTILINE) return re.compile(regex, re.MULTILINE)
def test_no_crash(self): def test_no_crash(self):
# Simple sanity check that the program runs without # Simple sanity check that the program runs without
# crashing when requested # crashing when requested
subprocess.check_output([self.helper, '--crash=no']) subprocess.check_output([self.helper, "--crash=no"])
def test_async(self): def test_async(self):
handler_setings = 'default=stream:stream=stderr,async=true' handler_setings = "default=stream:stream=stderr,async=true"
err = self.run_helper('--logging=;' + handler_setings) err = self.run_helper("--logging=;" + handler_setings)
self.assertRegex(err, self.get_crash_regex()) self.assertRegex(err, self.get_crash_regex())
def test_immediate(self): def test_immediate(self):
handler_setings = 'default=stream:stream=stderr,async=false' handler_setings = "default=stream:stream=stderr,async=false"
err = self.run_helper('--logging=;' + handler_setings) err = self.run_helper("--logging=;" + handler_setings)
self.assertRegex(err, self.get_crash_regex()) self.assertRegex(err, self.get_crash_regex())
def test_none(self): def test_none(self):
# The fatal message should be printed directly to stderr when there # The fatal message should be printed directly to stderr when there
# are no logging handlers configured. # are no logging handlers configured.
err = self.run_helper('--logging=ERR:') err = self.run_helper("--logging=ERR:")
self.assertRegex(err, self.get_crash_regex(glog=False)) self.assertRegex(err, self.get_crash_regex(glog=False))
def test_other_category(self): def test_other_category(self):
err = self.run_helper('--category=foo.bar', err = self.run_helper("--category=foo.bar", "--logging", ".=FATAL")
'--logging', '.=FATAL')
regex = re.compile( regex = re.compile(
br'^C[0-9]{4} .* FatalHelper.cpp:[0-9]+\] ' br"^C[0-9]{4} .* FatalHelper.cpp:[0-9]+\] "
br'crashing to category foo\.bar$', br"crashing to category foo\.bar$",
re.MULTILINE) re.MULTILINE,
)
self.assertRegex(err, regex) self.assertRegex(err, regex)
def test_static_init(self): def test_static_init(self):
err = self.run_helper(env={'CRASH_DURING_INIT': '1'}) err = self.run_helper(env={"CRASH_DURING_INIT": "1"})
regex = self.get_crash_regex(br'crashing during static initialization') regex = self.get_crash_regex(br"crashing during static initialization")
self.assertRegex(err, regex) self.assertRegex(err, regex)
def test_static_destruction(self): def test_static_destruction(self):
err = self.run_helper('--crash=no', err = self.run_helper("--crash=no", env={"CRASH_DURING_INIT": "shutdown"})
env={'CRASH_DURING_INIT': 'shutdown'})
# When crashing during static destruction we may or may not see a # When crashing during static destruction we may or may not see a
# glog-formatted message. This depends on whether the crashing # glog-formatted message. This depends on whether the crashing
# destructor runs before or after the code that uninstalls the log # destructor runs before or after the code that uninstalls the log
# handlers, and it is valid for that to occur in either order. # handlers, and it is valid for that to occur in either order.
regex = re.compile(br'^(FATAL|C[0-9]{4}).*FatalHelper.cpp:.* ' regex = re.compile(
br'crashing during static destruction$', br"^(FATAL|C[0-9]{4}).*FatalHelper.cpp:.* "
re.MULTILINE) br"crashing during static destruction$",
re.MULTILINE,
)
self.assertRegex(err, regex) self.assertRegex(err, regex)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment