From dfd0ecf931b68ea373be1e4e785421d48ca6fed5 Mon Sep 17 00:00:00 2001 From: Ali MJ Al-Nasrawy Date: Sat, 24 Apr 2021 07:28:55 +0300 Subject: 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. --- tests/fixtures/mod.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'tests/fixtures/mod.rs') 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; @@ -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(args: impl IntoIterator) -> TestServer +where + S: AsRef, +{ + 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(args: impl IntoIterator) -> TestServer +where + S: AsRef, +{ + 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() ? */ + } +} -- cgit v1.2.3