diff --git a/tests/conftest.py b/tests/conftest.py index 52662964..dc0abf41 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,22 @@ +# Copyright 2023 Alethea Katherine Flowers +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import annotations +import re +from pathlib import Path +from string import Template +from typing import Callable + import pytest @@ -6,3 +25,28 @@ def reset_color_envvars(monkeypatch): """Remove color-related envvars to fix test output""" monkeypatch.delenv("FORCE_COLOR", raising=False) monkeypatch.delenv("NO_COLOR", raising=False) + +RESOURCES = Path(__file__).parent.joinpath("resources") + + +@pytest.fixture +def generate_noxfile_options(tmp_path: Path) -> Callable[..., str]: + """Generate noxfile.py with test and templated options. + + The options are enabled (if disabled) and the values are applied + if a matching format string is encountered with the option name. + """ + + def generate_noxfile(**option_mapping: str | bool) -> str: + path = Path(RESOURCES) / "noxfile_options.py" + text = path.read_text(encoding="utf8") + if option_mapping: + for opt, _val in option_mapping.items(): + # "uncomment" options with values provided + text = re.sub(rf"(# )?nox.options.{opt}", f"nox.options.{opt}", text) + text = Template(text).safe_substitute(**option_mapping) + path = tmp_path / "noxfile.py" + path.write_text(text) + return str(path) + + return generate_noxfile diff --git a/tests/test_main.py b/tests/test_main.py index 8b97da1e..a30e7efb 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -16,10 +16,8 @@ import contextlib import os -import re import sys from pathlib import Path -from string import Template from unittest import mock import pytest @@ -551,29 +549,6 @@ def test_main_noxfile_options_sessions(monkeypatch, generate_noxfile_options): assert config.sessions == ["test"] -@pytest.fixture -def generate_noxfile_options(tmp_path): - """Generate noxfile.py with test and templated options. - - The options are enabled (if disabled) and the values are applied - if a matching format string is encountered with the option name. - """ - - def generate_noxfile(**option_mapping: str | bool): - path = Path(RESOURCES) / "noxfile_options.py" - text = path.read_text(encoding="utf8") - if option_mapping: - for opt, _val in option_mapping.items(): - # "uncomment" options with values provided - text = re.sub(rf"(# )?nox.options.{opt}", f"nox.options.{opt}", text) - text = Template(text).safe_substitute(**option_mapping) - path = tmp_path / "noxfile.py" - path.write_text(text) - return str(path) - - return generate_noxfile - - @pytest.fixture def generate_noxfile_options_pythons(tmp_path): """Generate noxfile.py with test and launch_rocket sessions. diff --git a/tests/test_tasks.py b/tests/test_tasks.py index 4627b9f2..947239ed 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -24,7 +24,6 @@ from unittest import mock import pytest -from test_main import generate_noxfile_options # noqa: F401 import nox from nox import _options, sessions, tasks @@ -321,10 +320,7 @@ def quux(): assert "Tag selection caused no sessions to be selected." in caplog.text -def test_merge_sessions_and_tags( - reset_global_nox_options, - generate_noxfile_options, # noqa: F811 -): +def test_merge_sessions_and_tags(reset_global_nox_options, generate_noxfile_options): @nox.session(tags=["foobar"]) def test(): pass