def test_deepcopy_no_stdout(self, tmp_path): """ Only BytesLoggers that log to stdout or stderr can be deepcopy-ed. """ p = tmp_path / "log.txt" with p.open(mode="wb") as f: logger = BytesLogger(f) logger.msg(b"hello") with pytest.raises(copy.error): copy.deepcopy(logger) assert "hello\n" == p.read_text()
def test_stdlib_methods_support(self, method): """ BytesLogger implements methods of stdlib loggers. """ sio = BytesIO() getattr(BytesLogger(sio), method)(b"hello") assert b"hello" in sio.getvalue()
def test_lock(self, sio): """ Creating a logger adds a lock to WRITE_LOCKS. """ assert sio not in WRITE_LOCKS BytesLogger(sio) assert sio in WRITE_LOCKS
def test_deepcopy(self, capsys): """ Deepcopied BytesLogger works. """ copied_logger = copy.deepcopy(BytesLogger()) copied_logger.msg(b"hello") out, err = capsys.readouterr() assert "hello\n" == out assert "" == err
def test_prints_to_stdout_by_default(self, capsys): """ Instantiating without arguments gives conveniently a logger to standard out. """ BytesLogger().msg(b"hell\xc3\xb6") out, err = capsys.readouterr() assert "hellö\n" == out assert "" == err
def test_pickle_not_stdout_stderr(self, tmpdir, proto): """ BytesLoggers with different files than stdout/stderr raise a PickingError. """ f = tmpdir.join("file.log") f.write("") pl = BytesLogger(file=f.open()) with pytest.raises(pickle.PicklingError, match="Only BytesLoggers to"): pickle.dumps(pl, proto)
def test_pickle(self, file, proto): """ Can be pickled and unpickled for stdout and stderr. Can't compare output because capsys et all would confuse the logic. """ pl = BytesLogger(file=file) rv = pickle.loads(pickle.dumps(pl, proto)) assert pl._file is rv._file assert pl._lock is rv._lock
def test_prints_to_correct_file(self, tmpdir, capsys): """ Supplied files are respected. """ f = tmpdir.join("test.log") fo = f.open("wb") BytesLogger(fo).msg(b"hello") out, err = capsys.readouterr() assert "" == out == err fo.close() assert "hello\n" == f.read()
def test_repr(self): """ __repr__ makes sense. """ assert repr(BytesLogger()).startswith("<BytesLogger(file=")