From 46aad9f9e59d18ceb0a6219251e0696ccdce4c86 Mon Sep 17 00:00:00 2001 From: dalthviz Date: Mon, 20 Feb 2023 13:38:07 -0500 Subject: [PATCH] Install test --- .github/workflows/linux-tests.yml | 1 + .../spyder/widgets/helper_widgets.py | 2 +- .../spyder/widgets/main_widget.py | 4 +- spyder_env_manager/tests/test_config.py | 6 +- spyder_env_manager/tests/test_plugin.py | 111 ++++++++++++++++-- 5 files changed, 106 insertions(+), 18 deletions(-) diff --git a/.github/workflows/linux-tests.yml b/.github/workflows/linux-tests.yml index e7dc968..0857dbf 100644 --- a/.github/workflows/linux-tests.yml +++ b/.github/workflows/linux-tests.yml @@ -21,6 +21,7 @@ jobs: matrix: PYTHON_VERSION: ['3.8', '3.9', '3.10'] USE_CONDA: ['True', 'False'] + timeout-minutes: 15 steps: - name: Checkout branch uses: actions/checkout@v2 diff --git a/spyder_env_manager/spyder/widgets/helper_widgets.py b/spyder_env_manager/spyder/widgets/helper_widgets.py index 35971d8..599a157 100644 --- a/spyder_env_manager/spyder/widgets/helper_widgets.py +++ b/spyder_env_manager/spyder/widgets/helper_widgets.py @@ -58,7 +58,7 @@ def __init__(self, parent, title, messages, types, contents): super().__init__(parent, Qt.WindowTitleHint | Qt.WindowCloseButtonHint) self.resize(450, 130) - self.setWindowTitle(_(title)) + self.setWindowTitle(title) self.setModal(True) self.lineedits = {} diff --git a/spyder_env_manager/spyder/widgets/main_widget.py b/spyder_env_manager/spyder/widgets/main_widget.py index 6a3fb2b..90deeb7 100644 --- a/spyder_env_manager/spyder/widgets/main_widget.py +++ b/spyder_env_manager/spyder/widgets/main_widget.py @@ -681,9 +681,9 @@ def _run_env_manager_action( *manager_action_args, **manager_action_kwargs, ) + self.manager_worker.moveToThread(self.env_manager_action_thread) self.manager_worker.sig_ready.connect(on_ready) self.manager_worker.sig_ready.connect(self.env_manager_action_thread.quit) - self.manager_worker.moveToThread(self.env_manager_action_thread) self.env_manager_action_thread.started.connect(self.manager_worker.start) self.start_spinner() self.env_manager_action_thread.start() @@ -962,7 +962,7 @@ def _message_new_environment(self): contents = [ {"conda-like"}, {}, - ["3.7", "3.8", "3.9", "3.10"], + ["3.7.12", "3.8.16", "3.9.16", "3.10.9"], ] self._message_box_editable( title, diff --git a/spyder_env_manager/tests/test_config.py b/spyder_env_manager/tests/test_config.py index 7a0f877..f31df99 100644 --- a/spyder_env_manager/tests/test_config.py +++ b/spyder_env_manager/tests/test_config.py @@ -16,14 +16,14 @@ # Local imports from spyder_env_manager.spyder.confpage import SpyderEnvManagerConfigPage -from spyder_env_manager.tests.test_plugin import spyder_env_manager +from spyder_env_manager.tests.test_plugin import spyder_env_manager_conf -def test_config(spyder_env_manager, qtbot): +def test_config(spyder_env_manager_conf, qtbot): """Test that config page can be created and shown.""" dlg = ConfigDialog() page = SpyderEnvManagerConfigPage( - spyder_env_manager, parent=spyder_env_manager.main + spyder_env_manager_conf, parent=spyder_env_manager_conf.main ) page.initialize() dlg.add_page(page) diff --git a/spyder_env_manager/tests/test_plugin.py b/spyder_env_manager/tests/test_plugin.py index e696c58..d8ea083 100644 --- a/spyder_env_manager/tests/test_plugin.py +++ b/spyder_env_manager/tests/test_plugin.py @@ -31,7 +31,7 @@ ) # Constants -LONG_OPERATION_TIMEOUT = 100000 +LONG_OPERATION_TIMEOUT = 600000 OPERATION_TIMEOUT = 50000 IMPORT_FILE_PATH = str(Path(__file__).parent / "data" / "import_env.yml") @@ -49,6 +49,36 @@ def get_plugin(self, plugin_name, error=True): return Mock() +@pytest.fixture +def spyder_env_manager_conf(tmp_path, qtbot, monkeypatch): + # Mocking mainwindow and get_config + window = MainMock() + backends_root_path = tmp_path / "backends" + backends_root_path.mkdir(parents=True) + + def get_conf(self, option, default=None, section=None): + if option == "environments_path": + return str(backends_root_path) + else: + return default + + monkeypatch.setattr(SpyderEnvManagerWidget, "get_conf", get_conf) + + # Setup plugin + plugin = SpyderEnvManager(parent=window, configuration=CONF) + window.setCentralWidget(plugin.get_widget()) + window.show() + + yield plugin + + # Wait for pending operations and close + qtbot.waitUntil( + lambda: plugin.get_widget().actions_enabled, + timeout=LONG_OPERATION_TIMEOUT, + ) + plugin.get_widget().close() + + @pytest.fixture def spyder_env_manager(tmp_path, qtbot, monkeypatch): # Mocking mainwindow and config @@ -65,24 +95,23 @@ def get_conf(self, option, default=None, section=None): monkeypatch.setattr(SpyderEnvManagerWidget, "get_conf", get_conf) # Setup plugin - plugin = SpyderEnvManager(parent=window, configuration=CONF) - widget = plugin.get_widget() - window.setCentralWidget(widget) - qtbot.addWidget(window) + plugin = SpyderEnvManager(parent=window, configuration=Mock()) + window.setCentralWidget(plugin.get_widget()) window.show() yield plugin # Wait for pending operations and close - qtbot.waitUntil(lambda: widget.actions_enabled, timeout=LONG_OPERATION_TIMEOUT) - widget.close() - del plugin - del window + qtbot.waitUntil( + lambda: plugin.get_widget().actions_enabled, + timeout=LONG_OPERATION_TIMEOUT, + ) + plugin.get_widget().close() # ---- Tests # ------------------------------------------------------------------------ -def test_plugin_initial_state(spyder_env_manager, qtbot): +def test_plugin_initial_state(spyder_env_manager): """Check plugin initialization and that actions and widgets have the correct state when initializing.""" widget = spyder_env_manager.get_widget() @@ -105,14 +134,15 @@ def test_plugin_initial_state(spyder_env_manager, qtbot): def test_environment_creation(spyder_env_manager, qtbot, caplog): - """Test creating an environment.""" + """Test creating an environment and installing a package in it.""" caplog.set_level(logging.DEBUG) widget = spyder_env_manager.get_widget() + # Create environment def handle_environment_creation_dialog(): dialog = widget.findChild(CustomParametersDialog) dialog.lineedit_string.setText("test_env") - dialog.combobox_edit.setCurrentText("3.8") + dialog.combobox_edit.setCurrentText("3.8.16") dialog.accept() QTimer.singleShot(100, handle_environment_creation_dialog) @@ -152,3 +182,60 @@ def handle_environment_import_dialog(): lambda: widget.packages_table.source_model.rowCount() == 3, timeout=OPERATION_TIMEOUT, ) + + +def test_environment_package_installation(spyder_env_manager, qtbot, caplog): + """Test creating an environment and installing a package in it.""" + caplog.set_level(logging.DEBUG) + widget = spyder_env_manager.get_widget() + + # Create environment + create_dialog = Mock() + create_dialog.combobox = combobox_mock = Mock() + combobox_mock.currentText = Mock(return_value="conda-like") + create_dialog.lineedit_string = lineedit_string_mock = Mock() + lineedit_string_mock.text = Mock(return_value="test_env") + create_dialog.combobox_edit = combobox_edit_mock = Mock() + combobox_edit_mock.currentText = Mock(return_value="3.9.16") + + assert create_dialog.combobox.currentText() == "conda-like" + assert create_dialog.lineedit_string.text() == "test_env" + assert create_dialog.combobox_edit.currentText() == "3.9.16" + + widget._run_action_for_env( + dialog=create_dialog, action=SpyderEnvManagerWidgetActions.NewEnvironment + ) + + qtbot.waitUntil( + lambda: widget.stack_layout.currentWidget() == widget.packages_table, + timeout=LONG_OPERATION_TIMEOUT, + ) + assert widget.select_environment.currentText() == "test_env" + qtbot.waitUntil( + lambda: widget.packages_table.source_model.rowCount() == 2, + timeout=OPERATION_TIMEOUT, + ) + + # Install package in environment + install_dialog = Mock() + install_dialog.lineedit_string = lineedit_string_mock = Mock() + lineedit_string_mock.text = Mock(return_value="packaging") + install_dialog.combobox = combobox_mock = Mock() + combobox_mock.currentText = Mock(return_value="==") + install_dialog.lineedit_version = lineedit_version_mock = Mock() + lineedit_version_mock.text = Mock(return_value="22.0") + + assert install_dialog.lineedit_string.text() == "packaging" + assert install_dialog.combobox.currentText() == "==" + assert install_dialog.lineedit_version.text() == "22.0" + + widget._run_action_for_env( + dialog=install_dialog, action=SpyderEnvManagerWidgetActions.InstallPackage + ) + + qtbot.waitUntil( + lambda: widget.packages_table.source_model.rowCount() == 3, + timeout=LONG_OPERATION_TIMEOUT, + ) + assert widget.packages_table.get_package_info(0)["name"] == "packaging" + assert widget.packages_table.get_package_info(0)["version"] == "22.0"