aboutsummaryrefslogtreecommitdiffstats
path: root/tests/fixtures
diff options
context:
space:
mode:
authorAli MJ Al-Nasrawy <alimjalnasrawy@gmail.com>2021-04-24 04:28:55 +0000
committerAli MJ Al-Nasrawy <alimjalnasrawy@gmail.com>2021-08-26 11:21:47 +0000
commitdfd0ecf931b68ea373be1e4e785421d48ca6fed5 (patch)
tree115986299dcdeccc0bf2fac00da32dc64759ec1f /tests/fixtures
parentUpgrade deps (diff)
downloadminiserve-dfd0ecf931b68ea373be1e4e785421d48ca6fed5.tar.gz
miniserve-dfd0ecf931b68ea373be1e4e785421d48ca6fed5.zip
tests: Refactor!
Remove duplicate code responsible for the initial setup and teardown of the test binary. This introduces `TestServer` as a resource manager for a running miniserve binary, which can be created with the fixtures `server()` and `server_no_stderr()` It also provides convenience function for handling server url.
Diffstat (limited to 'tests/fixtures')
-rw-r--r--tests/fixtures/mod.rs87
1 files changed, 87 insertions, 0 deletions
diff --git a/tests/fixtures/mod.rs b/tests/fixtures/mod.rs
index 1cf6c59..f143413 100644
--- a/tests/fixtures/mod.rs
+++ b/tests/fixtures/mod.rs
@@ -1,7 +1,12 @@
+use assert_cmd::prelude::*;
use assert_fs::fixture::TempDir;
use assert_fs::prelude::*;
use port_check::free_local_port;
+use reqwest::Url;
use rstest::fixture;
+use std::process::{Child, Command, Stdio};
+use std::thread::sleep;
+use std::time::Duration;
/// Error type used by tests
pub type Error = Box<dyn std::error::Error>;
@@ -76,3 +81,85 @@ pub fn tmpdir() -> TempDir {
pub fn port() -> u16 {
free_local_port().expect("Couldn't find a free local port")
}
+
+/// Run miniserve as a server; Start with a temporary directory, a free port and some
+/// optional arguments then wait for a while for the server setup to complete.
+#[fixture(args=&[] as &[&str])]
+#[allow(dead_code)]
+pub fn server<S>(args: impl IntoIterator<Item = S>) -> TestServer
+where
+ S: AsRef<std::ffi::OsStr>,
+{
+ let port = port();
+ let tmpdir = tmpdir();
+ let child = Command::cargo_bin("miniserve")
+ .expect("Couldn't find test binary")
+ .arg(tmpdir.path())
+ .arg("-p")
+ .arg(port.to_string())
+ .args(args)
+ .stdout(Stdio::null())
+ .spawn()
+ .expect("Couldn't run test binary");
+
+ sleep(Duration::from_secs(1));
+ TestServer::new(port, tmpdir, child)
+}
+
+/// Same as `server()` but ignore stderr
+#[fixture(args=&[] as &[&str])]
+#[allow(dead_code)]
+pub fn server_no_stderr<S>(args: impl IntoIterator<Item = S>) -> TestServer
+where
+ S: AsRef<std::ffi::OsStr>,
+{
+ let port = port();
+ let tmpdir = tmpdir();
+ let child = Command::cargo_bin("miniserve")
+ .expect("Couldn't find test binary")
+ .arg(tmpdir.path())
+ .arg("-p")
+ .arg(port.to_string())
+ .args(args)
+ .stdout(Stdio::null())
+ .stderr(Stdio::null())
+ .spawn()
+ .expect("Couldn't run test binary");
+
+ sleep(Duration::from_secs(1));
+ TestServer::new(port, tmpdir, child)
+}
+
+#[allow(dead_code)]
+pub struct TestServer {
+ port: u16,
+ tmpdir: TempDir,
+ child: Child,
+}
+
+#[allow(dead_code)]
+impl TestServer {
+ pub fn new(port: u16, tmpdir: TempDir, child: Child) -> Self {
+ Self {
+ port,
+ tmpdir,
+ child,
+ }
+ }
+ pub fn url(&self) -> Url {
+ Url::parse(&format!("http://localhost:{}", self.port)).unwrap()
+ }
+ pub fn path(&self) -> &std::path::Path {
+ self.tmpdir.path()
+ }
+ pub fn port(&self) -> u16 {
+ self.port
+ }
+}
+
+impl Drop for TestServer {
+ fn drop(&mut self) {
+ self.child.kill().expect("Couldn't kill test server");
+ /* TODO may need .wait() ? */
+ }
+}