aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/tutorial/mod.rs39
1 files changed, 28 insertions, 11 deletions
diff --git a/tests/tutorial/mod.rs b/tests/tutorial/mod.rs
index 576d3af..54f9c9f 100644
--- a/tests/tutorial/mod.rs
+++ b/tests/tutorial/mod.rs
@@ -1,8 +1,12 @@
use std::env::set_current_dir;
use std::fs;
-use std::io::Read;
+use std::io::{BufRead, BufReader};
use std::path::Path;
-use std::process::Command;
+use std::process::{Command, Stdio};
+use std::thread::sleep;
+use std::time::Duration;
+
+use rand::prelude::*;
trait ExitStatusExt {
fn check(self);
@@ -18,13 +22,13 @@ const CARGO: &str = env!("CARGO");
const PROJECT_DIR: &str = env!("CARGO_MANIFEST_DIR");
const TOSIN_ADMIN: &str = env!("CARGO_BIN_EXE_tosin-admin");
-fn get(url: &'static str) -> (hyper::StatusCode, String) {
+fn get(url: &str) -> (hyper::StatusCode, String) {
let get = async {
- use hyper::{Client, Uri};
+ use hyper::Client;
let client = Client::new();
- let res = client.get(Uri::from_static(url)).await.unwrap();
+ let res = client.get(url.parse().unwrap()).await.unwrap();
let status = res.status();
@@ -44,6 +48,11 @@ pub fn step1(dest: &str) {
// tosin-admin start-project {dest}
set_current_dir(PROJECT_DIR).unwrap();
set_current_dir("target").unwrap();
+ if fs::metadata("tests").is_err() {
+ fs::create_dir("tests").unwrap();
+ }
+ set_current_dir("tests").unwrap();
+ fs::write("Cargo.toml", "[workspace]\nmembers = ['tutorial1']").unwrap();
if fs::metadata(dest).is_ok() {
fs::remove_dir_all(dest).unwrap();
}
@@ -59,20 +68,28 @@ pub fn step1(dest: &str) {
assert!(fs::read_to_string("src/main.rs").unwrap().contains("tosin::main!"));
// cargo run run-server
+ let port = thread_rng().gen_range(8081u16..9000u16);
let mut server = Command::new(CARGO)
- .args(&["run", "run-server", "8069"])
+ .args(&["run", "run-server", &format!("{}", port)])
+ .stdout(Stdio::piped())
.spawn()
.unwrap();
- let server_poke = get("http://127.0.0.1:8069");
+ let mut server_output = String::new();
+ let server_stdout = server.stdout.take().unwrap();
+ let mut server_stdout = BufReader::new(server_stdout);
+ server_stdout.read_line(&mut server_output).unwrap();
+ assert!(server_output.contains(&format!("http://127.0.0.1:{}", port)));
+ sleep(Duration::from_secs_f32(0.5));
+ let server_poke = get(&format!("http://127.0.0.1:{}/", port));
assert_eq!(server_poke.0, hyper::StatusCode::NOT_FOUND);
+ if let Ok(Some(exit_status)) = server.try_wait() {
+ exit_status.check();
+ }
server.kill().unwrap();
- let mut server_stdout = String::new();
- server.stdout.unwrap().read_to_string(&mut server_stdout).unwrap();
- assert!(server_stdout.contains("http://127.0.0.1:8069"));
// could `cargo run start-app polls` or `tosin-admin start-app polls` so
// flip a coin i guess
- if rand::random() {
+ if random() {
let mut cmd = Command::new(CARGO);
cmd.arg("run");
cmd