From f23162a2080a3fb3fe2b8ba7eba4d61da5737dfe Mon Sep 17 00:00:00 2001 From: msrd0 Date: Mon, 2 Nov 2020 17:11:53 +0100 Subject: Use `syn::Error::to_compile_error` and add trybuild ui test (#374) --- askama_derive/Cargo.toml | 1 - askama_derive/src/lib.rs | 9 +++++---- testing/Cargo.toml | 1 + testing/tests/ui.rs | 7 +++++++ testing/tests/ui/incorrect_path.rs | 8 ++++++++ testing/tests/ui/incorrect_path.stderr | 7 +++++++ 6 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 testing/tests/ui.rs create mode 100644 testing/tests/ui/incorrect_path.rs create mode 100644 testing/tests/ui/incorrect_path.stderr diff --git a/askama_derive/Cargo.toml b/askama_derive/Cargo.toml index 84970fb..63d08bd 100644 --- a/askama_derive/Cargo.toml +++ b/askama_derive/Cargo.toml @@ -25,5 +25,4 @@ warp = [] [dependencies] askama_shared = { version = "0.10.4", path = "../askama_shared", default-features = false } proc-macro2 = "1" -quote = "1" syn = "1" diff --git a/askama_derive/src/lib.rs b/askama_derive/src/lib.rs index 8844e99..101b861 100644 --- a/askama_derive/src/lib.rs +++ b/askama_derive/src/lib.rs @@ -7,6 +7,7 @@ use askama_shared::{ generator, get_template_source, read_config_file, CompileError, Config, Integrations, }; use proc_macro::TokenStream; +use proc_macro2::Span; use std::collections::HashMap; use std::path::PathBuf; @@ -15,11 +16,11 @@ use std::path::PathBuf; pub fn derive_template(input: TokenStream) -> TokenStream { let ast: syn::DeriveInput = syn::parse(input).unwrap(); match build_template(&ast) { - Ok(source) => source, - Err(e) => format!("compile_error!({:?})", e), + Ok(source) => source.parse().unwrap(), + Err(e) => syn::Error::new(Span::call_site(), e) + .to_compile_error() + .into(), } - .parse() - .unwrap() } /// Takes a `syn::DeriveInput` and generates source code for it diff --git a/testing/Cargo.toml b/testing/Cargo.toml index 64f9095..0fcee8c 100644 --- a/testing/Cargo.toml +++ b/testing/Cargo.toml @@ -15,6 +15,7 @@ serde_json = { version = "1.0", optional = true } [dev-dependencies] criterion = "0.3" +trybuild = "1.0" [[bench]] name = "all" diff --git a/testing/tests/ui.rs b/testing/tests/ui.rs new file mode 100644 index 0000000..17c2248 --- /dev/null +++ b/testing/tests/ui.rs @@ -0,0 +1,7 @@ +use trybuild::TestCases; + +#[cfg_attr(not(windows), test)] +fn ui() { + let t = TestCases::new(); + t.compile_fail("tests/ui/*.rs"); +} diff --git a/testing/tests/ui/incorrect_path.rs b/testing/tests/ui/incorrect_path.rs new file mode 100644 index 0000000..f0c85cf --- /dev/null +++ b/testing/tests/ui/incorrect_path.rs @@ -0,0 +1,8 @@ +use askama::Template; + +#[derive(Template)] +#[template(path = "thisdoesnotexist.html")] +struct MyTemplate; + +fn main() { +} diff --git a/testing/tests/ui/incorrect_path.stderr b/testing/tests/ui/incorrect_path.stderr new file mode 100644 index 0000000..ea97149 --- /dev/null +++ b/testing/tests/ui/incorrect_path.stderr @@ -0,0 +1,7 @@ +error: template "thisdoesnotexist.html" not found in directories ["$WORKSPACE/target/tests/askama_testing/templates"] + --> $DIR/incorrect_path.rs:3:10 + | +3 | #[derive(Template)] + | ^^^^^^^^ + | + = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info) -- cgit