Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Delay check whether we are running under pytest until run time #6540

Merged
merged 2 commits into from
Mar 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions spyder/app/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@
#==============================================================================
# Create splash screen out of MainWindow to reduce perceived startup time.
#==============================================================================
from spyder.config.base import _, get_image_path, DEV, PYTEST
from spyder.config.base import _, get_image_path, DEV, running_under_pytest

if not PYTEST:
if not running_under_pytest():
SPLASH = QSplashScreen(QPixmap(get_image_path('splash.svg')))
SPLASH_FONT = SPLASH.font()
SPLASH_FONT.setPixelSize(10)
Expand Down Expand Up @@ -451,7 +451,7 @@ def signal_handler(signum, frame=None):
self.layout_toolbar = None
self.layout_toolbar_actions = []

if PYTEST:
if running_under_pytest():
# Show errors in internal console when testing.
CONF.set('main', 'show_internal_errors', False)

Expand Down Expand Up @@ -3114,7 +3114,7 @@ def run_spyder(app, options, args):
# the window
app.focusChanged.connect(main.change_last_focused_widget)

if not PYTEST:
if not running_under_pytest():
app.exec_()
return main

Expand All @@ -3124,7 +3124,7 @@ def run_spyder(app, options, args):
#==============================================================================
def main():
"""Main function"""
if PYTEST:
if running_under_pytest():
try:
from unittest.mock import Mock
except ImportError:
Expand Down
11 changes: 6 additions & 5 deletions spyder/app/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

# Local imports
from spyder.app.cli_options import get_options
from spyder.config.base import PYTEST, get_conf_path, running_in_mac_app
from spyder.config.base import (get_conf_path, running_in_mac_app,
running_under_pytest)
from spyder.config.main import CONF
from spyder.utils.external import lockfile
from spyder.py3compat import is_unicode
Expand Down Expand Up @@ -64,7 +65,7 @@ def main():
options to the application.
"""
# Parse command line options
if PYTEST:
if running_under_pytest():
try:
from unittest.mock import Mock
except ImportError:
Expand Down Expand Up @@ -146,7 +147,7 @@ def main():
# executing this script because it doesn't make
# sense
from spyder.app import mainwindow
if PYTEST:
if running_under_pytest():
return mainwindow.main()
else:
mainwindow.main()
Expand All @@ -155,7 +156,7 @@ def main():
if lock_created:
# Start a new instance
from spyder.app import mainwindow
if PYTEST:
if running_under_pytest():
return mainwindow.main()
else:
mainwindow.main()
Expand All @@ -169,7 +170,7 @@ def main():
"instance, please pass to it the --new-instance option")
else:
from spyder.app import mainwindow
if PYTEST:
if running_under_pytest():
return mainwindow.main()
else:
mainwindow.main()
Expand Down
17 changes: 11 additions & 6 deletions spyder/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@
TEST = os.environ.get('SPYDER_TEST')


# To do some adjustments for pytest
# This env var is defined in runtests.py
PYTEST = os.environ.get('SPYDER_PYTEST')
def running_under_pytest():
"""
Return True if currently running under py.test.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is trivially minor, but though while PEP 257 allows it, it seems to be convention that we put the summary one-liner on the first line, next to the '''.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really? I always thought it was the other way around ... I had a quick and random look in our code base. To me, it looks like we have far too few multi-line docstrings and that there is no clear preference for either style. We may need an executive decision here.

Copy link
Member

@goanpeca goanpeca Feb 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm actually I like either

def func():
    """Summary."""

or

def func():
    """
    Summary.

    Something extra.
    """

I don't like this at all :-p

def func():
    """Summary.

    Something extra.
    """

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for

def func():
    """
    Summary.

    Some
    """

I like to write docstrings this way too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer it as well. My concern was only with regard to consistency with the rest of Spyder's codebase, which seemed to mostly use the """Summary line''' form, but I'd be happy to switch.


This function is used to do some adjustment for testing. The environment
variable SPYDER_PYTEST is defined in conftest.py.
"""
return bool(os.environ.get('SPYDER_PYTEST'))


#==============================================================================
Expand Down Expand Up @@ -119,7 +124,7 @@ def get_home_dir():
def get_conf_path(filename=None):
"""Return absolute path for configuration file with specified filename"""
# Define conf_dir
if PYTEST:
if running_under_pytest():
import py
from _pytest.tmpdir import get_user
conf_dir = osp.join(str(py.path.local.get_temproot()),
Expand All @@ -139,7 +144,7 @@ def get_conf_path(filename=None):

# Create conf_dir
if not osp.isdir(conf_dir):
if PYTEST:
if running_under_pytest():
os.makedirs(conf_dir)
else:
os.mkdir(conf_dir)
Expand Down Expand Up @@ -293,7 +298,7 @@ def get_interface_language():
locale_language = DEFAULT_LANGUAGE

# Tests expect English as the interface language
if PYTEST:
if running_under_pytest():
locale_language = DEFAULT_LANGUAGE

language = DEFAULT_LANGUAGE
Expand Down
10 changes: 6 additions & 4 deletions spyder/plugins/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

# Local imports
from spyder import dependencies
from spyder.config.base import _, get_conf_path, PYTEST
from spyder.config.base import _, get_conf_path, running_under_pytest
from spyder.config.main import (CONF, RUN_CELL_SHORTCUT,
RUN_CELL_AND_ADVANCE_SHORTCUT)
from spyder.config.utils import (get_edit_filetypes, get_edit_filters,
Expand Down Expand Up @@ -452,7 +452,8 @@ def __init__(self, parent, ignore_last_opened_files=False):

# Don't start IntrospectionManager when running tests because
# it consumes a lot of memory
if PYTEST and not os.environ.get('SPY_TEST_USE_INTROSPECTION'):
if (running_under_pytest()
and not os.environ.get('SPY_TEST_USE_INTROSPECTION')):
try:
from unittest.mock import Mock
except ImportError:
Expand Down Expand Up @@ -1861,7 +1862,7 @@ def load(self, filenames=None, goto=None, word='', editorwindow=None,
osp.splitext(filename0)[1])
else:
selectedfilter = ''
if not PYTEST:
if not running_under_pytest():
filenames, _sf = getopenfilenames(
parent_widget,
_("Open file"), basedir,
Expand Down Expand Up @@ -2370,7 +2371,8 @@ def run_file(self, debug=False):
if self.dialog_size is not None:
dialog.resize(self.dialog_size)
dialog.setup(fname)
if CONF.get('run', 'open_at_least_once', not PYTEST):
if CONF.get('run', 'open_at_least_once',
not running_under_pytest()):
# Open Run Config dialog at least once: the first time
# a script is ever run in Spyder, so that the user may
# see it at least once and be conscious that it exists
Expand Down
4 changes: 2 additions & 2 deletions spyder/plugins/ipythonconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# Local imports
from spyder import dependencies
from spyder.config.base import (_, DEV, get_conf_path, get_home_dir,
get_module_path, PYTEST)
get_module_path, running_under_pytest)
from spyder.config.main import CONF
from spyder.plugins import SpyderPluginWidget
from spyder.plugins.configdialog import PluginConfigPage
Expand Down Expand Up @@ -1094,7 +1094,7 @@ def set_editor(self):
import1 = "import sys"
# We need to add spy_dir to sys.path so this test can be
# run in our CIs
if PYTEST:
if running_under_pytest():
if os.name == 'nt':
import1 = (import1 +
'; sys.path.append(""{}"")'.format(spy_dir))
Expand Down
4 changes: 2 additions & 2 deletions spyder/utils/external/lockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from time import time as _uniquefloat

import psutil
from spyder.config.base import PYTEST
from spyder.config.base import running_under_pytest
from spyder.py3compat import PY2, to_binary_string

def unique():
Expand Down Expand Up @@ -185,7 +185,7 @@ def lock(self):

# Valid names for main script
names = set(['spyder', 'spyder3', 'bootstrap.py'])
if PYTEST:
if running_under_pytest():
names.add('runtests.py')

# Check the first three command line arguments
Expand Down
4 changes: 2 additions & 2 deletions spyder/widgets/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
QVBoxLayout, QWidget, QListWidget, QListWidgetItem)

# Local imports
from spyder.config.base import _, DEBUG, PYTEST, STDERR, STDOUT
from spyder.config.base import _, DEBUG, STDERR, STDOUT, running_under_pytest
from spyder.config.gui import config_shortcut, get_shortcut
from spyder.config.utils import (get_edit_filetypes, get_edit_filters,
get_filter, is_kde_desktop, is_anaconda)
Expand Down Expand Up @@ -595,7 +595,7 @@ def __init__(self, parent, actions):
self.edit_filters = None

# For testing
self.save_dialog_on_tests = not PYTEST
self.save_dialog_on_tests = not running_under_pytest()

@Slot()
def show_in_external_file_explorer(self, fnames=None):
Expand Down