Example #1
0
    def test_pickle(self, file):
        """
        Can be pickled and unpickled for stdout and stderr.

        Can't compare output because capsys et all would confuse the logic.
        """
        pl = PrintLogger(file=file)

        assert pl._file is pickle.loads(pickle.dumps(pl))._file
        assert pl._lock is pickle.loads(pickle.dumps(pl))._lock
Example #2
0
    def test_prints_to_stdout_by_default(self, capsys):
        """
        Instantiating without arguments gives conveniently a logger to standard
        out.
        """
        PrintLogger().msg("hello")

        out, err = capsys.readouterr()
        assert "hello\n" == out
        assert "" == err
Example #3
0
    def test_deepcopy(self, capsys):
        """
        Deepcopied PrintLogger works.
        """
        copied_logger = copy.deepcopy(PrintLogger())
        copied_logger.msg("hello")

        out, err = capsys.readouterr()
        assert "hello\n" == out
        assert "" == err
Example #4
0
    def test_pickle_not_stdout_stderr(self, tmpdir, proto):
        """
        PrintLoggers with differnt files than stdout/stderr raise a
        PickingError.
        """
        f = tmpdir.join("file.log")
        f.write("")
        pl = PrintLogger(file=f.open())

        with pytest.raises(pickle.PicklingError, match="Only PrintLoggers to"):
            pickle.dumps(pl, proto)
Example #5
0
    def test_lock(self):
        """
        Creating a logger adds a lock to WRITE_LOCKS.
        """
        sio = StringIO()

        assert sio not in WRITE_LOCKS

        PrintLogger(sio)

        assert sio in WRITE_LOCKS
Example #6
0
 def test_prints_to_correct_file(self, tmpdir, capsys):
     """
     Supplied files are respected.
     """
     f = tmpdir.join('test.log')
     fo = f.open('w')
     PrintLogger(fo).msg('hello')
     out, err = capsys.readouterr()
     assert '' == out == err
     fo.close()
     assert 'hello\n' == f.read()
Example #7
0
    def test_prints_to_correct_file(self, tmpdir, capsys):
        """
        Supplied files are respected.
        """
        f = tmpdir.join("test.log")
        fo = f.open("w")
        PrintLogger(fo).msg("hello")
        out, err = capsys.readouterr()

        assert "" == out == err
        fo.close()
        assert "hello\n" == f.read()
Example #8
0
 def test_repr(self):
     """
     __repr__ makes sense.
     """
     assert repr(PrintLogger()).startswith("<PrintLogger(file=")