diff --git a/examples/todo/bootstrap.sh b/examples/todo/bootstrap.sh deleted file mode 100755 index 818811fd..00000000 --- a/examples/todo/bootstrap.sh +++ /dev/null @@ -1,6 +0,0 @@ -#! /usr/bin/env bash - -SCRIPT_PATH=$(cd "$(dirname "$0")" ; pwd -P) -DATABASE_URL="${SCRIPT_PATH}/db/db.sqlite" - -rm -f "${DATABASE_URL}" diff --git a/examples/todo/src/main.rs b/examples/todo/src/main.rs index 20aeb7de..8ce8c4aa 100644 --- a/examples/todo/src/main.rs +++ b/examples/todo/src/main.rs @@ -78,32 +78,27 @@ fn index(msg: Option, conn: DbConn) -> Template { }) } -fn rocket() -> (Rocket, Option) { - let rocket = rocket::ignite() +fn run_db_migrations(rocket: Rocket) -> Result { + let conn = DbConn::get_one(&rocket).expect("database connection"); + match embedded_migrations::run(&*conn) { + Ok(()) => Ok(rocket), + Err(e) => { + error!("Failed to run database migrations: {:?}", e); + Err(rocket) + } + } +} + +fn rocket() -> Rocket { + rocket::ignite() .attach(DbConn::fairing()) - .attach(AdHoc::on_attach("Database Migrations", |rocket| { - let conn = DbConn::get_one(&rocket).expect("database connection"); - match embedded_migrations::run(&*conn) { - Ok(()) => Ok(rocket), - Err(e) => { - error!("Failed to run database migrations: {:?}", e); - Err(rocket) - }, - } - })) + .attach(AdHoc::on_attach("Database Migrations", run_db_migrations)) .mount("/", StaticFiles::from("static/")) .mount("/", routes![index]) .mount("/todo", routes![new, toggle, delete]) - .attach(Template::fairing()); - - let conn = match cfg!(test) { - true => DbConn::get_one(&rocket), - false => None, - }; - - (rocket, conn) + .attach(Template::fairing()) } fn main() { - rocket().0.launch(); + rocket().launch(); } diff --git a/examples/todo/src/task.rs b/examples/todo/src/task.rs index a1d2bbc9..4d369172 100644 --- a/examples/todo/src/task.rs +++ b/examples/todo/src/task.rs @@ -50,4 +50,9 @@ impl Task { pub fn delete_with_id(id: i32, conn: &SqliteConnection) -> bool { diesel::delete(all_tasks.find(id)).execute(conn).is_ok() } + + #[cfg(test)] + pub fn delete_all(conn: &SqliteConnection) -> bool { + diesel::delete(all_tasks).execute(conn).is_ok() + } } diff --git a/examples/todo/src/tests.rs b/examples/todo/src/tests.rs index 206987d7..a9ca6bae 100644 --- a/examples/todo/src/tests.rs +++ b/examples/todo/src/tests.rs @@ -16,9 +16,12 @@ static DB_LOCK: Mutex<()> = Mutex::new(()); macro_rules! run_test { (|$client:ident, $conn:ident| $block:expr) => ({ let _lock = DB_LOCK.lock(); - let (rocket, db) = super::rocket(); + let rocket = super::rocket(); + let db = super::DbConn::get_one(&rocket); let $client = Client::new(rocket).expect("Rocket client"); let $conn = db.expect("failed to get database connection for testing"); + assert!(Task::delete_all(&$conn), "failed to delete all tasks for testing"); + $block }) } diff --git a/scripts/test.sh b/scripts/test.sh index 40986d3d..94df21a9 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -49,24 +49,6 @@ function ensure_trailing_whitespace_free() { fi } -function bootstrap_examples() { - while read -r file; do - bootstrap_script="${file}/bootstrap.sh" - if [ -x "${bootstrap_script}" ]; then - echo " Bootstrapping ${file}..." - - env_vars=$(bash "${bootstrap_script}") - bootstrap_result=$? - if [ $bootstrap_result -ne 0 ]; then - echo " Running bootstrap script (${bootstrap_script}) failed!" - exit 1 - else - eval $env_vars - fi - fi - done < <(find "${EXAMPLES_DIR}" -maxdepth 1 -type d) -} - echo ":: Ensuring all crate versions match..." check_versions_match "${ALL_PROJECT_DIRS[@]}" @@ -128,9 +110,6 @@ elif [ "$1" = "--core" ]; then popd > /dev/null 2>&1 else - echo ":: Bootstrapping examples..." - bootstrap_examples - echo ":: Building and testing libraries..." CARGO_INCREMENTAL=0 cargo test --all-features --all $@ fi