Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: use FORCE_COLOR=1 to ensure the test works #12036

Merged
merged 2 commits into from
Mar 2, 2024

Conversation

nedbat
Copy link
Contributor

@nedbat nedbat commented Mar 2, 2024

Subject: set the environment to get predictable test output

Feature or Bugfix

  • Bugfix

Purpose

Without this fix, one test fails when I run the test suite locally with tox:

tests/test_util/test_util_display.py::test_status_iterator_verbosity_0 FAILED [100%]

=================================== FAILURES ===================================
_______________________ test_status_iterator_verbosity_0 _______________________

app = <SphinxTestApp buildername='dummy'>
status = <_io.StringIO object at 0x10785afd0>
warning = <_io.StringIO object at 0x1190e3e30>

    @pytest.mark.sphinx('dummy')
    def test_status_iterator_verbosity_0(app, status, warning):
        logging.setup(app, status, warning)

        # test for status_iterator (verbosity=0)
        status.seek(0)
        status.truncate(0)
        yields = list(status_iterator(['hello', 'sphinx', 'world'], 'testing ... ',
                                      length=3, verbosity=0))
        output = strip_escseq(status.getvalue())
>       assert 'testing ... [ 33%] hello\r' in output
E       AssertionError: assert 'testing ... [ 33%] hello\r' in 'testing ... [ 33%] hello\ntesting ... [ 67%] sphinx\ntesting ... [100%] world\n\n'

tests/test_util/test_util_display.py:46: AssertionError
--------------------------- Captured stdout teardown ---------------------------
# testroot: root
# builder: dummy
# srcdir: /private/var/folders/6j/khn0mcrj35d1k3yylpl8zl080000gn/T/pytest-of-ned/pytest-63/root
# outdir: /private/var/folders/6j/khn0mcrj35d1k3yylpl8zl080000gn/T/pytest-of-ned/pytest-63/root/_build/dummy
# status:
testing ... [ 33%] hello
testing ... [ 67%] sphinx
testing ... [100%] world

The problem is that color_terminal() returns False because stdout is not a tty. The output then has \n instead of \r and the test assertion fails. By setting FORCE_COLOR=1 in the test, we remove the check on stdout, and the test produces predictable expected output.

@nedbat
Copy link
Contributor Author

nedbat commented Mar 2, 2024

BTW: this test could also use monkeypatch to reduce the amount of custom code: https://github.com/sphinx-doc/sphinx/blob/master/tests/test_builders/test_build.py#L142-L165

@picnixz
Copy link
Member

picnixz commented Mar 2, 2024

Ah yes, I fixed it locally because I'm running pytest from CLI via `pytest -m (and somehow it's not recognized as a TTY so colors are not properly set; I'm not sure whether this is a bug in the test suite itself or not).

But thanks for having a separate PR for this hotfix.

@picnixz picnixz merged commit 5745199 into sphinx-doc:master Mar 2, 2024
22 checks passed
@picnixz
Copy link
Member

picnixz commented Mar 2, 2024

Thank you

picnixz added a commit that referenced this pull request Mar 3, 2024
@nedbat nedbat deleted the nedbat/fix-status-test branch March 4, 2024 12:31
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 6, 2024
@AA-Turner AA-Turner added this to the 7.3.0 milestone Jul 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants