Rocket

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit b9c3a5c64bf20b1b83bfb709d117970156b0fc00
parent d8ada552c9c95bff61a8019fa74b7210005d811d
Author: jeb <jeb@jebrosen.com>
Date:   Sun, 30 Dec 2018 13:52:08 -0800

Remove test bootstrapping.

Diffstat:
Dexamples/todo/bootstrap.sh | 6------
Mexamples/todo/src/main.rs | 37++++++++++++++++---------------------
Mexamples/todo/src/task.rs | 5+++++
Mexamples/todo/src/tests.rs | 5++++-
Mscripts/test.sh | 21---------------------
5 files changed, 25 insertions(+), 49 deletions(-)

diff --git a/examples/todo/bootstrap.sh b/examples/todo/bootstrap.sh @@ -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 @@ -78,32 +78,27 @@ fn index(msg: Option<FlashMessage>, conn: DbConn) -> Template { }) } -fn rocket() -> (Rocket, Option<DbConn>) { - let rocket = rocket::ignite() +fn run_db_migrations(rocket: Rocket) -> Result<Rocket, 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) + } + } +} + +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 @@ -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 @@ -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 @@ -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