This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split demo plugin test cases to facilitate testing in CI
- Loading branch information
Showing
5 changed files
with
202 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import os | ||
import subprocess | ||
import time | ||
import pytest | ||
import pynvim | ||
|
||
# mock nvim in a new address in case you're already running in your machine | ||
nvim_test_addr = "/tmp/nvim_test_addr" | ||
manifest_f = os.path.expanduser("~/.config/nvim/settings/demo-plugin.vim") | ||
assert os.environ.get("NVIM_LISTEN_ADDRESS") == nvim_test_addr | ||
|
||
|
||
class PluginLog(object): | ||
|
||
"""Abstract plugin log functionalities""" | ||
|
||
def __init__(self, log_file: str) -> None: | ||
"""Constructor of PluginLog.""" | ||
self.log_file = log_file | ||
|
||
def match_line(self, line: str) -> bool: | ||
"""Match a str line in the log file.""" | ||
if not line: | ||
return False | ||
|
||
with open(self.log_file) as f: | ||
for read_line in f.readlines(): | ||
if line in read_line: | ||
return True | ||
return False | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def get_nvim(): | ||
|
||
nvim_pid = spawn_nvim(" -u {} ".format(manifest_f)) | ||
assert nvim_pid | ||
nvim = pynvim.attach("socket", path=nvim_test_addr) | ||
assert nvim | ||
return nvim | ||
|
||
|
||
def spawn_nvim(cmd: str = ""): | ||
"""Spawn nvim in background.""" | ||
|
||
if os.path.exists(nvim_test_addr): | ||
print("removing ", nvim_test_addr) | ||
os.remove(nvim_test_addr) | ||
|
||
nvim_cmd = "nvim " + cmd + " --headless &" | ||
popen = subprocess.Popen( | ||
[nvim_cmd], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
universal_newlines=True, | ||
shell=True, | ||
) | ||
print("spawned: " + nvim_cmd + " pid: " + str(popen.pid)) | ||
|
||
time.sleep(0.5) | ||
assert not popen.returncode | ||
assert popen.pid | ||
return popen.pid |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import subprocess | ||
import pytest | ||
import os | ||
import time | ||
from nvim_fixtures import spawn_nvim, PluginLog | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def spawn_demoplugin() -> (int, int): | ||
"""Force the bootstrap of the plugin for testing purposes.""" | ||
|
||
spawn_nvim() | ||
|
||
popen = subprocess.Popen( | ||
["demo-plugin"], | ||
shell=True, | ||
universal_newlines=True, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
) | ||
|
||
time.sleep(0.5) | ||
assert not popen.returncode | ||
plugin_pid = popen.pid | ||
return plugin_pid | ||
|
||
|
||
class TestDemoPluginBootstrap(object): | ||
|
||
"""TestDemoPlugin. | ||
This test suite forces the bootstrap of the plugin | ||
by directly calling the plugin binary. | ||
""" | ||
|
||
def test_is_running(self, spawn_demoplugin): | ||
pid = spawn_demoplugin | ||
|
||
f_log = os.environ.get("NVIMHOST_LOG_FILE") | ||
assert os.path.exists(f_log) | ||
assert pid | ||
|
||
def test_manifest_exists(self, spawn_demoplugin): | ||
"""Make sure the plugin is generating the manifest vim file.""" | ||
manifest_file = "~/.config/nvim/settings/demo-plugin.vim" | ||
assert os.path.exists(os.path.expanduser(manifest_file)) | ||
|
||
def test_started(self, spawn_demoplugin): | ||
"""Check if the plugin properly started.""" | ||
f_log = os.environ.get("NVIMHOST_LOG_FILE") | ||
pl = PluginLog(f_log) | ||
lines = [ | ||
"Main thread connected to nvim", | ||
"cbThread ready", | ||
"Setting g:demoplugin_channel", | ||
"Plugin demoplugin is ready", | ||
] | ||
for line in lines: | ||
assert pl.match_line(line) | ||
|
||
def test_kill(self, spawn_demoplugin): | ||
plugin_pid = spawn_demoplugin | ||
assert plugin_pid | ||
|
||
nvim_out = subprocess.check_output( | ||
["ps -aux"], shell=True, universal_newlines=True | ||
) | ||
for line in nvim_out.split("\n"): | ||
if "nvim --headless" in line: | ||
col = line.split() | ||
nvim_pid = str(col[1]).strip() | ||
print("found nvim --headless, pid ", nvim_pid) | ||
out = subprocess.check_output( | ||
["kill " + str(nvim_pid)], | ||
shell=True, | ||
stderr=subprocess.STDOUT, | ||
universal_newlines=True, | ||
) | ||
assert not out | ||
|
||
f_log = os.environ.get("NVIMHOST_LOG_FILE") | ||
os.remove(f_log) | ||
assert not os.path.exists(f_log) | ||
assert not out | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import pytest | ||
import pynvim | ||
from nvim_fixtures import get_nvim | ||
|
||
|
||
class TestDemoPluginFunctions(object): | ||
|
||
""" | ||
TestDemoPluginFunctions. | ||
This test suite tests all demo-plugin funtions by leveraging pynvim | ||
official client (since it's super stable). | ||
""" | ||
|
||
def test_rpc_connection(self, get_nvim): | ||
"""Test pynvim socket RPC connection.""" | ||
assert get_nvim | ||
|
||
def test_greet(self, get_nvim): | ||
"""Test demo-plugin Greet function.""" | ||
nvim = get_nvim | ||
res = nvim.command_output("echo Greet('D')") | ||
assert res == "Hello D" | ||
|
||
def test_greet_wrong_args(self, get_nvim): | ||
"""Test demo-plugin Greet function.""" | ||
nvim = get_nvim | ||
with pytest.raises(pynvim.api.nvim.NvimError) as exc: | ||
nvim.command_output("echo Greet(1)") | ||
assert "Wrong function argument types" in str(exc.value) | ||
|
||
def test_sum_begin_to_end(self, get_nvim): | ||
"""Test demo-plugin SumBeginToEnd function.""" | ||
nvim = get_nvim | ||
res = nvim.command_output("echo SumBeginToEnd(0, 10)") | ||
assert res == str(sum(range(0, 10))) | ||
|
||
def test_set_var_value_sync(self, get_nvim): | ||
"""Test demo-plugin SetVarValueSync function.""" | ||
nvim = get_nvim | ||
nvim.command("call SetVarValueSync(555)") | ||
res = nvim.command_output("echo g:test_var_value") | ||
assert res == "555" | ||
|
||
def test_set_var_value_async(self, get_nvim): | ||
"""Test demo-plugin SetVarValueAsync function.""" | ||
nvim = get_nvim | ||
nvim.command("call SetVarValueAsync(56)") | ||
res = nvim.command_output("echo g:testasync_var_value") | ||
assert res == "56" |