Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add normalized comparison to check sessions (#396) #417

Merged
merged 1 commit into from
Apr 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions nox/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import argparse
import ast
import collections.abc
import itertools
from collections import OrderedDict
Expand Down Expand Up @@ -133,21 +134,26 @@ def filter_by_name(self, specified_sessions: Iterable[str]) -> None:
queue = []
for session_name in specified_sessions:
for session in self._queue:
if session_name == session.name or session_name in set(
session.signatures
):
if _normalized_session_match(session_name, session):
queue.append(session)

self._queue = queue

# If a session was requested and was not found, complain loudly.
all_sessions = set(
itertools.chain(
[x.name for x in self._all_sessions if x.name],
*[x.signatures for x in self._all_sessions],
map(
_normalize_arg,
(
itertools.chain(
[x.name for x in self._all_sessions if x.name],
*[x.signatures for x in self._all_sessions],
)
),
)
)
missing_sessions = set(specified_sessions) - all_sessions
normalized_specified_sessions = [
_normalize_arg(session_name) for session_name in specified_sessions
]
missing_sessions = set(normalized_specified_sessions) - all_sessions
if missing_sessions:
raise KeyError("Sessions not found: {}".format(", ".join(missing_sessions)))

Expand Down Expand Up @@ -344,4 +350,24 @@ def _null_session_func_(session: Session) -> None:
session.skip("This session had no parameters available.")


def _normalized_session_match(session_name: str, session: SessionRunner) -> bool:
"""Checks if session_name matches session."""
if session_name == session.name or session_name in session.signatures:
return True
for name in session.signatures:
equal_rep = _normalize_arg(session_name) == _normalize_arg(name)
if equal_rep:
return True
# Exhausted
return False


def _normalize_arg(arg: str) -> Union[str]:
"""Normalize arg for comparison."""
try:
return str(ast.dump(ast.parse(arg)))
except (TypeError, SyntaxError):
return arg


_null_session_func = Func(_null_session_func_, python=False)
18 changes: 18 additions & 0 deletions tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
WARN_PYTHONS_IGNORED,
KeywordLocals,
Manifest,
_normalize_arg,
_normalized_session_match,
_null_session_func,
)

Expand All @@ -42,6 +44,22 @@ def create_mock_config():
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 strng
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())
Expand Down