Skip to content

Commit 55c7eaf

Browse files
authored
feat: support default=False (#810)
Signed-off-by: Henry Schreiner <[email protected]>
1 parent 9a4068f commit 55c7eaf

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

docs/config.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ with ``@nox.session``. For example:
2626
2727
You can also configure sessions to run against multiple Python versions as described in :ref:`virtualenv config` and parametrize sessions as described in :ref:`parametrized sessions <parametrized>`.
2828

29+
By default, all sessions will be run if no sessions are specified. You can
30+
remove sessions from this default list by passing ``default=False`` in the
31+
``@nox.session(...)`` decorator. You can also specify a list of sessions to run by
32+
default using the ``nox.options.sessions = [...]`` configuration option.
2933

3034
Session description
3135
-------------------
@@ -421,7 +425,7 @@ Nox has various :doc:`command line arguments <usage>` that can be used to modify
421425
def tests(session):
422426
...
423427
424-
Or, if you wanted to provide a set of sessions that are run by default:
428+
Or, if you wanted to provide a set of sessions that are run by default (this overrides the ``default=`` argument to sessions):
425429

426430
.. code-block:: python
427431

nox/_decorators.py

+5
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ def __init__(
6969
venv_params: Any = None,
7070
should_warn: Mapping[str, Any] | None = None,
7171
tags: Sequence[str] | None = None,
72+
*,
73+
default: bool = True,
7274
) -> None:
7375
self.func = func
7476
self.python = python
@@ -78,6 +80,7 @@ def __init__(
7880
self.venv_params = venv_params
7981
self.should_warn = dict(should_warn or {})
8082
self.tags = list(tags or [])
83+
self.default = default
8184

8285
def __call__(self, *args: Any, **kwargs: Any) -> Any:
8386
return self.func(*args, **kwargs)
@@ -94,6 +97,7 @@ def copy(self, name: str | None = None) -> Func:
9497
self.venv_params,
9598
self.should_warn,
9699
self.tags,
100+
default=self.default,
97101
)
98102

99103

@@ -125,6 +129,7 @@ def __init__(self, func: Func, param_spec: Param) -> None:
125129
func.venv_params,
126130
func.should_warn,
127131
func.tags,
132+
default=func.default,
128133
)
129134
self.call_spec = call_spec
130135
self.session_signature = session_signature

nox/manifest.py

+5
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ def filter_by_name(self, specified_sessions: Iterable[str]) -> None:
155155
if missing_sessions:
156156
raise KeyError(f"Sessions not found: {', '.join(missing_sessions)}")
157157

158+
def filter_by_default(self) -> None:
159+
"""Filter sessions in the queue based on the default flag."""
160+
161+
self._queue = [x for x in self._queue if x.func.default]
162+
158163
def filter_by_python_interpreter(self, specified_pythons: Sequence[str]) -> None:
159164
"""Filter sessions in the queue based on the user-specified
160165
python interpreter versions.

nox/registry.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def session_decorator(
4343
venv_backend: Any | None = ...,
4444
venv_params: Any | None = ...,
4545
tags: Sequence[str] | None = ...,
46+
*,
47+
default: bool = ...,
4648
) -> Callable[[F], F]:
4749
...
4850

@@ -56,6 +58,8 @@ def session_decorator(
5658
venv_backend: Any | None = None,
5759
venv_params: Any | None = None,
5860
tags: Sequence[str] | None = None,
61+
*,
62+
default: bool = True,
5963
) -> F | Callable[[F], F]:
6064
"""Designate the decorated function as a session."""
6165
# If `func` is provided, then this is the decorator call with the function
@@ -75,6 +79,7 @@ def session_decorator(
7579
venv_backend=venv_backend,
7680
venv_params=venv_params,
7781
tags=tags,
82+
default=default,
7883
)
7984

8085
if py is not None and python is not None:
@@ -88,7 +93,14 @@ def session_decorator(
8893

8994
final_name = name or func.__name__
9095
fn = Func(
91-
func, python, reuse_venv, final_name, venv_backend, venv_params, tags=tags
96+
func,
97+
python,
98+
reuse_venv,
99+
final_name,
100+
venv_backend,
101+
venv_params,
102+
tags=tags,
103+
default=default,
92104
)
93105
_REGISTRY[final_name] = fn
94106
return fn

nox/tasks.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ def filter_manifest(manifest: Manifest, global_config: Namespace) -> Manifest |
173173
# This can raise KeyError if a specified session does not exist;
174174
# log this if it happens. The sessions does not come from the Noxfile
175175
# if keywords is not empty.
176-
if global_config.sessions is not None:
176+
if global_config.sessions is None:
177+
manifest.filter_by_default()
178+
else:
177179
try:
178180
manifest.filter_by_name(global_config.sessions)
179181
except KeyError as exc:

tests/test_tasks.py

+36
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def session_func():
4040
session_func.venv_backend = None
4141
session_func.should_warn = {}
4242
session_func.tags = []
43+
session_func.default = True
4344

4445

4546
def session_func_with_python():
@@ -48,6 +49,7 @@ def session_func_with_python():
4849

4950
session_func_with_python.python = "3.8"
5051
session_func_with_python.venv_backend = None
52+
session_func_with_python.default = True
5153

5254

5355
def session_func_venv_pythons_warning():
@@ -346,6 +348,40 @@ def bar():
346348
assert len(manifest) == 2
347349

348350

351+
@pytest.mark.parametrize("selection", [None, ["qux"], ["quuz"], ["qux", "quuz"]])
352+
def test_default_false(selection):
353+
@nox.session()
354+
def qux():
355+
pass
356+
357+
@nox.session()
358+
def quux():
359+
pass
360+
361+
@nox.session(default=False)
362+
def quuz():
363+
pass
364+
365+
@nox.session(default=False)
366+
def corge():
367+
pass
368+
369+
config = _options.options.namespace(sessions=selection, pythons=(), posargs=[])
370+
manifest = Manifest(
371+
{
372+
"qux": qux,
373+
"quux": quux,
374+
"quuz": quuz,
375+
"corge": corge,
376+
},
377+
config,
378+
)
379+
return_value = tasks.filter_manifest(manifest, config)
380+
assert return_value is manifest
381+
expected = 2 if selection is None else len(selection)
382+
assert len(manifest) == expected
383+
384+
349385
def test_honor_list_request_noop():
350386
config = _options.options.namespace(list_sessions=False)
351387
manifest = {"thing": mock.sentinel.THING}

0 commit comments

Comments
 (0)