Skip to content

Tests fail when NO_COLOR=1 is set in the environment #708

@mgorny

Description

@mgorny

Current Behavior

We're running the test suite with NO_COLOR set in the containing environment to prevent tools from outputting ANSI color codes to build logs. However, this causes nox tests to fail, seemingly causing it to conflict with --force-color option used by a test:

============================================================== FAILURES ===============================================================
_______________________________________________ test_main_color_from_isatty[True-True] ________________________________________________

monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f7c74791b40>, isatty_value = True, expected = True

    @pytest.mark.parametrize(("isatty_value", "expected"), [(True, True), (False, False)])
    def test_main_color_from_isatty(monkeypatch, isatty_value, expected):
        monkeypatch.delenv("FORCE_COLOR", raising=False)
        monkeypatch.setattr(sys, "argv", [sys.executable])
        with mock.patch("nox.workflow.execute") as execute:
            execute.return_value = 0
            with mock.patch("sys.stdout.isatty") as isatty:
                isatty.return_value = isatty_value
    
                # Call the main function.
                with mock.patch.object(sys, "exit"):
                    nox.__main__.main()
    
                config = execute.call_args[1]["global_config"]
>               assert config.color == expected
E               AssertionError: assert False == True
E                +  where False = Namespace(help=None, version=None, list_sessions=None, json=None, sessions=None, pythons=None, keywords=None, tags=None, posargs=[], verbose=None, add_timestamp=None, default_venv_backend=None, force_venv_backend=None, no_venv=False, reuse_existing_virtualenvs=False, no_reuse_existing_virtualenvs=None, R=False, noxfile='noxfile.py', envdir=None, extra_pythons=None, force_pythons=None, stop_on_first_error=False, no_stop_on_first_error=None, error_on_missing_interpreters=False, no_error_on_missing_interpreters=None, error_on_external_run=False, no_error_on_external_run=None, install_only=None, no_install=False, report=None, non_interactive=None, nocolor=True, forcecolor=False, color=False, invoked_from='/tmp/portage/dev-python/nox-2023.04.22/temp/pytest-of-portage/pytest-0/test_main_noxfile_options_with1').color

config     = Namespace(help=None, version=None, list_sessions=None, json=None, sessions=None, pythons=None, keywords=None, tags=None, posargs=[], verbose=None, add_timestamp=None, default_venv_backend=None, force_venv_backend=None, no_venv=False, reuse_existing_virtualenvs=False, no_reuse_existing_virtualenvs=None, R=False, noxfile='noxfile.py', envdir=None, extra_pythons=None, force_pythons=None, stop_on_first_error=False, no_stop_on_first_error=None, error_on_missing_interpreters=False, no_error_on_missing_interpreters=None, error_on_external_run=False, no_error_on_external_run=None, install_only=None, no_install=False, report=None, non_interactive=None, nocolor=True, forcecolor=False, color=False, invoked_from='/tmp/portage/dev-python/nox-2023.04.22/temp/pytest-of-portage/pytest-0/test_main_noxfile_options_with1')
execute    = <MagicMock name='execute' id='140172506709584'>
expected   = True
isatty     = <MagicMock name='isatty' id='140172508461184'>
isatty_value = True
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f7c74791b40>

/tmp/portage/dev-python/nox-2023.04.22/work/nox-2023.04.22/tests/test_main.py:640: AssertionError
_____________________________________________ test_main_color_options[--forcecolor-True] ______________________________________________

monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f7c7494f4c0>, color_opt = '--forcecolor', expected = True

    @pytest.mark.parametrize(
        ("color_opt", "expected"),
        [
            ("--forcecolor", True),
            ("--nocolor", False),
            ("--force-color", True),
            ("--no-color", False),
        ],
    )
    def test_main_color_options(monkeypatch, color_opt, expected):
        monkeypatch.delenv("FORCE_COLOR", raising=False)
        monkeypatch.setattr(sys, "argv", [sys.executable, color_opt])
        with mock.patch("nox.workflow.execute") as execute:
            execute.return_value = 0
    
            # Call the main function.
            with mock.patch.object(sys, "exit"):
                nox.__main__.main()
    
            config = execute.call_args[1]["global_config"]
>           assert config.color == expected
E           AssertionError: assert None == True
E            +  where None = Namespace(help=None, version=None, list_sessions=None, json=None, sessions=None, pythons=None, keywords=None, tags=None, posargs=[], verbose=None, add_timestamp=None, default_venv_backend=None, force_venv_backend=None, no_venv=False, reuse_existing_virtualenvs=False, no_reuse_existing_virtualenvs=None, R=False, noxfile='noxfile.py', envdir=None, extra_pythons=None, force_pythons=None, stop_on_first_error=False, no_stop_on_first_error=None, error_on_missing_interpreters=False, no_error_on_missing_interpreters=None, error_on_external_run=False, no_error_on_external_run=None, install_only=None, no_install=False, report=None, non_interactive=None, nocolor=True, forcecolor=True, color=None).color

color_opt  = '--forcecolor'
config     = Namespace(help=None, version=None, list_sessions=None, json=None, sessions=None, pythons=None, keywords=None, tags=None, posargs=[], verbose=None, add_timestamp=None, default_venv_backend=None, force_venv_backend=None, no_venv=False, reuse_existing_virtualenvs=False, no_reuse_existing_virtualenvs=None, R=False, noxfile='noxfile.py', envdir=None, extra_pythons=None, force_pythons=None, stop_on_first_error=False, no_stop_on_first_error=None, error_on_missing_interpreters=False, no_error_on_missing_interpreters=None, error_on_external_run=False, no_error_on_external_run=None, install_only=None, no_install=False, report=None, non_interactive=None, nocolor=True, forcecolor=True, color=None)
execute    = <MagicMock name='execute' id='140172524274592'>
expected   = True
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f7c7494f4c0>

/tmp/portage/dev-python/nox-2023.04.22/work/nox-2023.04.22/tests/test_main.py:663: AssertionError
-------------------------------------------------------- Captured stderr call ---------------------------------------------------------
usage: python3.10 [-h] [--version] [-l] [--json] [-s [SESSIONS ...]] [-p [PYTHONS ...]] [-k KEYWORDS] [-t [TAGS ...]] [-v] [-ts]
                  [-db {none,virtualenv,conda,mamba,venv}] [-fb {none,virtualenv,conda,mamba,venv}] [--no-venv] [-r] [-N] [-R]
                  [-f NOXFILE] [--envdir ENVDIR] [--extra-pythons [EXTRA_PYTHONS ...]] [--force-pythons [FORCE_PYTHONS ...]] [-x]
                  [--no-stop-on-first-error] [--error-on-missing-interpreters] [--no-error-on-missing-interpreters]
                  [--error-on-external-run] [--no-error-on-external-run] [--install-only] [--no-install] [--report REPORT]
                  [--non-interactive] [--nocolor] [--forcecolor]
                  ...
python3.10: error: Can not specify both --no-color and --force-color.
_____________________________________________ test_main_color_options[--force-color-True] _____________________________________________

monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f7c74954250>, color_opt = '--force-color', expected = True

    @pytest.mark.parametrize(
        ("color_opt", "expected"),
        [
            ("--forcecolor", True),
            ("--nocolor", False),
            ("--force-color", True),
            ("--no-color", False),
        ],
    )
    def test_main_color_options(monkeypatch, color_opt, expected):
        monkeypatch.delenv("FORCE_COLOR", raising=False)
        monkeypatch.setattr(sys, "argv", [sys.executable, color_opt])
        with mock.patch("nox.workflow.execute") as execute:
            execute.return_value = 0
    
            # Call the main function.
            with mock.patch.object(sys, "exit"):
                nox.__main__.main()
    
            config = execute.call_args[1]["global_config"]
>           assert config.color == expected
E           AssertionError: assert None == True
E            +  where None = Namespace(help=None, version=None, list_sessions=None, json=None, sessions=None, pythons=None, keywords=None, tags=None, posargs=[], verbose=None, add_timestamp=None, default_venv_backend=None, force_venv_backend=None, no_venv=False, reuse_existing_virtualenvs=False, no_reuse_existing_virtualenvs=None, R=False, noxfile='noxfile.py', envdir=None, extra_pythons=None, force_pythons=None, stop_on_first_error=False, no_stop_on_first_error=None, error_on_missing_interpreters=False, no_error_on_missing_interpreters=None, error_on_external_run=False, no_error_on_external_run=None, install_only=None, no_install=False, report=None, non_interactive=None, nocolor=True, forcecolor=True, color=None).color

color_opt  = '--force-color'
config     = Namespace(help=None, version=None, list_sessions=None, json=None, sessions=None, pythons=None, keywords=None, tags=None, posargs=[], verbose=None, add_timestamp=None, default_venv_backend=None, force_venv_backend=None, no_venv=False, reuse_existing_virtualenvs=False, no_reuse_existing_virtualenvs=None, R=False, noxfile='noxfile.py', envdir=None, extra_pythons=None, force_pythons=None, stop_on_first_error=False, no_stop_on_first_error=None, error_on_missing_interpreters=False, no_error_on_missing_interpreters=None, error_on_external_run=False, no_error_on_external_run=None, install_only=None, no_install=False, report=None, non_interactive=None, nocolor=True, forcecolor=True, color=None)
execute    = <MagicMock name='execute' id='140172506707280'>
expected   = True
monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f7c74954250>

/tmp/portage/dev-python/nox-2023.04.22/work/nox-2023.04.22/tests/test_main.py:663: AssertionError
-------------------------------------------------------- Captured stderr call ---------------------------------------------------------
usage: python3.10 [-h] [--version] [-l] [--json] [-s [SESSIONS ...]] [-p [PYTHONS ...]] [-k KEYWORDS] [-t [TAGS ...]] [-v] [-ts]
                  [-db {none,virtualenv,conda,mamba,venv}] [-fb {none,virtualenv,conda,mamba,venv}] [--no-venv] [-r] [-N] [-R]
                  [-f NOXFILE] [--envdir ENVDIR] [--extra-pythons [EXTRA_PYTHONS ...]] [--force-pythons [FORCE_PYTHONS ...]] [-x]
                  [--no-stop-on-first-error] [--error-on-missing-interpreters] [--no-error-on-missing-interpreters]
                  [--error-on-external-run] [--no-error-on-external-run] [--install-only] [--no-install] [--report REPORT]
                  [--non-interactive] [--nocolor] [--forcecolor]
                  ...
python3.10: error: Can not specify both --no-color and --force-color.

Expected Behavior

Tests passing. Ideally, I think --force-color should override NO_COLOR in the containing environment rather than collide with it.

Steps To Reproduce

NO_COLOR=1 nox  # in nox's source tree ;-)

Environment

- OS: Gentoo Linux
- Python: 3.10.11
- Nox: 2023.04.23

Anything else?

Original bug report: https://bugs.gentoo.org/904948

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions