Skip to content

Commit

Permalink
Add test fixture to generate noxfile.py
Browse files Browse the repository at this point in the history
The noxfile.py is templated with the default session (`nox.options.sessions`),
and the default Python version (`nox.options.pythons`), as well as an alternate
Python version.

This allows us to avoid the situation where a test case running on one Python
version needs to launch Nox using another Python version. As a side-effect, it
makes the test cases a bit more explicit.
  • Loading branch information
cjolowicz committed Nov 13, 2020
1 parent 2ee6040 commit c0ece7d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion nox/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import os
import sys
from typing import Any, Iterable, Optional, Sequence, Union, List
from typing import Any, Iterable, List, Optional, Sequence, Union

import py
from nox.logger import logger
Expand Down
4 changes: 2 additions & 2 deletions nox/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import platform
import re
import shutil
from socket import gethostbyname
import sys
from typing import Any, Mapping, Optional, Tuple, Union, List
from socket import gethostbyname
from typing import Any, List, Mapping, Optional, Tuple, Union

import nox.command
import py
Expand Down
28 changes: 28 additions & 0 deletions tests/resources/noxfile_options_pythons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2020 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.

import nox

nox.options.sessions = ["{default_session}"]
nox.options.pythons = ["{default_python}"]


@nox.session(python=["{default_python}", "{alternate_python}"])
def test(session):
pass


@nox.session(python=["{default_python}", "{alternate_python}"])
def launch_rocket(session):
pass
1 change: 0 additions & 1 deletion tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from unittest import mock

import pytest

from nox import logger


Expand Down
25 changes: 25 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
import sys
from pathlib import Path
from unittest import mock

import contexter
Expand Down Expand Up @@ -444,6 +445,30 @@ def test_main_noxfile_options_sessions(monkeypatch):
assert config.sessions == ["test"]


@pytest.fixture
def generate_noxfile_options_pythons(tmp_path):
"""Generate noxfile.py with test and launch_rocket sessions.
The sessions are defined for both the default and alternate Python versions.
The ``default_session`` and ``default_python`` parameters determine what
goes into ``nox.options.sessions`` and ``nox.options.pythons``, respectively.
"""

def generate_noxfile(default_session, default_python, alternate_python):
path = Path(RESOURCES) / "noxfile_options_pythons.py"
text = path.read_text()
text = text.format(
default_session=default_session,
default_python=default_python,
alternate_python=alternate_python,
)
path = tmp_path / "noxfile.py"
path.write_text(text)
return str(path)

return generate_noxfile


@pytest.mark.parametrize(("isatty_value", "expected"), [(True, True), (False, False)])
def test_main_color_from_isatty(monkeypatch, isatty_value, expected):
monkeypatch.setattr(sys, "argv", [sys.executable])
Expand Down

0 comments on commit c0ece7d

Please sign in to comment.