Exemple #1
0
def test_python_toggle_plotter():
    """
    Ensure toggling the plotter causes it to be added/removed.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    pm.add_plotter = mock.MagicMock()
    pm.remove_plotter = mock.MagicMock()
    pm.toggle_plotter()
    pm.add_plotter.assert_called_once_with()
    pm.plotter = True
    pm.toggle_plotter()
    pm.remove_plotter.assert_called_once_with()
Exemple #2
0
def test_python_remove_plotter():
    """
    Ensure the button states are returned to normal before calling super
    method.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    with mock.patch('builtins.super') as mock_super:
        pm = PythonMode(editor, view)
        pm.set_buttons = mock.MagicMock()
        mock_super.reset_mock()
        pm.remove_plotter()
        pm.set_buttons.assert_called_once_with(run=True, repl=True, debug=True)
        mock_super().remove_plotter.assert_called_once_with()
Exemple #3
0
def test_python_run_toggle_on_cancelled():
    """
    Ensure the button states are correct if running an unsaved script is
    cancelled before the process is allowed to start. See issue #338.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    pm.runner = None
    pm.run_script = mock.MagicMock()
    pm.run_toggle(None)
    pm.run_script.assert_called_once_with()
    slot = pm.view.button_bar.slots["run"]
    assert slot.setIcon.call_count == 0
Exemple #4
0
def test_python_stop_script():
    """
    Check that the child process is killed, the runner cleaned up and UI
    is reset.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    mock_runner = mock.MagicMock()
    pm.runner = mock_runner
    pm.stop_script()
    mock_runner.process.kill.assert_called_once_with()
    mock_runner.process.waitForFinished.assert_called_once_with()
    assert pm.runner is None
Exemple #5
0
def test_python_toggle_repl():
    """
    Ensure the REPL handling works as expected.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    pm.add_repl = mock.MagicMock()
    pm.remove_repl = mock.MagicMock()
    pm.toggle_repl(None)
    pm.add_repl.assert_called_once_with()
    pm.repl = True
    pm.toggle_repl(None)
    pm.remove_repl.assert_called_once_with()
Exemple #6
0
def test_python_on_kernel_stop():
    """
    Ensure everything REPL based is cleaned up when this handler is called.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    view.button_bar.slots = {
        'repl': mock.MagicMock(),
    }
    pm = PythonMode(editor, view)
    pm.on_kernel_stop()
    assert pm.repl_kernel_manager is None
    view.button_bar.slots['repl'].setEnabled.assert_called_once_with(True)
    editor.show_status_message.assert_called_once_with('REPL stopped.')
    assert pm.kernel_runner is None
Exemple #7
0
def test_python_on_kernel_start():
    """
    Ensure the handler for when the kernel has started updates the UI such that
    the kernel manager and kernel client are used to add the Jupyter widget to
    the UI, the REPL button is re-enabled and a status update is shown.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    mock_kernel_manager = mock.MagicMock()
    mock_client = mock.MagicMock()
    pm.on_kernel_start(mock_kernel_manager, mock_client)
    view.add_jupyter_repl.assert_called_once_with(mock_kernel_manager,
                                                  mock_client)
    view.button_bar.slots['repl'].setEnabled.assert_called_once_with(True)
    editor.show_status_message.assert_called_once_with('REPL started.')
Exemple #8
0
def test_python_add_repl():
    """
    Check the REPL's kernal manager is configured correctly before being handed
    to the Jupyter widget in the view.
    """
    mock_kernel_manager = mock.MagicMock()
    mock_manager_class = mock.MagicMock(return_value=mock_kernel_manager)
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    with mock.patch('mu.modes.python3.QtInProcessKernelManager',
                    mock_manager_class):
        pm.add_repl()
    mock_kernel_manager.start_kernel.assert_called_once_with(show_banner=False)
    view.add_jupyter_repl.assert_called_once_with(mock_kernel_manager)
    assert pm.repl == mock_kernel_manager
Exemple #9
0
def test_python_run_toggle_on():
    """
    Check the handler for clicking run starts the new process and updates the
    UI state.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    pm.runner = None
    pm.run_script = mock.MagicMock()
    pm.run_toggle(None)
    pm.run_script.assert_called_once_with()
    slot = pm.view.button_bar.slots['run']
    assert slot.setIcon.call_count == 1
    slot.setText.assert_called_once_with('Stop')
    slot.setToolTip.assert_called_once_with('Stop your Python script.')
    pm.view.button_bar.slots['debug'].setEnabled.assert_called_once_with(False)
Exemple #10
0
def test_python_run_script():
    """
    Ensure that running the script launches the process as expected.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    view.current_tab.path = '/foo'
    view.current_tab.isModified.return_value = True
    mock_runner = mock.MagicMock()
    view.add_python3_runner.return_value = mock_runner
    pm = PythonMode(editor, view)
    pm.workspace_dir = mock.MagicMock(return_value='/bar')
    with mock.patch('builtins.open') as oa, \
            mock.patch('mu.modes.python3.write_and_flush'):
        pm.run_script()
        oa.assert_called_once_with('/foo', 'w', newline='')
    view.add_python3_runner.assert_called_once_with('/foo', '/bar',
                                                    interactive=True)
    mock_runner.process.waitForStarted.assert_called_once_with()
Exemple #11
0
def test_python_mode():
    """
    Sanity check for setting up of the mode.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    assert pm.name == 'Python 3'
    assert pm.description is not None
    assert pm.icon == 'python'
    assert pm.is_debugger is False
    assert pm.editor == editor
    assert pm.view == view

    actions = pm.actions()
    assert len(actions) == 2
    assert actions[0]['name'] == 'run'
    assert actions[0]['handler'] == pm.run
    assert actions[1]['name'] == 'repl'
    assert actions[1]['handler'] == pm.toggle_repl
Exemple #12
0
def test_python_add_repl():
    """
    Check the REPL's kernal manager is configured correctly before being handed
    to the Jupyter widget in the view.
    """
    mock_qthread = mock.MagicMock()
    mock_kernel_runner = mock.MagicMock()
    editor = mock.MagicMock()
    editor.envars = [["name", "value"]]
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    pm.set_buttons = mock.MagicMock()
    pm.stop_kernel = mock.MagicMock()
    with mock.patch("mu.modes.python3.QThread", mock_qthread), mock.patch(
        "mu.modes.python3.KernelRunner", mock_kernel_runner
    ):
        pm.add_repl()
    mock_qthread.assert_called_once_with()
    mock_kernel_runner.assert_called_once_with(
        cwd=pm.workspace_dir(), envars=editor.envars
    )
    assert pm.kernel_thread == mock_qthread()
    assert pm.kernel_runner == mock_kernel_runner()
    pm.set_buttons.assert_called_once_with(repl=False)
    pm.kernel_runner.moveToThread.assert_called_once_with(pm.kernel_thread)
    pm.kernel_runner.kernel_started.connect.assert_called_once_with(
        pm.on_kernel_start
    )
    pm.kernel_runner.kernel_finished.connect.assert_called_once_with(
        pm.kernel_thread.quit
    )
    pm.stop_kernel.connect.assert_called_once_with(
        pm.kernel_runner.stop_kernel
    )
    pm.kernel_thread.started.connect.assert_called_once_with(
        pm.kernel_runner.start_kernel
    )
    pm.kernel_thread.finished.connect.assert_called_once_with(
        pm.on_kernel_stop
    )
    pm.kernel_thread.start.assert_called_once_with()
Exemple #13
0
def test_python_run_toggle_off():
    """
    Check the handler for clicking run stops the process and reverts the UI
    state.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    view.button_bar.slots = {
        "debug": mock.MagicMock(),
        "modes": mock.MagicMock(),
        "run": mock.MagicMock(),
    }
    pm = PythonMode(editor, view)
    pm.runner = True
    pm.stop_script = mock.MagicMock()
    pm.run_toggle(None)
    pm.stop_script.assert_called_once_with()
    slot = pm.view.button_bar.slots["run"]
    assert slot.setIcon.call_count == 1
    slot.setText.assert_called_once_with("Run")
    slot.setToolTip.assert_called_once_with("Run your Python script.")
    pm.view.button_bar.slots["debug"].setEnabled.assert_called_once_with(True)
    pm.view.button_bar.slots["modes"].setEnabled.assert_called_once_with(True)
Exemple #14
0
def test_python_mode_no_charts():
    """
    If QCharts is not available, ensure the plotter feature is not available.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    assert pm.name == "Python 3"
    assert pm.description is not None
    assert pm.icon == "python"
    assert pm.is_debugger is False
    assert pm.editor == editor
    assert pm.view == view

    with mock.patch("mu.modes.python3.CHARTS", False):
        actions = pm.actions()
    assert len(actions) == 3
    assert actions[0]["name"] == "run"
    assert actions[0]["handler"] == pm.run_toggle
    assert actions[1]["name"] == "debug"
    assert actions[1]["handler"] == pm.debug
    assert actions[2]["name"] == "repl"
    assert actions[2]["handler"] == pm.toggle_repl
def test_python_run_script():
    """
    Ensure that running the script launches the process as expected.
    """
    editor = mock.MagicMock()
    editor.envars = [['name', 'value']]
    view = mock.MagicMock()
    view.current_tab.path = '/foo'
    view.current_tab.isModified.return_value = True
    mock_runner = mock.MagicMock()
    view.add_python3_runner.return_value = mock_runner
    pm = PythonMode(editor, view)
    pm.workspace_dir = mock.MagicMock(return_value='/bar')
    with mock.patch('builtins.open') as oa, \
            mock.patch('mu.modes.python3.write_and_flush'):
        pm.run_script()
        oa.assert_called_once_with('/foo', 'w', newline='')
    view.add_python3_runner.assert_called_once_with('/foo',
                                                    '/bar',
                                                    interactive=True,
                                                    envars=editor.envars)
    mock_runner.process.waitForStarted.assert_called_once_with()
    # Check the buttons are set to the correct state when other aspects of the
    # mode are also in play.
    pm.set_buttons = mock.MagicMock()
    pm.kernel_runner = True
    with mock.patch('builtins.open') as oa, \
            mock.patch('mu.modes.python3.write_and_flush'):
        pm.run_script()
    pm.set_buttons.assert_called_once_with(plotter=False)
    pm.set_buttons.reset_mock()
    pm.kernel_runner = False
    pm.plotter = True
    with mock.patch('builtins.open') as oa, \
            mock.patch('mu.modes.python3.write_and_flush'):
        pm.run_script()
    pm.set_buttons.assert_called_once_with(repl=False)
Exemple #16
0
def test_python_mode():
    """
    Sanity check for setting up of the mode.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    assert pm.name == "Python 3"
    assert pm.description is not None
    assert pm.icon == "python"
    assert pm.is_debugger is False
    assert pm.editor == editor
    assert pm.view == view

    actions = pm.actions()
    assert len(actions) == 4
    assert actions[0]["name"] == "run"
    assert actions[0]["handler"] == pm.run_toggle
    assert actions[1]["name"] == "debug"
    assert actions[1]["handler"] == pm.debug
    assert actions[2]["name"] == "repl"
    assert actions[2]["handler"] == pm.toggle_repl
    assert actions[3]["name"] == "plotter"
    assert actions[3]["handler"] == pm.toggle_plotter
Exemple #17
0
def test_python_run_script():
    """
    Ensure that running the script launches the process as expected.
    """
    editor = mock.MagicMock()
    editor.envars = [["name", "value"]]
    view = mock.MagicMock()
    view.current_tab.path = "/foo/bar"
    view.current_tab.isModified.return_value = True
    view.current_tab.text = mock.MagicMock(return_value="abc")
    mock_runner = mock.MagicMock()
    view.add_python3_runner.return_value = mock_runner
    pm = PythonMode(editor, view)
    with mock.patch.object(venv, "interpreter", "interpreter"):
        pm.run_script()

    editor.save_tab_to_file.assert_called_once_with(view.current_tab)
    view.add_python3_runner.assert_called_once_with(
        interpreter="interpreter",
        script_name="/foo/bar",
        working_directory="/foo",
        interactive=True,
        envars=editor.envars,
    )
    mock_runner.process.waitForStarted.assert_called_once_with()
    # Check the buttons are set to the correct state when other aspects of the
    # mode are also in play.
    pm.set_buttons = mock.MagicMock()
    pm.kernel_runner = True
    pm.run_script()
    pm.set_buttons.assert_called_once_with(plotter=False)
    pm.set_buttons.reset_mock()
    pm.kernel_runner = False
    pm.plotter = True
    pm.run_script()
    pm.set_buttons.assert_called_once_with(repl=False)
Exemple #18
0
def test_python_on_kernel_start():
    """
    Ensure the handler for when the kernel has started updates the UI such that
    the kernel manager and kernel client are used to add the Jupyter widget to
    the UI, the REPL button is re-enabled and a status update is shown.
    """
    editor = mock.MagicMock()
    view = mock.MagicMock()
    pm = PythonMode(editor, view)
    pm.set_buttons = mock.MagicMock()
    mock_kernel_manager = mock.MagicMock()
    mock_client = mock.MagicMock()
    pm.on_kernel_start(mock_kernel_manager, mock_client)
    view.add_jupyter_repl.assert_called_once_with(
        mock_kernel_manager, mock_client
    )
    pm.set_buttons.assert_called_once_with(repl=True)
    editor.show_status_message.assert_called_once_with("REPL started.")
    # Check button states are set according to what other aspects of the mode
    # are currently enabled.
    # With Runner enabled.
    pm.set_buttons.reset_mock()
    pm.runner = True
    pm.on_kernel_start(mock_kernel_manager, mock_client)
    assert pm.set_buttons.call_count == 2
    assert pm.set_buttons.call_args_list[0][1]["repl"] is True
    assert pm.set_buttons.call_args_list[1][1]["plotter"] is False
    # With Plotter enabled.
    pm.set_buttons.reset_mock()
    pm.runner = False
    pm.plotter = True
    pm.on_kernel_start(mock_kernel_manager, mock_client)
    assert pm.set_buttons.call_count == 2
    assert pm.set_buttons.call_args_list[0][1]["repl"] is True
    assert pm.set_buttons.call_args_list[1][1]["run"] is False
    assert pm.set_buttons.call_args_list[1][1]["debug"] is False