diff options
-rw-r--r-- | src/bin/tosin-admin.rs | 39 | ||||
-rw-r--r-- | tests/tutorial/mod.rs | 35 |
2 files changed, 58 insertions, 16 deletions
diff --git a/src/bin/tosin-admin.rs b/src/bin/tosin-admin.rs index 3eb57a1..6408337 100644 --- a/src/bin/tosin-admin.rs +++ b/src/bin/tosin-admin.rs @@ -1,6 +1,7 @@ use std::env::set_current_dir; use std::fs; use std::io::Write as _; +use std::path::Path; use std::process::Command; use structopt::StructOpt; @@ -26,11 +27,37 @@ fn settings() -> Settings { tosin::main!(urls(), settings()); "#; +const APP_MOD: &str = r#" +pub mod urls; +pub mod views; + +pub use urls::urls; +"#; +const APP_URLS: &str = r#" +use tosin::urls::{UrlMap, url_map}; + +use super::views; + +pub fn urls() -> UrlMap { + todo!("fill in URL map") +} +"#; +const APP_VIEWS: &str = r#" +use tosin::http::{Reply, Response}; + +todo!("write some views"); +"#; + #[derive(StructOpt, Debug)] enum Opt { /// Start a new project/site (can contain multiple apps) StartProject { name: Option<String>, + }, + + /// Start a new app + StartApp { + name: String, } } @@ -66,5 +93,17 @@ fn main() { } } } + Opt::StartApp { name } => { + // TODO make this more robust + if fs::metadata("Cargo.toml").is_ok() { + let app_folder = Path::new("src").join(name); + fs::create_dir(&app_folder).unwrap(); + fs::write(app_folder.join("mod.rs"), APP_MOD).unwrap(); + fs::write(app_folder.join("urls.rs"), APP_URLS).unwrap(); + fs::write(app_folder.join("views.rs"), APP_VIEWS).unwrap(); + } else { + todo!("new standalone app crate maybe??") + } + } } } 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()); } } |