Exemplo n.º 1
0
    def make_session(self, name, func):
        """Create a session object from the session function.

        Args:
            name (str): The name of the session.
            func (function): The session function.

        Returns:
            Sequence[~nox.session.Session]: A sequence of Session objects
                bound to this manifest and configuration.
        """
        sessions = []

        # If the func has the python attribute set to a list, we'll need
        # to expand them.
        if isinstance(func.python, (list, tuple, set)):
            for python in func.python:
                single_func = _copy_func(func)
                single_func.python = python
                sessions.extend(self.make_session(name, single_func))

            return sessions

        # Simple case: If this function is not parametrized, then make
        # a simple session
        if not hasattr(func, 'parametrize'):
            if func.python:
                long_name = '{}-{}'.format(name, func.python)
            else:
                long_name = name
            session = SessionRunner(name, long_name, func, self._config, self)
            return [session]

        # Since this function is parametrized, we need to add a distinct
        # session for each permutation.
        calls = generate_calls(func, func.parametrize)
        for call in calls:
            if func.python:
                long_name = '{}-{}{}'.format(name, func.python,
                                             call.session_signature)
            else:
                long_name = '{}{}'.format(name, call.session_signature)

            sessions.append(
                SessionRunner(name, long_name, call, self._config, self))

        # Edge case: If the parameters made it such that there were no valid
        # calls, add an empty, do-nothing session.
        if not calls:
            sessions.append(
                SessionRunner(name, None, _null_session_func, self._config,
                              self), )

        # Return the list of sessions.
        return sessions
Exemplo n.º 2
0
    def make_session(self,
                     name: str,
                     func: Func,
                     multi: bool = False) -> list[SessionRunner]:
        """Create a session object from the session function.

        Args:
            name (str): The name of the session.
            func (function): The session function.
            multi (bool): Whether the function is a member of a set of sessions
                with different interpreters.

        Returns:
            Sequence[~nox.session.Session]: A sequence of Session objects
                bound to this manifest and configuration.
        """
        sessions = []

        # If the backend is "none", we won't parametrize `python`.
        backend = (self._config.force_venv_backend or func.venv_backend
                   or self._config.default_venv_backend)
        if backend == "none" and isinstance(func.python, (list, tuple, set)):
            # we can not log a warning here since the session is maybe deselected.
            # instead let's set a flag, to warn later when session is actually run.
            func.should_warn[WARN_PYTHONS_IGNORED] = func.python
            func.python = False

        if self._config.extra_pythons:
            # If extra python is provided, expand the func.python list to
            # include additional python interpreters
            extra_pythons: list[str] = self._config.extra_pythons
            if isinstance(func.python, (list, tuple, set)):
                func.python = _unique_list(*func.python, *extra_pythons)
            elif not multi and func.python:
                # If this is multi, but there is only a single interpreter, it
                # is the reentrant case. The extra_python interpreter shouldn't
                # be added in that case. If func.python is False, the session
                # has no backend; if None, it uses the same interpreter as Nox.
                # Otherwise, add the extra specified python.
                assert isinstance(func.python, str)
                func.python = _unique_list(func.python, *extra_pythons)

        # If the func has the python attribute set to a list, we'll need
        # to expand them.
        if isinstance(func.python, (list, tuple, set)):

            for python in func.python:
                single_func = func.copy()
                single_func.python = python
                sessions.extend(
                    self.make_session(name, single_func, multi=True))

            return sessions

        # Simple case: If this function is not parametrized, then make
        # a simple session.
        if not hasattr(func, "parametrize"):
            long_names = []
            if not multi:
                long_names.append(name)
            if func.python:
                long_names.append(f"{name}-{func.python}")

            return [SessionRunner(name, long_names, func, self._config, self)]

        # Since this function is parametrized, we need to add a distinct
        # session for each permutation.
        parametrize = func.parametrize  # type: ignore[attr-defined]
        calls = Call.generate_calls(func, parametrize)
        for call in calls:
            long_names = []
            if not multi:
                long_names.append(f"{name}{call.session_signature}")
            if func.python:
                long_names.append(
                    f"{name}-{func.python}{call.session_signature}")
                # Ensure that specifying session-python will run all parameterizations.
                long_names.append(f"{name}-{func.python}")

            sessions.append(
                SessionRunner(name, long_names, call, self._config, self))

        # Edge case: If the parameters made it such that there were no valid
        # calls, add an empty, do-nothing session.
        if not calls:
            sessions.append(
                SessionRunner(name, [], _null_session_func, self._config,
                              self))

        # Return the list of sessions.
        return sessions
Exemplo n.º 3
0
    def make_session(self,
                     name: str,
                     func: "Func",
                     multi: bool = False) -> List[SessionRunner]:
        """Create a session object from the session function.

        Args:
            name (str): The name of the session.
            func (function): The session function.
            multi (bool): Whether the function is a member of a set of sessions
                with different interpreters.

        Returns:
            Sequence[~nox.session.Session]: A sequence of Session objects
                bound to this manifest and configuration.
        """
        sessions = []

        # If the func has the python attribute set to a list, we'll need
        # to expand them.
        if isinstance(func.python, (list, tuple, set)):

            for python in func.python:
                single_func = func.copy()
                single_func.python = python
                sessions.extend(
                    self.make_session(name, single_func, multi=True))

            return sessions

        # Simple case: If this function is not parametrized, then make
        # a simple session
        if not hasattr(func, "parametrize"):
            long_names = []
            if not multi:
                long_names.append(name)
            if func.python:
                long_names.append("{}-{}".format(name, func.python))

            return [SessionRunner(name, long_names, func, self._config, self)]

        # Since this function is parametrized, we need to add a distinct
        # session for each permutation.
        parametrize = func.parametrize  # type: ignore
        calls = Call.generate_calls(func, parametrize)
        for call in calls:
            long_names = []
            if not multi:
                long_names.append("{}{}".format(name, call.session_signature))
            if func.python:
                long_names.append("{}-{}{}".format(name, func.python,
                                                   call.session_signature))
                # Ensure that specifying session-python will run all parameterizations.
                long_names.append("{}-{}".format(name, func.python))

            sessions.append(
                SessionRunner(name, long_names, call, self._config, self))

        # Edge case: If the parameters made it such that there were no valid
        # calls, add an empty, do-nothing session.
        if not calls:
            sessions.append(
                SessionRunner(name, [], _null_session_func, self._config,
                              self))

        # Return the list of sessions.
        return sessions