aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-03-12 15:33:31 -0500
committerJesse Luehrs <doy@tozt.net>2021-03-12 15:33:31 -0500
commitf069116b8c71775b3a6c83c247608eabc9b6fec1 (patch)
tree261ef405833dd3f4324ad2f06674bd846dfb77a5
parent3ad62c9428e2ca9411f7b993909ce52207871e2d (diff)
downloadtextmode-f069116b8c71775b3a6c83c247608eabc9b6fec1.tar.gz
textmode-f069116b8c71775b3a6c83c247608eabc9b6fec1.zip
add some test scaffolding
-rw-r--r--.gitignore1
-rw-r--r--Cargo.toml3
-rw-r--r--tests/basic.rs23
-rw-r--r--tests/fixtures/bin/Cargo.toml9
l---------tests/fixtures/bin/src/bin/basic.rs1
-rw-r--r--tests/fixtures/mod.rs77
6 files changed, 114 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 96ef6c0..dd4ff2a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
/target
+/tests/fixtures/bin/target
Cargo.lock
diff --git a/Cargo.toml b/Cargo.toml
index 6fced2f..fe1b835 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,6 +19,9 @@ default = []
async = ["blocking", "futures-lite"]
[dev-dependencies]
+assert_cmd = "1.0"
+assert_fs = "1.0"
+escargot = "0.5"
libc = "0.2"
pty-process = { version = "0.1", features = ["backend-smol"] }
smol = "1.2"
diff --git a/tests/basic.rs b/tests/basic.rs
new file mode 100644
index 0000000..71ed857
--- /dev/null
+++ b/tests/basic.rs
@@ -0,0 +1,23 @@
+use std::io::Write as _;
+use std::os::unix::io::AsRawFd as _;
+
+mod fixtures;
+
+#[test]
+fn test_basic() {
+ fixtures::run_fixture("basic", true, |pty| {
+ pty.write_all(b"a").unwrap();
+ assert_eq!(fixtures::read(pty), b"\x1b[6;6Hfoo");
+
+ pty.write_all(b"a").unwrap();
+ assert!(!fixtures::read_ready(pty.as_raw_fd()));
+
+ pty.write_all(b"a").unwrap();
+ assert_eq!(
+ fixtures::read(pty),
+ b"\x1b[9;9H\x1b[32mbar\x1b[12;12H\x1b[mbaz"
+ );
+
+ pty.write_all(b"a").unwrap();
+ });
+}
diff --git a/tests/fixtures/bin/Cargo.toml b/tests/fixtures/bin/Cargo.toml
new file mode 100644
index 0000000..83fff5e
--- /dev/null
+++ b/tests/fixtures/bin/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "textmode_tests"
+version = "0.1.0"
+authors = ["Jesse Luehrs <doy@tozt.net>"]
+edition = "2018"
+
+[dependencies]
+textmode = { path = "../../.." }
+vt100 = "*"
diff --git a/tests/fixtures/bin/src/bin/basic.rs b/tests/fixtures/bin/src/bin/basic.rs
new file mode 120000
index 0000000..8cd8f58
--- /dev/null
+++ b/tests/fixtures/bin/src/bin/basic.rs
@@ -0,0 +1 @@
+../../../../../examples/basic.rs \ No newline at end of file
diff --git a/tests/fixtures/mod.rs b/tests/fixtures/mod.rs
new file mode 100644
index 0000000..766886c
--- /dev/null
+++ b/tests/fixtures/mod.rs
@@ -0,0 +1,77 @@
+use pty_process::Command as _;
+use std::io::Read as _;
+use std::os::unix::io::AsRawFd as _;
+
+pub fn run_fixture<F>(name: &str, screenguard: bool, f: F)
+where
+ F: FnOnce(&mut std::fs::File),
+{
+ let temp = assert_fs::TempDir::new().unwrap();
+ let run = escargot::CargoBuild::new()
+ .bin(name)
+ .current_release()
+ .current_target()
+ .manifest_path("tests/fixtures/bin/Cargo.toml")
+ .target_dir(temp.path())
+ .run()
+ .unwrap();
+ let mut cmd = run.command();
+ let mut child = cmd
+ .spawn_pty(Some(&pty_process::Size::new(24, 80)))
+ .unwrap();
+
+ if screenguard {
+ assert!(read_ready(child.pty().as_raw_fd()));
+ let mut buf = vec![0u8; 1024];
+ let bytes = child.pty().read(&mut buf).unwrap();
+ buf.truncate(bytes);
+ assert_eq!(&buf[..], b"\x1b7\x1b[?47h\x1b[2J\x1b[H\x1b[?25h");
+ }
+
+ f(child.pty_mut());
+
+ if screenguard {
+ assert!(read_ready(child.pty().as_raw_fd()));
+ let mut buf = vec![0u8; 1024];
+ let bytes = child.pty().read(&mut buf).unwrap();
+ buf.truncate(bytes);
+ assert_eq!(&buf[..], b"\x1b[?47l\x1b8\x1b[?25h");
+ }
+
+ let status = child.wait().unwrap();
+ assert!(status.success());
+}
+
+pub fn read(f: &mut std::fs::File) -> Vec<u8> {
+ assert!(read_ready(f.as_raw_fd()));
+ let mut buf = vec![0u8; 1024];
+ let bytes = f.read(&mut buf).unwrap();
+ buf.truncate(bytes);
+ buf
+}
+
+pub fn read_ready(fd: std::os::unix::io::RawFd) -> bool {
+ let mut set = nix::sys::select::FdSet::new();
+ set.insert(fd);
+ let timeout = libc::timeval {
+ tv_sec: 0,
+ tv_usec: 100_000,
+ };
+ let timeout = &mut nix::sys::time::TimeVal::from(timeout);
+ match nix::sys::select::select(
+ None,
+ Some(&mut set),
+ None,
+ None,
+ Some(timeout),
+ ) {
+ Ok(n) => {
+ if n > 0 {
+ set.contains(fd)
+ } else {
+ false
+ }
+ }
+ Err(_) => false,
+ }
+}