Skip to content

Commit

Permalink
Testing: Fix tests related to completions in different Jedi environments
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Aug 21, 2024
1 parent 210faf0 commit 22873aa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
31 changes: 18 additions & 13 deletions .github/scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#!/bin/bash -ex

# Auxiliary functions
install_spyder_kernels() {
echo "Installing subrepo version of spyder-kernels in "$1"..."

pushd external-deps/spyder-kernels

if [ "$OS" = "win" ]; then
# `conda run` fails on Windows without a clear reason
/c/Miniconda/envs/"$1"/python -m pip install -q .
else
conda run -n "$1" python -m pip install -q .
fi

popd
}

# Install gdb
if [ "$USE_GDB" = "true" ]; then
micromamba install gdb -c conda-forge -q -y
Expand Down Expand Up @@ -65,23 +81,12 @@ fi

# Create environment for Jedi environment tests
conda create -n jedi-test-env -q -y python=3.9 flask
install_spyder_kernels jedi-test-env
conda list -n jedi-test-env

# Create environment to test conda env activation before launching a kernel
conda create -n spytest-ž -q -y -c conda-forge python=3.9

# Install subrepo version of Spyder-kernels in that env
pushd external-deps/spyder-kernels

if [ "$OS" = "win" ]; then
# `conda run` fails on Windows without a clear reason
/c/Miniconda/envs/spytest-ž/python -m pip install .
else
conda run -n spytest-ž python -m pip install .
fi

popd

install_spyder_kernels spytest-ž
conda list -n spytest-ž

# Install pyenv on Linux systems
Expand Down
40 changes: 23 additions & 17 deletions spyder/app/tests/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3344,44 +3344,50 @@ def test_preferences_shortcut_reset_regression(main_window, qtbot):

@pytest.mark.order(1)
@flaky(max_runs=3)
@pytest.mark.order(before="test_PYTHONPATH_in_consoles")
@pytest.mark.skipif(not is_anaconda(), reason='Only works with Anaconda')
@pytest.mark.skipif(not running_in_ci(), reason='Only works on CIs')
def test_preferences_change_interpreter(qtbot, main_window):
"""Test that on main interpreter change signal is emitted."""
@pytest.mark.skipif(
not sys.platform.startswith("linux"),
reason="Only works on Linux on CIs but passes locally"
)
def test_change_lsp_interpreter(qtbot, main_window):
"""
Test that the LSP Python interpreter changes when switching consoles for
different envs.
"""
# Wait until the window is fully up
shell = main_window.ipyconsole.get_current_shellwidget()
qtbot.waitUntil(
lambda: shell.spyder_kernel_ready and shell._prompt_html is not None,
timeout=SHELL_TIMEOUT,
)

# Check original pyls configuration
# Check original pylsp configuration
lsp = main_window.completions.get_provider('lsp')
config = lsp.generate_python_config()
jedi = config['configurations']['pylsp']['plugins']['jedi']
assert jedi['environment'] == sys.executable
assert jedi['extra_paths'] == []

# Get conda env to use
conda_env = get_list_conda_envs()['Conda: jedi-test-env'][0]
# Get new interpreter to use
new_interpreter = get_list_conda_envs()['Conda: jedi-test-env'][0]

# Change main interpreter on preferences
dlg, index, page = preferences_dialog_helper(
qtbot, main_window, 'main_interpreter'
)
page.cus_exec_radio.radiobutton.setChecked(True)
page.cus_exec_combo.combobox.setCurrentText(conda_env)

main_interpreter = main_window.main_interpreter
# Create console for new interpreter
ipyconsole = main_window.ipyconsole
with qtbot.waitSignal(
main_interpreter.sig_interpreter_changed, timeout=5000, raising=True
ipyconsole.sig_interpreter_changed, timeout=SHELL_TIMEOUT, raising=True
):
dlg.ok_btn.animateClick()
ipyconsole.get_widget().create_environment_client(
"jedi-test-env",
new_interpreter
)

# Check updated pyls configuration
# Check updated pylsp configuration
qtbot.wait(1000) # Account for debounced timeout when setting interpreter
config = lsp.generate_python_config()
jedi = config['configurations']['pylsp']['plugins']['jedi']
assert jedi['environment'] == conda_env
assert jedi['environment'] == new_interpreter
assert jedi['extra_paths'] == []


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,11 @@
LOCATION = osp.realpath(osp.join(os.getcwd(), osp.dirname(__file__)))


def set_executable_config_helper(completion_plugin, executable=None):
def set_executable_helper(completion_plugin, executable=None):
if executable is None:
completion_plugin.set_conf('executable', sys.executable,
'main_interpreter')
completion_plugin.set_conf('default', True, 'main_interpreter')
completion_plugin.set_conf('custom', False, 'main_interpreter')
completion_plugin._sig_interpreter_changed.emit(sys.executable)
else:
completion_plugin.set_conf('executable', executable,
'main_interpreter')
completion_plugin.set_conf('default', False, 'main_interpreter')
completion_plugin._sig_interpreter_changed.emit(executable)


@pytest.mark.order(1)
Expand Down Expand Up @@ -1019,8 +1014,9 @@ def spam():
@flaky(max_runs=20)
@pytest.mark.skipif(not is_anaconda(), reason='Requires conda to work')
@pytest.mark.skipif(not running_in_ci(), reason="Only meant for CIs")
@pytest.mark.skipif(not sys.platform.startswith('linux'),
reason="Works reliably on Linux")
@pytest.mark.skipif(
not sys.platform.startswith('linux'), reason="Works reliably on Linux"
)
def test_completions_environment(completions_codeeditor, qtbot, tmpdir):
"""
Exercise code completions when using another Jedi environment, i.e. a
Expand All @@ -1045,7 +1041,7 @@ def test_completions_environment(completions_codeeditor, qtbot, tmpdir):
# Set interpreter that has Flask and check we can provide completions for
# it
code_editor.set_text('')
set_executable_config_helper(completion_plugin, py_exe)
set_executable_helper(completion_plugin, py_exe)
completion_plugin.after_configuration_update([])
qtbot.wait(5000)

Expand All @@ -1059,7 +1055,7 @@ def test_completions_environment(completions_codeeditor, qtbot, tmpdir):
assert "flask" in [x['label'] for x in sig.args[0]]
assert code_editor.toPlainText() == 'import flask'

set_executable_config_helper(completion_plugin)
set_executable_helper(completion_plugin)
completion_plugin.after_configuration_update([])
qtbot.wait(5000)

Expand Down

0 comments on commit 22873aa

Please sign in to comment.