コード例 #1
0
ファイル: test_main.py プロジェクト: tseaver/nox
def test_main_interrupted():
    sys.argv = [sys.executable]

    with contexter.ExitStack() as stack:
        run_mock = stack.enter_context(mock.patch('nox.main.run'))
        exit_mock = stack.enter_context(mock.patch('sys.exit'))
        run_mock.side_effect = KeyboardInterrupt()
        nox.main.main()
        exit_mock.assert_called_with(1)
コード例 #2
0
ファイル: test_main.py プロジェクト: tseaver/nox
def test_main_failure():
    sys.argv = [sys.executable]

    with contexter.ExitStack() as stack:
        run_mock = stack.enter_context(mock.patch('nox.main.run'))
        exit_mock = stack.enter_context(mock.patch('sys.exit'))
        run_mock.return_value = False
        nox.main.main()
        exit_mock.assert_called_with(1)
コード例 #3
0
ファイル: test_main.py プロジェクト: vaneseltine/nox
def test_main_help(capsys, monkeypatch):
    monkeypatch.setattr(sys, "argv", [sys.executable, "--help"])

    with contexter.ExitStack() as stack:
        execute = stack.enter_context(mock.patch("nox.workflow.execute"))
        exit_mock = stack.enter_context(mock.patch("sys.exit"))
        nox.__main__.main()
        out, _ = capsys.readouterr()
        assert "help" in out
        exit_mock.assert_not_called()
        execute.assert_not_called()
コード例 #4
0
ファイル: test_main.py プロジェクト: vaneseltine/nox
def test_main_version(capsys, monkeypatch):
    monkeypatch.setattr(sys, "argv", [sys.executable, "--version"])

    with contexter.ExitStack() as stack:
        execute = stack.enter_context(mock.patch("nox.workflow.execute"))
        exit_mock = stack.enter_context(mock.patch("sys.exit"))
        nox.__main__.main()
        _, err = capsys.readouterr()
        assert VERSION in err
        exit_mock.assert_not_called()
        execute.assert_not_called()
コード例 #5
0
ファイル: test_main.py プロジェクト: stsewd/nox
def test_main_version(capsys):
    sys.argv = [sys.executable, '--version']

    with contexter.ExitStack() as stack:
        execute = stack.enter_context(mock.patch('nox.workflow.execute'))
        exit_mock = stack.enter_context(mock.patch('sys.exit'))
        nox.main.main()
        _, err = capsys.readouterr()
        assert VERSION in err
        exit_mock.assert_not_called()
        execute.assert_not_called()
コード例 #6
0
ファイル: test_main.py プロジェクト: tseaver/nox
def test_run(monkeypatch, capsys, tmpdir):
    class MockSession(nox.sessions.Session):
        def __init__(self,
                     name='session_name',
                     signature=None,
                     global_config=None,
                     return_value=True):
            super(MockSession, self).__init__(name, signature, None,
                                              global_config)
            self.execute = mock.Mock()
            self.execute.return_value = return_value

    global_config = Namespace(
        noxfile='somefile.py',
        sessions=None,
        keywords=None,
        list_sessions=False,
        stop_on_first_error=False,
        posargs=[],
        report=None,
    )
    user_nox_module = mock.Mock()
    session_functions = mock.Mock()
    sessions = [MockSession(), MockSession()]

    with contexter.ExitStack() as stack:
        mock_load_user_module = stack.enter_context(
            mock.patch('nox.main.load_user_nox_module',
                       side_effect=lambda _: user_nox_module))
        mock_discover_session_functions = stack.enter_context(
            mock.patch('nox.main.discover_session_functions',
                       side_effect=lambda _: session_functions))
        mock_make_sessions = stack.enter_context(
            mock.patch('nox.main.make_sessions',
                       side_effect=lambda _1, _2: sessions))

        # Default options
        result = nox.main.run(global_config)
        assert result

        # The `load_user_module` function receives an absolute path,
        # but it should end with the noxfile argument.
        mock_load_user_module.assert_called_once()
        _, args, _ = mock_load_user_module.mock_calls[0]
        assert args[0].endswith('somefile.py')

        mock_discover_session_functions.assert_called_with(user_nox_module)
        mock_make_sessions.assert_called_with(session_functions, global_config)

        for session in sessions:
            assert session.execute.called
            session.execute.reset_mock()

        # List sessions
        global_config.list_sessions = True
        result = nox.main.run(global_config)
        assert result

        out, _ = capsys.readouterr()
        assert '* session_name' in out

        global_config.list_sessions = False

        # One failing session at the beginning, should still execute all.
        failing_session = MockSession(return_value=False)
        sessions.insert(0, failing_session)

        result = nox.main.run(global_config)
        assert not result

        for session in sessions:
            assert session.execute.called
            session.execute.reset_mock()

        # Now it should stop after the first failed session.
        global_config.stop_on_first_error = True

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # This time it should only run a subset of sessions
        sessions[0].execute.return_value = True
        sessions[0].name = '1'
        sessions[1].name = '2'
        sessions[2].name = '3'

        global_config.sessions = ['1', '3']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is True

        for session in sessions:
            session.execute.reset_mock()

        # Try to run with a session that doesn't exist.
        global_config.sessions = ['1', 'doesntexist']

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Now we'll try with parametrized sessions. Calling the basename
        # should execute all parametrized versions.
        sessions[0].name = 'a'
        sessions[0].signature = 'a(1)'
        sessions[1].name = 'a'
        sessions[1].signature = 'a(2)'
        sessions[2].name = 'b'

        global_config.sessions = ['a']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is True
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Calling the signature of should only call one parametrized version.
        global_config.sessions = ['a(2)']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is True
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Calling a signature that does not exist should not call any version.
        global_config.sessions = ['a(1)', 'a(3)', 'b']

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

        # Calling a name of an empty parametrized session should work.
        sessions[:] = [
            nox.sessions.Session('name', None, nox.main._null_session_func,
                                 global_config)
        ]
        global_config.sessions = ['name']

        assert nox.main.run(global_config)

        # Using -k should filter sessions
        sessions[:] = [
            MockSession('red', 'red()'),
            MockSession('blue', 'blue()'),
            MockSession('red', 'red(blue)'),
            MockSession('redder', 'redder()')
        ]
        global_config.sessions = None
        global_config.keywords = 'red and not blue'

        assert nox.main.run(global_config)
        assert sessions[0].execute.called
        assert not sessions[1].execute.called
        assert not sessions[2].execute.called
        assert sessions[3].execute.called

        # Reporting should work
        report = tmpdir.join('report.json')
        global_config.report = str(report)
        assert nox.main.run(global_config)
        assert report.exists()
コード例 #7
0
ファイル: test_main.py プロジェクト: yowmamasita/nox
def test_run(monkeypatch, capsys):
    class MockSession(object):
        def __init__(self, return_value=True):
            self.name = 'session_name'
            self.signature = None
            self.execute = mock.Mock()
            self.execute.return_value = return_value

    global_config = Namespace(noxfile='somefile.py',
                              sessions=None,
                              list_sessions=False,
                              stop_on_first_error=False)
    user_nox_module = mock.Mock()
    session_functions = mock.Mock()
    sessions = [MockSession(), MockSession()]

    with contexter.ExitStack() as stack:
        mock_load_user_module = stack.enter_context(
            mock.patch('nox.main.load_user_nox_module',
                       side_effect=lambda _: user_nox_module))
        mock_discover_session_functions = stack.enter_context(
            mock.patch('nox.main.discover_session_functions',
                       side_effect=lambda _: session_functions))
        mock_make_sessions = stack.enter_context(
            mock.patch('nox.main.make_sessions',
                       side_effect=lambda _1, _2: sessions))

        # Default options
        result = nox.main.run(global_config)
        assert result

        mock_load_user_module.assert_called_with('somefile.py')
        mock_discover_session_functions.assert_called_with(user_nox_module)
        mock_make_sessions.assert_called_with(session_functions, global_config)

        for session in sessions:
            assert session.execute.called
            session.execute.reset_mock()

        # List sessions
        global_config.list_sessions = True
        result = nox.main.run(global_config)
        assert result

        out, _ = capsys.readouterr()
        assert '* session_name' in out

        global_config.list_sessions = False

        # One failing session at the beginning, should still execute all.
        failing_session = MockSession(return_value=False)
        sessions.insert(0, failing_session)

        result = nox.main.run(global_config)
        assert not result

        for session in sessions:
            assert session.execute.called
            session.execute.reset_mock()

        # Now it should stop after the first failed session.
        global_config.stop_on_first_error = True

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # This time it should only run a subset of sessions
        sessions[0].execute.return_value = True
        sessions[0].name = '1'
        sessions[1].name = '2'
        sessions[2].name = '3'

        global_config.sessions = ['1', '3']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is True

        for session in sessions:
            session.execute.reset_mock()

        # Now we'll try with parametrized sessions. Calling the basename
        # should execute all parametrized versions.
        sessions[0].name = 'a'
        sessions[0].signature = 'a(1)'
        sessions[1].name = 'a'
        sessions[1].signature = 'a(2)'
        sessions[2].name = 'b'

        global_config.sessions = ['a']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is True
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Calling the signature of should only call one parametrized version.
        global_config.sessions = ['a(2)']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is True
        assert sessions[2].execute.called is False