Skip to content

Commit

Permalink
refactor: Catch more panics for better test reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Aug 11, 2020
1 parent b1cd2e3 commit f9051e3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
13 changes: 13 additions & 0 deletions check/tests/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1135,3 +1135,16 @@ match writer with
"#,
"test.List String"
}

test_check! {
issue_863,
r#"
#[infix(right, 0)]
let (<|) f x : (a -> b) -> a -> b = f x
let g f x = x
let f a =
g a <| f a
{ f }
"#
}
14 changes: 9 additions & 5 deletions src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,11 +595,15 @@ where
Box::pin(async move {
Ok(From::from(move || {
async move {
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
db.import(modulename)
.await
.map_err(|err| MacroError::message(err.to_string()))
.map(move |id| pos::spanned(span, Expr::Ident(id)))
let result = {
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
db.import(modulename)
.await
.map_err(|err| MacroError::message(err.to_string()))
.map(move |id| pos::spanned(span, Expr::Ident(id)))
};
drop(db);
result
}
.boxed()
}))
Expand Down
11 changes: 10 additions & 1 deletion tests/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use support::*;

mod support;

use gluon::{self, query::Compilation, vm::core::tests::check_expr_eq, ThreadExt};
use gluon::{self, query::{Compilation, AsyncCompilation}, vm::core::tests::check_expr_eq, ThreadExt};

#[tokio::test]
async fn inline_cross_module() {
Expand All @@ -24,6 +24,7 @@ async fn inline_cross_module() {
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down Expand Up @@ -69,6 +70,7 @@ num.(+) 3
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down Expand Up @@ -98,6 +100,7 @@ async fn inline_across_two_modules() {
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down Expand Up @@ -127,6 +130,7 @@ async fn prune_prelude() {
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down Expand Up @@ -161,6 +165,7 @@ async fn prune_factorial() {
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down Expand Up @@ -198,6 +203,7 @@ mod.(+) (no_inline 1) 2
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down Expand Up @@ -236,6 +242,7 @@ match A with
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down Expand Up @@ -267,6 +274,7 @@ m.(<)
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down Expand Up @@ -300,6 +308,7 @@ f (Some 1)
.unwrap_or_else(|err| panic!("{}", err));

let mut db = thread.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
let core_expr = db
.core_expr("test".into(), None)
.await
Expand Down
32 changes: 25 additions & 7 deletions tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,27 @@ impl TestCase {
}
}

async fn catch_unwind_test(
name: String,
f: impl Future<Output = tensile::Test<Error>>,
) -> tensile::Test<Error> {
std::panic::AssertUnwindSafe(f)
.catch_unwind()
.await
.unwrap_or_else(|err| {
let err = Error::from(
err.downcast::<String>()
.map(|s| *s)
.or_else(|e| e.downcast::<&str>().map(|s| String::from(&s[..])))
.unwrap_or_else(|_| "Unknown panic".to_string()),
);
tensile::test(name, Err(err))
})
}

async fn make_test<'t>(vm: &'t Thread, name: &str, filename: &Path) -> Result<TestCase, Error> {
let text = fs::read_to_string(filename).await?;
let (De(test), _) = vm.run_expr_async(&name, &text).await?;
let (De(test), _) = std::panic::AssertUnwindSafe(vm.run_expr_async(&name, &text)).await?;
Ok(test)
}

Expand Down Expand Up @@ -306,7 +324,7 @@ async fn run_doc_tests<'t>(
.into_iter()
.map(move |(test_name, test_source)| {
let mut convert_test_fn = convert_test_fn.clone();
async move {
catch_unwind_test(test_name.clone(), async move {
let vm = vm.new_thread().unwrap();

match vm
Expand All @@ -320,7 +338,7 @@ async fn run_doc_tests<'t>(
tensile::test(test_name, || Err(err.0.into()))
}
}
}
})
})
.collect::<stream::FuturesOrdered<_>>()
.collect()
Expand Down Expand Up @@ -366,15 +384,15 @@ async fn main_(options: &Opt) -> Result<(), Error> {
let vm = vm.new_thread().unwrap();

let name2 = name.clone();
pool.spawn_with_handle(async move {
pool.spawn_with_handle(catch_unwind_test(name.clone(), async move {
match make_test(&vm, &name, &filename).await {
Ok(test) => test.into_tensile_test(),
Err(err) => {
let err = ::std::panic::AssertUnwindSafe(err);
tensile::test(name2, || Err(err.0))
}
}
})
}))
.expect("Could not spawn test future")
})
.collect::<stream::FuturesOrdered<_>>()
Expand Down Expand Up @@ -404,15 +422,15 @@ async fn main_(options: &Opt) -> Result<(), Error> {
.filter_map(&filter_fn)
.map(|(filename, name)| {
let vm = vm.new_thread().unwrap();
pool.spawn_with_handle(async move {
pool.spawn_with_handle(catch_unwind_test(name.clone(), async move {
match run_doc_tests(&vm, &name, &filename).await {
Ok(tests) => tensile::group(name.clone(), tests),
Err(err) => {
let err = ::std::panic::AssertUnwindSafe(err);
tensile::test(name.clone(), || Err(err.0))
}
}
})
}))
.expect("Could not spawn test future")
})
.collect::<stream::FuturesOrdered<_>>()
Expand Down
4 changes: 1 addition & 3 deletions vm/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1544,9 +1544,7 @@ fn get_return_type(
.remove_forall()
.as_function()
.map(|t| Cow::Borrowed(t.1))
.unwrap_or_else(|| {
panic!("Unexpected type {} is not a function", function_type);
});
.ok_or_else(|| format!("Unexpected type {} is not a function", function_type))?;

get_return_type(env, &ret, arg_count - 1)
}
Expand Down

0 comments on commit f9051e3

Please sign in to comment.