From 19b851c71ee8c7499046936771446e6ae6e60c16 Mon Sep 17 00:00:00 2001 From: Melody Horn / boringcactus Date: Sun, 13 Jun 2021 21:05:56 -0600 Subject: finish passing the tutorial 1 tests --- tests/tutorial/mod.rs | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'tests/tutorial') diff --git a/tests/tutorial/mod.rs b/tests/tutorial/mod.rs index 54f9c9f..71cba3d 100644 --- a/tests/tutorial/mod.rs +++ b/tests/tutorial/mod.rs @@ -87,15 +87,8 @@ pub fn step1(dest: &str) { } server.kill().unwrap(); - // could `cargo run start-app polls` or `tosin-admin start-app polls` so - // flip a coin i guess - if random() { - let mut cmd = Command::new(CARGO); - cmd.arg("run"); - cmd - } else { - Command::new(TOSIN_ADMIN) - } + // tosin-admin start-app polls + Command::new(TOSIN_ADMIN) .args(&["start-app", "polls"]) .status().unwrap().check(); assert!(fs::metadata("src/polls/mod.rs").is_ok()); @@ -147,22 +140,32 @@ tosin::main!(urls(), settings()); "#).unwrap(); // poke that new route + 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/polls/"); + 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:{}/polls/", port)); assert_eq!(server_poke.0, hyper::StatusCode::OK); assert_eq!(server_poke.1, "Hello, world. You're at the polls index."); server.kill().unwrap(); // vibe check + let test_tutorial1 = Path::new("src"); let example_tutorial1 = Path::new(PROJECT_DIR).join("examples/tutorial01"); - for file in &["src/main.rs", "src/polls/mod.rs", "src/polls/urls.rs", "src/polls/views.rs"] { - let this_file = fs::read_to_string(file).unwrap(); - let example_tutorial1_path = example_tutorial1.join(file); - let example_tutorial1_file = fs::read_to_string(example_tutorial1_path).unwrap(); - assert_eq!(this_file.trim(), example_tutorial1_file.trim()); + for file in &["main.rs", "polls/mod.rs", "polls/urls.rs", "polls/views.rs"] { + let test_path = test_tutorial1.join(file); + let test_file = fs::read_to_string(test_path).unwrap(); + let example_path = example_tutorial1.join(file); + let example_file = fs::read_to_string(example_path).unwrap(); + assert_eq!(test_file.trim(), example_file.trim()); } } -- cgit v1.2.3