Skip to content

Commit 86cc50e

Browse files
authored
test: always use the same python regardless of $PATH #547
Problem: The embedded neovim fixture instance for testing picks up `python3` on the `$PATH`. This can be different from the current python. (Example) $ which python3 /usr/local/bin/python3 $ which python3.11 $HOME/.pyenvs/versions/3.11.5/bin/python3.11 $ python3.11 -m pytest then neovim will have `/usr/local/bin/python3` as the python3 host (however should be `python3.11`), so the behavior of `:python` and the `pynvim` module especially when it has local changes during development) can be different. Solution: The embedded nvim instance should always pin `g:python3_host_prog` to `sys.executable`.
1 parent 71d2d65 commit 86cc50e

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

test/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import gc
44
import json
55
import os
6+
import sys
67
from typing import Generator
78

89
import pytest
@@ -26,7 +27,8 @@ def vim() -> Generator[pynvim.Nvim, None, None]:
2627
"-n", # no swap file
2728
"--embed",
2829
"--headless",
29-
"-c", "let g:python3_host_prog='python3'", # workaround neovim/neovim#25316
30+
# Always use the same exact python executable regardless of $PATH
31+
"--cmd", f"let g:python3_host_prog='{sys.executable}'",
3032
])
3133

3234
if child_argv is not None:

test/test_vim.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
2+
import sys
23
import tempfile
3-
from typing import Any
4+
from pathlib import Path
45

56
import pytest
67

@@ -199,15 +200,25 @@ def test_hash(vim: Nvim) -> None:
199200
assert d[vim.current.buffer] == 'beta'
200201

201202

202-
def test_python3(vim: Nvim, tmpdir: Any) -> None:
203+
def test_python3(vim: Nvim) -> None:
204+
"""Tests whether python3 host can load."""
203205
python3_prog = vim.command_output('echom provider#python3#Prog()')
204206
python3_err = vim.command_output('echom provider#python3#Error()')
205207
assert python3_prog != "", python3_err
208+
assert python3_prog == sys.executable
206209

210+
assert sys.executable == vim.command_output(
211+
'python3 import sys; print(sys.executable)')
212+
213+
assert 1 == vim.eval('has("python3")')
214+
215+
216+
def test_python_cwd(vim: Nvim, tmp_path: Path) -> None:
207217
vim.command('python3 import os')
208218
cwd_before = vim.command_output('python3 print(os.getcwd())')
209219

210-
vim.command('cd {}'.format(tmpdir.strpath))
220+
# handle DirChanged #296
221+
vim.command('cd {}'.format(str(tmp_path)))
211222
cwd_vim = vim.command_output('pwd')
212223
cwd_python = vim.command_output('python3 print(os.getcwd())')
213224
assert cwd_python == cwd_vim

0 commit comments

Comments
 (0)