-
Notifications
You must be signed in to change notification settings - Fork 3
/
conftest.py
92 lines (75 loc) · 2.58 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs
import contextlib
import json
import os
import subprocess
import sys
import tempfile
from pathlib import Path
from typing import Optional
import pytest
def get_git_root() -> Path:
try:
almost = codecs.decode(
subprocess.check_output(["git", "rev-parse", "--show-toplevel"]), "utf-8"
)
return Path(almost.rstrip())
except subprocess.CalledProcessError as e:
print(e)
return Path.cwd()
@contextlib.contextmanager
def as_cwd(new_dir: os.PathLike, *args, **kwargs):
old_cwd = Path.cwd()
try:
os.chdir(new_dir)
yield
finally:
os.chdir(old_cwd)
@pytest.fixture(scope="session")
def env(listen_addr: Optional[os.PathLike] =None):
# Context manager for env
if not os.environ.get("NVIM_LISTEN_ADDRESS"):
if listen_addr is None:
try:
listen_addr = tempfile.TemporaryDirectory()
finally:
listen_addr._cleanup(listen_addr.name, '')
os.environ.putenv("NVIM_LISTEN_ADDRESS", listen_addr.name)
@pytest.fixture
def old_vim():
# now that i'm rereading it this is such a weird way of settings things up.
child_argv = os.environ.get("NVIM_CHILD_ARGV")
# so if we're on windows should we have shelltemp set or not?
listen_address = os.environ.get("NVIM_LISTEN_ADDRESS")
if child_argv is None and listen_address is None:
child_argv = '["nvim", "-u", "NONE", "--embed", "--headless"]'
if child_argv is not None:
editor = pynvim.attach("child", argv=json.loads(child_argv))
else:
# is an assert necessary in the fixture?
assert listen_address is None or listen_address != ""
editor = pynvim.attach("socket", path=listen_address)
return editor
@pytest.fixture(scope="session")
def vim():
inside_nvim = os.environ.get("NVIM_LISTEN_ADDRESS")
if inside_nvim is None:
# If we aren't running inside a `:terminal`, just exec nvim.
if sys.argv[:1] == "script_host.py":
sys.argv.pop()
os.execvp('nvim', sys.argv)
if inside_nvim:
editor = pynvim.attach("socket", path=inside_nvim)
else:
editor = pynvim.attach("stdio", sys.argv)
return editor
if __name__ == "__main__":
from py._path.local import LocalPath
# this raises while inside of nvim
with as_cwd(get_git_root()):
local_path = LocalPath(os.path.abspath(".") + "/python3/pynvim.py")
pynvim = local_path.pyimport()
import selectors
selectors._fileobj_to_fd(open("python3/pynvim.py"))