Skip to content

Commit

Permalink
Merge pull request #641 from diesel-rs/sg-dont-panic-in-cli-drop
Browse files Browse the repository at this point in the history
Don't panic while panicking in CLI's tests
  • Loading branch information
sgrif authored Feb 8, 2017
2 parents 6570663 + 9073602 commit c0037b1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
9 changes: 9 additions & 0 deletions diesel_cli/tests/support/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ impl CommandResult {
pub fn code(&self) -> i32 {
self.output.status.code().unwrap()
}

#[allow(dead_code)]
pub fn result(self) -> Result<Self, Self> {
if self.is_success() {
Ok(self)
} else {
Err(self)
}
}
}

fn path_to_diesel_cli() -> PathBuf {
Expand Down
15 changes: 15 additions & 0 deletions diesel_cli/tests/support/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
macro_rules! try_drop {
($e:expr, $msg:expr) => { match $e {
Ok(x) => x,
Err(e) => {
use ::std::io::{Write, stderr};
if ::std::thread::panicking() {
write!(stderr(), "{}: {:?}", $msg, e);
return;
} else {
panic!("{}: {:?}", $msg, e);
}
}
}}
}

mod command;
mod project_builder;

Expand Down
4 changes: 2 additions & 2 deletions diesel_cli/tests/support/postgres_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl Database {
impl Drop for Database {
fn drop(&mut self) {
let (database, postgres_url) = self.split_url();
let conn = PgConnection::establish(&postgres_url).unwrap();
let conn = try_drop!(PgConnection::establish(&postgres_url), "Couldn't connect to database");
conn.silence_notices(|| {
conn.execute(&format!("DROP DATABASE IF EXISTS {}", database)).unwrap();
try_drop!(conn.execute(&format!("DROP DATABASE IF EXISTS {}", database)), "Couldn't drop database");
});
}
}
12 changes: 1 addition & 11 deletions diesel_cli/tests/support/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,7 @@ impl Project {
#[cfg(feature = "postgres")]
impl Drop for Project {
fn drop(&mut self) {
use std::io::{self, Write};
use std::thread;

let result = self.command("database").arg("drop").run();
if !result.is_success() {
if thread::panicking() {
writeln!(io::stderr(), "Couldn't drop database: {:?}", result).unwrap();
} else {
panic!("Couldn't drop database: {:?}", result);
}
}
try_drop!(self.command("database").arg("drop").run().result(), "Couldn't drop database");
}
}

Expand Down

0 comments on commit c0037b1

Please sign in to comment.