1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
|
# Copyright 2017 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 collections
from collections.abc import Sequence
from unittest import mock
import pytest
import nox
from nox._decorators import Func
from nox.manifest import (
WARN_PYTHONS_IGNORED,
KeywordLocals,
Manifest,
_normalize_arg,
_normalized_session_match,
_null_session_func,
)
def create_mock_sessions():
sessions = collections.OrderedDict()
sessions["foo"] = mock.Mock(spec=(), python=None, venv_backend=None, tags=["baz"])
sessions["bar"] = mock.Mock(
spec=(),
python=None,
venv_backend=None,
tags=["baz", "qux"],
)
return sessions
def create_mock_config():
cfg = mock.sentinel.MOCKED_CONFIG
cfg.force_venv_backend = None
cfg.default_venv_backend = None
cfg.extra_pythons = None
cfg.force_pythons = None
cfg.posargs = []
return cfg
def test__normalize_arg():
assert _normalize_arg('test(foo="bar")') == _normalize_arg('test(foo="bar")')
# In the case of SyntaxError it should fallback to string
assert (
_normalize_arg("datetime.datetime(1990; 2, 18),")
== "datetime.datetime(1990; 2, 18),"
)
def test__normalized_session_match():
session_mock = mock.MagicMock()
session_mock.signatures = ['test(foo="bar")']
assert _normalized_session_match("test(foo='bar')", session_mock)
def test_init():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
# Assert that basic properties look correctly.
assert len(manifest) == 2
assert manifest["foo"].func is sessions["foo"]
assert manifest["bar"].func is sessions["bar"]
def test_contains():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
# Establish that contains works pre-iteration.
assert "foo" in manifest
assert "bar" in manifest
assert "baz" not in manifest
# Establish that __contains__ works post-iteration.
for _session in manifest:
pass
assert "foo" in manifest
assert "bar" in manifest
assert "baz" not in manifest
# Establish that sessions themselves work.
assert manifest["foo"] in manifest
def test_getitem():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
# Establish that each session is present, and a made-up session
# is not.
assert manifest["foo"].func is sessions["foo"]
assert manifest["bar"].func is sessions["bar"]
with pytest.raises(KeyError):
manifest["baz"]
# Establish that the sessions are still present even after being
# consumed by iteration.
for _session in manifest:
pass
assert manifest["foo"].func is sessions["foo"]
assert manifest["bar"].func is sessions["bar"]
def test_iteration():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
# There should be two sessions in the queue.
assert len(manifest._queue) == 2
assert len(manifest._consumed) == 0
# The first item should be our "foo" session.
foo = next(manifest)
assert foo.func == sessions["foo"]
assert foo in manifest._consumed
assert foo not in manifest._queue
assert len(manifest._consumed) == 1
assert len(manifest._queue) == 1
# The .next() or .__next__() methods can be called directly according
# to Python's data model.
bar = manifest.next()
assert bar.func == sessions["bar"]
assert bar in manifest._consumed
assert bar not in manifest._queue
assert len(manifest._consumed) == 2
assert len(manifest._queue) == 0
# Continuing past the end raises StopIteration.
with pytest.raises(StopIteration):
manifest.__next__()
def test_len():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
assert len(manifest) == 2
for _session in manifest:
assert len(manifest) == 2
def test_filter_by_name():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
manifest.filter_by_name(("foo",))
assert "foo" in manifest
assert "bar" not in manifest
def test_filter_by_name_maintains_order():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
manifest.filter_by_name(("bar", "foo"))
assert [session.name for session in manifest] == ["bar", "foo"]
def test_filter_by_name_not_found():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
with pytest.raises(KeyError):
manifest.filter_by_name(("baz",))
def test_filter_by_python_interpreter():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
manifest["foo"].func.python = "3.8"
manifest["bar"].func.python = "3.7"
manifest.filter_by_python_interpreter(("3.8",))
assert "foo" in manifest
assert "bar" not in manifest
def test_filter_by_keyword():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
assert len(manifest) == 2
manifest.filter_by_keywords("foo or bar")
assert len(manifest) == 2
manifest.filter_by_keywords("foo")
assert len(manifest) == 1
# Match tags
manifest.filter_by_keywords("not baz")
assert len(manifest) == 0
@pytest.mark.parametrize(
"tags,session_count",
[
(["baz", "qux"], 2),
(["baz"], 2),
(["qux"], 1),
(["missing"], 0),
(["baz", "missing"], 2),
],
)
def test_filter_by_tags(tags: Sequence[str], session_count: int):
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
assert len(manifest) == 2
manifest.filter_by_tags(tags)
assert len(manifest) == session_count
def test_list_all_sessions_with_filter():
sessions = create_mock_sessions()
manifest = Manifest(sessions, create_mock_config())
assert len(manifest) == 2
manifest.filter_by_keywords("foo")
assert len(manifest) == 1
all_sessions = list(manifest.list_all_sessions())
assert len(all_sessions) == 2
# Only one should be marked as selected.
assert all_sessions[0][1] is True
assert all_sessions[1][1] is False
def test_add_session_plain():
manifest = Manifest({}, create_mock_config())
session_func = mock.Mock(spec=(), python=None, venv_backend=None)
for session in manifest.make_session("my_session", session_func):
manifest.add_session(session)
assert len(manifest) == 1
def test_add_session_multiple_pythons():
manifest = Manifest({}, create_mock_config())
def session_func():
pass
func = Func(session_func, python=["3.5", "3.6"])
for session in manifest.make_session("my_session", func):
manifest.add_session(session)
assert len(manifest) == 2
@pytest.mark.parametrize(
"python,extra_pythons,expected",
[
(None, [], [None]),
(None, ["3.8"], [None]),
(None, ["3.8", "3.9"], [None]),
(False, [], [False]),
(False, ["3.8"], [False]),
(False, ["3.8", "3.9"], [False]),
("3.5", [], ["3.5"]),
("3.5", ["3.8"], ["3.5", "3.8"]),
("3.5", ["3.8", "3.9"], ["3.5", "3.8", "3.9"]),
(["3.5", "3.9"], [], ["3.5", "3.9"]),
(["3.5", "3.9"], ["3.8"], ["3.5", "3.9", "3.8"]),
(["3.5", "3.9"], ["3.8", "3.4"], ["3.5", "3.9", "3.8", "3.4"]),
(["3.5", "3.9"], ["3.5", "3.9"], ["3.5", "3.9"]),
],
)
def test_extra_pythons(python, extra_pythons, expected):
cfg = create_mock_config()
cfg.extra_pythons = extra_pythons
manifest = Manifest({}, cfg)
def session_func():
pass
func = Func(session_func, python=python)
for session in manifest.make_session("my_session", func):
manifest.add_session(session)
assert expected == [session.func.python for session in manifest._all_sessions]
@pytest.mark.parametrize(
"python,force_pythons,expected",
[
(None, [], [None]),
(None, ["3.8"], ["3.8"]),
(None, ["3.8", "3.9"], ["3.8", "3.9"]),
(False, [], [False]),
(False, ["3.8"], ["3.8"]),
(False, ["3.8", "3.9"], ["3.8", "3.9"]),
("3.5", [], ["3.5"]),
("3.5", ["3.8"], ["3.5", "3.8"]),
("3.5", ["3.8", "3.9"], ["3.5", "3.8", "3.9"]),
(["3.5", "3.9"], [], ["3.5", "3.9"]),
(["3.5", "3.9"], ["3.8"], ["3.5", "3.9", "3.8"]),
(["3.5", "3.9"], ["3.8", "3.4"], ["3.5", "3.9", "3.8", "3.4"]),
(["3.5", "3.9"], ["3.5", "3.9"], ["3.5", "3.9"]),
],
)
def test_force_pythons(python, force_pythons, expected):
cfg = create_mock_config()
cfg.force_pythons = force_pythons
cfg.extra_pythons = force_pythons
manifest = Manifest({}, cfg)
def session_func():
pass
func = Func(session_func, python=python)
for session in manifest.make_session("my_session", func):
manifest.add_session(session)
assert expected == [session.func.python for session in manifest._all_sessions]
def test_add_session_parametrized():
manifest = Manifest({}, create_mock_config())
# Define a session with parameters.
@nox.parametrize("param", ("a", "b", "c"))
def my_session(session, param):
pass
func = Func(my_session, python=None)
# Add the session to the manifest.
for session in manifest.make_session("my_session", func):
manifest.add_session(session)
assert len(manifest) == 3
def test_add_session_parametrized_multiple_pythons():
manifest = Manifest({}, create_mock_config())
# Define a session with parameters.
@nox.parametrize("param", ("a", "b"))
def my_session(session, param):
pass
func = Func(my_session, python=["2.7", "3.6"])
# Add the session to the manifest.
for session in manifest.make_session("my_session", func):
manifest.add_session(session)
assert len(manifest) == 4
def test_add_session_parametrized_noop():
manifest = Manifest({}, create_mock_config())
# Define a session without any parameters.
@nox.parametrize("param", ())
def my_session(session, param):
pass
my_session.python = None
my_session.venv_backend = None
# Add the session to the manifest.
for session in manifest.make_session("my_session", my_session):
manifest.add_session(session)
assert len(manifest) == 1
session = manifest["my_session"]
assert session.func == _null_session_func
def test_notify():
manifest = Manifest({}, create_mock_config())
# Define a session.
def my_session(session):
pass
my_session.python = None
my_session.venv_backend = None
def notified(session):
pass
notified.python = None
notified.venv_backend = None
# Add the sessions to the manifest.
for session in manifest.make_session("my_session", my_session):
manifest.add_session(session)
for session in manifest.make_session("notified", notified):
manifest.add_session(session)
assert len(manifest) == 2
# Filter so only the first session is included in the queue.
manifest.filter_by_name(("my_session",))
assert len(manifest) == 1
# Notify the notified session.
manifest.notify("notified")
assert len(manifest) == 2
def test_notify_noop():
manifest = Manifest({}, create_mock_config())
# Define a session and add it to the manifest.
def my_session(session):
pass
my_session.python = None
my_session.venv_backend = None
for session in manifest.make_session("my_session", my_session):
manifest.add_session(session)
assert len(manifest) == 1
# Establish idempotency; notifying a session already in the queue no-ops.
manifest.notify("my_session")
assert len(manifest) == 1
def test_notify_with_posargs():
cfg = create_mock_config()
manifest = Manifest({}, cfg)
session = manifest.make_session("my_session", Func(lambda session: None))[0]
manifest.add_session(session)
# delete my_session from the queue
manifest.filter_by_name(())
assert session.posargs == cfg.posargs
assert manifest.notify("my_session", posargs=["--an-arg"])
assert session.posargs == ["--an-arg"]
def test_notify_error():
manifest = Manifest({}, create_mock_config())
with pytest.raises(ValueError):
manifest.notify("does_not_exist")
def test_add_session_idempotent():
manifest = Manifest({}, create_mock_config())
session_func = mock.Mock(spec=(), python=None, venv_backend=None)
for session in manifest.make_session("my_session", session_func):
manifest.add_session(session)
manifest.add_session(session)
assert len(manifest) == 1
def test_null_session_function():
session = mock.Mock(spec=("skip",))
_null_session_func(session)
assert session.skip.called
def test_keyword_locals_length():
kw = KeywordLocals({"foo", "bar"})
assert len(kw) == 2
def test_keyword_locals_iter():
values = ["foo", "bar"]
kw = KeywordLocals(values)
assert list(kw) == values
def test_no_venv_backend_but_some_pythons():
manifest = Manifest({}, create_mock_config())
# Define a session and add it to the manifest.
def my_session(session):
pass
# the session sets "no venv backend" but declares some pythons
my_session.python = ["3.7", "3.8"]
my_session.venv_backend = "none"
my_session.should_warn = {}
sessions = manifest.make_session("my_session", my_session)
# check that the pythons were correctly removed (a log warning is also emitted)
assert sessions[0].func.python is False
assert sessions[0].func.should_warn == {WARN_PYTHONS_IGNORED: ["3.7", "3.8"]}
|