Skip to content

Commit

Permalink
chore: move generate_noxfile_options fixture to conftest.py
Browse files Browse the repository at this point in the history
  • Loading branch information
samypr100 authored and henryiii committed Feb 23, 2024
1 parent 397d89d commit 4eb4c4e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
44 changes: 44 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
25 changes: 0 additions & 25 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4eb4c4e

Please sign in to comment.