Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the log level when capturing logs in tests. #5418

Merged
merged 2 commits into from Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions tests/python/pants_test/base_test.py
Expand Up @@ -483,9 +483,16 @@ def warnings(self):
return self._messages_for_level('WARNING')

@contextmanager
def captured_logging(self):
def captured_logging(self, level=None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could make more sense to default to level=logging.INFO here vs NOTSET below?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm always maybe unjustifiably worried about someone then saying captured_logging(level=None) so I ~always defer the conversion. This probably negatively impacts pydoc though.

root_logger = logging.getLogger()

old_level = root_logger.level
root_logger.setLevel(level or logging.NOTSET)

handler = self.LoggingRecorder()
logger = logging.getLogger('')
logger.addHandler(handler)
yield handler
logger.removeHandler(handler)
root_logger.addHandler(handler)
try:
yield handler
finally:
root_logger.setLevel(old_level)
root_logger.removeHandler(handler)
4 changes: 3 additions & 1 deletion tests/python/pants_test/pantsd/test_process_manager.py
Expand Up @@ -6,13 +6,15 @@
unicode_literals, with_statement)

import errno
import logging
import os
import sys
from contextlib import contextmanager

import mock
import psutil

from pants.pantsd import process_manager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this new import is unused/redundant, afaict

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yup - thanks.

from pants.pantsd.process_manager import (ProcessGroup, ProcessManager, ProcessMetadataManager,
swallow_psutil_exceptions)
from pants.util.contextutil import temporary_dir
Expand Down Expand Up @@ -141,7 +143,7 @@ def test_readwrite_metadata_by_name(self):

def test_deadline_until(self):
with self.assertRaises(self.pmm.Timeout):
with self.captured_logging() as captured:
with self.captured_logging(logging.INFO) as captured:
self.pmm._deadline_until(lambda: False, 'the impossible', timeout=.5, info_interval=.1)
self.assertTrue(4 <= len(captured.infos()) <= 6,
'Expected between 4 and 6 infos, got: {}'.format(captured.infos()))
Expand Down
6 changes: 4 additions & 2 deletions tests/python/pants_test/util/test_osutil.py
Expand Up @@ -5,6 +5,8 @@
from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
unicode_literals, with_statement)

import logging

from pants.util.osutil import OS_ALIASES, known_os_names, normalize_os_name
from pants_test.base_test import BaseTest

Expand All @@ -22,14 +24,14 @@ def test_keys_in_aliases(self):

def test_no_warnings_on_known_names(self):
for name in known_os_names():
with self.captured_logging() as captured:
with self.captured_logging(logging.WARNING) as captured:
normalize_os_name(name)
self.assertEqual(0, len(captured.warnings()),
'Recieved unexpected warnings: {}'.format(captured.warnings()))

def test_warnings_on_unknown_names(self):
name = 'I really hope no one ever names an operating system with this string.'
with self.captured_logging() as captured:
with self.captured_logging(logging.WARNING) as captured:
normalize_os_name(name)
self.assertEqual(1, len(captured.warnings()),
'Expected exactly one warning, but got: {}'.format(captured.warnings()))