Skip to content

Commit

Permalink
API: Validate if plugin has parent in get_command_line_options
Browse files Browse the repository at this point in the history
- This can happen in our tests.
- In case the plugin doesn't have a parent, we return our default
cli options.
- Also fix failures in a couple of tests related to cli options.
  • Loading branch information
ccordoba12 committed Apr 14, 2022
1 parent da04f80 commit c1c2525
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion spyder/api/plugins/new_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import logging
import os
import os.path as osp
import sys
from typing import List, Union
import warnings

Expand All @@ -33,6 +34,7 @@
from spyder.api.widgets.main_widget import PluginMainWidget
from spyder.api.widgets.mixins import SpyderActionMixin
from spyder.api.widgets.mixins import SpyderWidgetMixin
from spyder.app.cli_options import get_options
from spyder.config.gui import get_color_scheme, get_font
from spyder.config.manager import CONF
from spyder.config.user import NoDefault
Expand Down Expand Up @@ -671,7 +673,12 @@ def get_command_line_options(self):
See app/cli_options.py for the option names.
"""
return self._main._cli_options
if self._main is not None:
return self._main._cli_options
else:
# This is necessary when the plugin has no parent.
sys_argv = [sys.argv[0]] # Avoid options passed to pytest
return get_options(sys_argv)[0]

# --- API: Mandatory methods to define -----------------------------------
# ------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ def test_window_title(main_window, tmpdir):
projects.open_project(path=path)

# Set non-ascii window title
main_window.window_title = u'اختبار'
main_window._cli_options.window_title = u'اختبار'

# Assert window title is computed without errors
# and has the expected strings
Expand Down
7 changes: 7 additions & 0 deletions spyder/plugins/preferences/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
Testing utilities to be used with pytest.
"""

# Standard library imports
import sys
import types
from unittest.mock import Mock, MagicMock

Expand All @@ -20,6 +22,7 @@
# Local imports
from spyder.api.plugins import Plugins
from spyder.api.plugin_registration.registry import PLUGIN_REGISTRY
from spyder.app.cli_options import get_options
from spyder.config.manager import CONF
from spyder.plugins.preferences.plugin import Preferences
from spyder.utils.icon_manager import ima
Expand All @@ -39,6 +42,10 @@ def __init__(self, parent):

self.console = Mock()

# To provide command line options for plugins that need them
sys_argv = [sys.argv[0]] # Avoid options passed to pytest
self._cli_options = get_options(sys_argv)[0]

PLUGIN_REGISTRY.reset()
PLUGIN_REGISTRY.sig_plugin_ready.connect(self.register_plugin)
PLUGIN_REGISTRY.register_plugin(self, Preferences)
Expand Down

0 comments on commit c1c2525

Please sign in to comment.