diff --git a/azure-job.yml b/azure-job.yml index bf5cbe6..6bf3e06 100644 --- a/azure-job.yml +++ b/azure-job.yml @@ -32,5 +32,5 @@ jobs: - script: pip install -r requirements-ci.txt displayName: Install CI requirements - - script: PYTHONUNBUFFERED=1 pytest + - script: python -u -m pytest displayName: Run pytest (Python ${{ python.value.spec }}) diff --git a/conftest.py b/conftest.py index 7234e69..ffa6fca 100644 --- a/conftest.py +++ b/conftest.py @@ -36,6 +36,23 @@ from stdio_mgr import stdio_mgr +# is_stdout_buffered is copied from https://stackoverflow.com/a/49736559 +# Licensed CC-BY-SA 4.0 +# Author https://stackoverflow.com/users/528711/sparrowt +def is_stdout_buffered(): + # Print a single space + carriage return but no new-line + # (should have no visible effect) + print(" \r") + # If the file position is a positive integer then stdout is buffered + try: + pos = sys.stdout.tell() + if pos > 0: + return True + except IOError: # In some terminals tell() throws IOError if stdout is unbuffered + pass + return False + + @pytest.fixture(scope="session") def warnings_are_errors(pytestconfig): """Provide concise access to '-W error::Warning' CLI option.""" @@ -76,7 +93,7 @@ def enable_warnings_plugin(request): @pytest.fixture(scope="session") def unbufferedio(): """Provide concise access to PYTHONUNBUFFERED envvar.""" - return os.environ.get("PYTHONUNBUFFERED") + return os.environ.get("PYTHONUNBUFFERED") or not is_stdout_buffered() @pytest.fixture(autouse=True)