Ejemplo n.º 1
0
def test_filter_manifest_keywords():
    config = _options.options.namespace(sessions=(),
                                        pythons=(),
                                        keywords="foo or bar")
    manifest = Manifest(
        {
            "foo": session_func,
            "bar": session_func,
            "baz": session_func
        }, config)
    return_value = tasks.filter_manifest(manifest, config)
    assert return_value is manifest
    assert len(manifest) == 2
Ejemplo n.º 2
0
def test_filter_manifest_keywords_not_found(caplog):
    config = _options.options.namespace(sessions=None,
                                        pythons=(),
                                        keywords="mouse or python",
                                        posargs=[])
    manifest = Manifest(
        {
            "foo": session_func,
            "bar": session_func,
            "baz": session_func
        }, config)
    return_value = tasks.filter_manifest(manifest, config)
    assert return_value == 3
    assert "No sessions selected after filtering by keyword." in caplog.text
Ejemplo n.º 3
0
def test_add_session_parametrized():
    manifest = Manifest({}, mock.sentinel.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
Ejemplo n.º 4
0
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"]
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
def test_add_session_parametrized_multiple_pythons():
    manifest = Manifest({}, mock.sentinel.CONFIG)

    # Define a session with parameters.
    @nox.parametrize("param", ("a", "b"))
    def my_session(session, param):
        pass

    my_session.python = ["2.7", "3.6"]

    # Add the session to the manifest.
    for session in manifest.make_session("my_session", my_session):
        manifest.add_session(session)
    assert len(manifest) == 4
Ejemplo n.º 7
0
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]
Ejemplo n.º 8
0
def test_add_session_parametrized_noop():
    manifest = Manifest({}, mock.sentinel.CONFIG)

    # Define a session without any parameters.
    @nox.parametrize("param", ())
    def my_session(session, param):
        pass

    my_session.python = 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
Ejemplo n.º 9
0
def test_add_session_parametrized_multiple_pythons():
    manifest = Manifest({}, mock.sentinel.CONFIG)

    # Define a session with parameters.
    @nox.parametrize('param', ('a', 'b'))
    def my_session(session, param):
        pass

    my_session.python = ['2.7', '3.6']

    # Add the session to the manifest.
    for session in manifest.make_session('my_session', my_session):
        manifest.add_session(session)
    assert len(manifest) == 4
Ejemplo n.º 10
0
def test_filter_manifest_pythons():
    config = _options.options.namespace(sessions=(),
                                        pythons=("3.8", ),
                                        keywords=())
    manifest = Manifest(
        {
            "foo": session_func_with_python,
            "bar": session_func,
            "baz": session_func
        },
        config,
    )
    return_value = tasks.filter_manifest(manifest, config)
    assert return_value is manifest
    assert len(manifest) == 1
Ejemplo n.º 11
0
def test_filter_manifest_pythons_not_found(caplog):
    config = _options.options.namespace(sessions=None,
                                        pythons=("1.2", ),
                                        keywords=(),
                                        posargs=[])
    manifest = Manifest(
        {
            "foo": session_func_with_python,
            "bar": session_func,
            "baz": session_func
        },
        config,
    )
    return_value = tasks.filter_manifest(manifest, config)
    assert return_value == 3
    assert "Python version selection caused no sessions to be selected." in caplog.text
Ejemplo n.º 12
0
def test_notify_noop():
    manifest = Manifest({}, mock.sentinel.CONFIG)

    # Define a session and add it to the manifest.
    def my_session(session):
        pass

    my_session.python = 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
Ejemplo n.º 13
0
def test_getitem():
    sessions = create_mock_sessions()
    manifest = Manifest(sessions, mock.sentinel.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"]
Ejemplo n.º 14
0
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 = dict()

    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"]}
Ejemplo n.º 15
0
def discover_manifest(module, global_config):
    """Discover all session functions in the noxfile module.

    Args:
        module (module): The Noxfile module.
        global_config (~nox.main.GlobalConfig): The global configuration.

    Returns:
        ~.Manifest: A manifest of session functions.
    """
    # Find any function added to the session registry (meaning it was
    # decorated with @nox.session); do not sort these, as they are being
    # sorted by decorator call time.
    functions = registry.get()

    # Return the final dictionary of session functions.
    return Manifest(functions, global_config)
Ejemplo n.º 16
0
def test_contains():
    sessions = create_mock_sessions()
    manifest = Manifest(sessions, mock.sentinel.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
Ejemplo n.º 17
0
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
Ejemplo n.º 18
0
def test_extra_pythons(python, extra_pythons, expected):
    cfg = mock.sentinel.CONFIG
    cfg.force_venv_backend = None
    cfg.default_venv_backend = None
    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
    ]
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
def discover_manifest(module: types.ModuleType | int,
                      global_config: Namespace) -> Manifest:
    """Discover all session functions in the Noxfile module.

    Args:
        module (module): The Noxfile module.
        global_config (~nox.main.GlobalConfig): The global configuration.

    Returns:
        ~.Manifest: A manifest of session functions.
    """
    # Find any function added to the session registry (meaning it was
    # decorated with @nox.session); do not sort these, as they are being
    # sorted by decorator call time.
    functions = registry.get()

    # Get the docstring from the Noxfile
    module_docstring = module.__doc__

    # Return the final dictionary of session functions.
    return Manifest(functions, global_config, module_docstring)
Ejemplo n.º 21
0
def test_notify():
    manifest = Manifest({}, mock.sentinel.CONFIG)

    # Define a session.
    def my_session(session):
        pass

    def notified(session):
        pass

    # 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
Ejemplo n.º 22
0
def test_verify_manifest_nonempty():
    config = argparse.Namespace(sessions=(), keywords=())
    manifest = Manifest({"session": session_func}, config)
    return_value = tasks.verify_manifest_nonempty(manifest, global_config=config)
    assert return_value == manifest
Ejemplo n.º 23
0
def test_filter_manifest():
    config = argparse.Namespace(sessions=(), keywords=())
    manifest = Manifest({"foo": session_func, "bar": session_func}, config)
    return_value = tasks.filter_manifest(manifest, config)
    assert return_value is manifest
    assert len(manifest) == 2
Ejemplo n.º 24
0
def test_notify_error():
    manifest = Manifest({}, mock.sentinel.CONFIG)
    with pytest.raises(ValueError):
        manifest.notify("does_not_exist")
Ejemplo n.º 25
0
def test_filter_manifest_not_found():
    config = argparse.Namespace(sessions=("baz",), keywords=())
    manifest = Manifest({"foo": session_func, "bar": session_func}, config)
    return_value = tasks.filter_manifest(manifest, config)
    assert return_value == 3
Ejemplo n.º 26
0
def test_len():
    sessions = create_mock_sessions()
    manifest = Manifest(sessions, mock.sentinel.CONFIG)
    assert len(manifest) == 2
    for session in manifest:
        assert len(manifest) == 2
Ejemplo n.º 27
0
def test_filter_by_name():
    sessions = create_mock_sessions()
    manifest = Manifest(sessions, mock.sentinel.CONFIG)
    manifest.filter_by_name(("foo", ))
    assert "foo" in manifest
    assert "bar" not in manifest
Ejemplo n.º 28
0
def test_add_session_plain():
    manifest = Manifest({}, mock.sentinel.CONFIG)
    session_func = mock.Mock(spec=(), python=None)
    for session in manifest.make_session("my_session", session_func):
        manifest.add_session(session)
    assert len(manifest) == 1
Ejemplo n.º 29
0
def test_filter_by_name_not_found():
    sessions = create_mock_sessions()
    manifest = Manifest(sessions, mock.sentinel.CONFIG)
    with pytest.raises(KeyError):
        manifest.filter_by_name(("baz", ))
Ejemplo n.º 30
0
def test_filter_by_name_maintains_order():
    sessions = create_mock_sessions()
    manifest = Manifest(sessions, mock.sentinel.CONFIG)
    manifest.filter_by_name(("bar", "foo"))
    assert [session.name for session in manifest] == ["bar", "foo"]