def filter_manifest(manifest: Manifest, global_config: Namespace) -> Manifest | int: """Filter the manifest according to the provided configuration. Args: manifest (~.Manifest): The manifest of sessions to be run. global_config (~nox.main.GlobalConfig): The global configuration. Returns: Union[~.Manifest,int]: ``3`` if a specified session is not found, the manifest otherwise (to be sent to the next task). """ # Shouldn't happen unless the Noxfile is empty if not manifest: logger.error(f"No sessions found in {global_config.noxfile}.") return 3 # Filter by the name of any explicit sessions. # This can raise KeyError if a specified session does not exist; # log this if it happens. The sessions does not come from the Noxfile # if keywords is not empty. if global_config.sessions is not None: try: manifest.filter_by_name(global_config.sessions) except KeyError as exc: logger.error("Error while collecting sessions.") logger.error(exc.args[0]) return 3 if not manifest and not global_config.list_sessions: print( "No sessions selected. Please select a session with -s <session name>.\n" ) _produce_listing(manifest, global_config) return 0 # Filter by python interpreter versions. if global_config.pythons: manifest.filter_by_python_interpreter(global_config.pythons) if not manifest and not global_config.list_sessions: logger.error( "Python version selection caused no sessions to be selected.") return 3 # Filter by tags. if global_config.tags is not None: manifest.filter_by_tags(global_config.tags) if not manifest and not global_config.list_sessions: logger.error("Tag selection caused no sessions to be selected.") return 3 # Filter by keywords. if global_config.keywords: try: ast.parse(global_config.keywords, mode="eval") except SyntaxError: logger.error( "Error while collecting sessions: keywords argument must be a Python" " expression.") return 3 # This function never errors, but may cause an empty list of sessions # (which is an error condition later). manifest.filter_by_keywords(global_config.keywords) if not manifest and not global_config.list_sessions: logger.error("No sessions selected after filtering by keyword.") return 3 # Return the modified manifest. return manifest
def test_filter_by_tags(tags: list[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