コード例 #1
0
def test_configure(temp_dir_cwd, capsys, caplog):
    log_file = Path('file.log')
    stderr_handler, file_handler = logging_.configure(log_file)
    assert isinstance(stderr_handler, logging.StreamHandler)
    assert isinstance(file_handler, logging.FileHandler)
    
    logger = logging.getLogger(__name__)
    logger.info('Fake info')
    logger.debug('Ignored debug')
    logger.setLevel(logging.DEBUG)
    logger.debug('Fake debug')
    
    # stderr
    #
    # - level is INFO
    # - terse format
    expected = 'I: Fake info\n'
    actual = capsys.readouterr()[1]
    assert_text_equals(actual, expected)
    
    # log file
    #
    # - level is DEBUG
    # - long format with fairly unambiguous source
    log_file_content = path_.read(log_file)
    def prefix(log_type, package, module_name):
        return r'{} [0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}} [0-9]{{2}}:[0-9]{{2}}:[0-9]{{2}},[0-9]{{3}} {} \({}:[0-9]+\):'.format(
            log_type, package, module_name
        )
    pattern = dedent('''\
        {}
        Fake info
         
        {}
        Fake debug
        '''
        .format(
            prefix('I', __name__, 'test_logging'),
            prefix('D', __name__, 'test_logging'),
        )
    )
    assert re.match(pattern, log_file_content, re.MULTILINE), 'Actual:{}\n\nExpected to match:\n{}'.format(log_file_content, pattern)
    
コード例 #2
0
def test_read(path, contents):
    with path.open('w') as f:
        f.write(contents)
    assert path_.read(path) == contents