Skip to content

Commit

Permalink
Add test for new_unix_socket
Browse files Browse the repository at this point in the history
Test is marked #[ignore] until travis ci environment includes neovim
binary.

Signed-off-by: Justin Charette <charetjc@gmail.com>
  • Loading branch information
boxofrox committed Mar 13, 2017
1 parent 5488aa3 commit 21d0adc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ log = "0.3"

[target.'cfg(unix)'.dependencies]
unix_socket = "0.5.0"

[dev-dependencies]
tempdir = "0.3"
69 changes: 69 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
extern crate neovim_lib;
extern crate rmp;
extern crate tempdir;

use neovim_lib::session::Session;
use neovim_lib::neovim::Neovim;
use neovim_lib::neovim_api::NeovimApi;
use neovim_lib::Value;

use std::process::Command;
use tempdir::TempDir;

#[ignore]
#[test]
Expand Down Expand Up @@ -41,3 +46,67 @@ fn edit_test() {
let windows = nvim.get_windows().unwrap();
windows[0].set_width(&mut nvim, 10).unwrap();
}

#[cfg(unix)]
#[ignore]
#[test]
fn can_connect_via_unix_socket() {
use std::path::Path;
use std::thread::sleep;
use std::time::{Duration, Instant};

let dir = TempDir::new("neovim-lib.test").expect("Cannot create temporary directory for test.");

let socket_path = dir.path().join("unix_socket");

let _child = Command::new("nvim")
.arg("--embed")
.env("NVIM_LISTEN_ADDRESS", &socket_path)
.spawn()
.expect("Cannot start neovim");

// wait at least 1 second for neovim to start and create socket path.
{
let start = Instant::now();
let one_second = Duration::from_secs(1);
loop {
sleep(Duration::from_millis(100));

if let Ok(_) = std::fs::metadata(&socket_path) {
break;
}

if one_second <= start.elapsed() {
panic!(format!("neovim socket not found at '{:?}'", &socket_path));
}
}
}


let mut session = Session::new_unix_socket(&socket_path)
.expect(&format!("Unable to connect to neovim's unix socket at {:?}",
&socket_path));

session.start_event_loop();

let mut nvim = Neovim::new(session);

let servername = nvim.get_vvar("servername")
.expect("Error retrieving servername from neovim over unix socket");

// let's make sure the servername string and socket path string both match.
match servername {
Value::String(ref name) => {
if Path::new(name) != socket_path {
panic!(format!("Server name does not match socket path! {} != {}",
name,
socket_path.to_str().unwrap()));
}
}
_ => {
panic!(format!("Server name does not match socket path! {:?} != {}",
servername,
socket_path.to_str().unwrap()))
}
}
}

0 comments on commit 21d0adc

Please sign in to comment.