| 30 | |
| 31 | @pytest.hookimpl(trylast=True) |
| 32 | def pytest_configure(config: Config) -> None: |
| 33 | if config.option.pastebin == "all": |
| 34 | tr = config.pluginmanager.getplugin("terminalreporter") |
| 35 | # If no terminal reporter plugin is present, nothing we can do here; |
| 36 | # this can happen when this function executes in a worker node |
| 37 | # when using pytest-xdist, for example. |
| 38 | if tr is not None: |
| 39 | # pastebin file will be UTF-8 encoded binary file. |
| 40 | config.stash[pastebinfile_key] = tempfile.TemporaryFile("w+b") |
| 41 | oldwrite = tr._tw.write |
| 42 | |
| 43 | def tee_write(s, **kwargs): |
| 44 | oldwrite(s, **kwargs) |
| 45 | if isinstance(s, str): |
| 46 | s = s.encode("utf-8") |
| 47 | config.stash[pastebinfile_key].write(s) |
| 48 | |
| 49 | tr._tw.write = tee_write |
| 50 | |
| 51 | |
| 52 | def pytest_unconfigure(config: Config) -> None: |