From 13eea3bd74152a6bb24fbb485b6bae0717ef484a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Sat, 8 Feb 2025 22:56:29 +0100 Subject: [PATCH] derive: fail if requested template block is missing Previously it was only tested if the block exists if there were any blocks. If there weren't any, then an empty result would be rendered. --- rinja_derive/src/lib.rs | 26 +++++++++-------- .../templates/no-block-with-base-template.txt | 1 + .../no-block-with-include-times-2.txt | 1 + testing/templates/no-block-with-include.txt | 1 + testing/templates/no-block.txt | 0 testing/tests/ui/no-block.rs | 28 ++++++++++++++++++ testing/tests/ui/no-block.stderr | 29 +++++++++++++++++++ 7 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 testing/templates/no-block-with-base-template.txt create mode 100644 testing/templates/no-block-with-include-times-2.txt create mode 100644 testing/templates/no-block-with-include.txt create mode 100644 testing/templates/no-block.txt create mode 100644 testing/tests/ui/no-block.rs create mode 100644 testing/tests/ui/no-block.stderr diff --git a/rinja_derive/src/lib.rs b/rinja_derive/src/lib.rs index 0d6a68f56..14b510baf 100644 --- a/rinja_derive/src/lib.rs +++ b/rinja_derive/src/lib.rs @@ -284,22 +284,24 @@ fn build_template_item( let ctx = &contexts[&input.path]; let heritage = if !ctx.blocks.is_empty() || ctx.extends.is_some() { - let heritage = Heritage::new(ctx, &contexts); - - if let Some((block_name, block_span)) = input.block { - if !heritage.blocks.contains_key(&block_name) { - return Err(CompileError::no_file_info( - format_args!("cannot find block `{block_name}`"), - Some(block_span), - )); - } - } - - Some(heritage) + Some(Heritage::new(ctx, &contexts)) } else { None }; + if let Some((block_name, block_span)) = input.block { + let has_block = match &heritage { + Some(heritage) => heritage.blocks.contains_key(block_name), + None => ctx.blocks.contains_key(block_name), + }; + if !has_block { + return Err(CompileError::no_file_info( + format_args!("cannot find block `{block_name}`"), + Some(block_span), + )); + } + } + if input.print == Print::Ast || input.print == Print::All { eprintln!("{:?}", templates[&input.path].nodes()); } diff --git a/testing/templates/no-block-with-base-template.txt b/testing/templates/no-block-with-base-template.txt new file mode 100644 index 000000000..ee28da6e2 --- /dev/null +++ b/testing/templates/no-block-with-base-template.txt @@ -0,0 +1 @@ +{% extends "no-block-with-include-times-2.txt" %} diff --git a/testing/templates/no-block-with-include-times-2.txt b/testing/templates/no-block-with-include-times-2.txt new file mode 100644 index 000000000..d164ee5f1 --- /dev/null +++ b/testing/templates/no-block-with-include-times-2.txt @@ -0,0 +1 @@ +{% include "no-block-with-include.txt" %} diff --git a/testing/templates/no-block-with-include.txt b/testing/templates/no-block-with-include.txt new file mode 100644 index 000000000..1094814b2 --- /dev/null +++ b/testing/templates/no-block-with-include.txt @@ -0,0 +1 @@ +{% include "no-block.txt" %} diff --git a/testing/templates/no-block.txt b/testing/templates/no-block.txt new file mode 100644 index 000000000..e69de29bb diff --git a/testing/tests/ui/no-block.rs b/testing/tests/ui/no-block.rs new file mode 100644 index 000000000..eba121260 --- /dev/null +++ b/testing/tests/ui/no-block.rs @@ -0,0 +1,28 @@ +use rinja::Template; + +#[derive(Template)] +#[template( + ext = "txt", + source = "{% block not_a %}{% endblock %}", + block = "a" +)] +struct SourceTemplate; + +#[derive(Template)] +#[template(path = "no-block.txt", block = "a")] +struct PathTemplate; + +#[derive(Template)] +#[template(path = "no-block-with-include.txt", block = "a")] +struct NoBlockWithInclude; + +#[derive(Template)] +#[template(path = "no-block-with-include-times-2.txt", block = "a")] +struct NoBlockWithIncludeTimes2; + +#[derive(Template)] +#[template(path = "no-block-with-base-template.txt", block = "a")] +struct NoBlockWithBaseTemplate; + +fn main() { +} diff --git a/testing/tests/ui/no-block.stderr b/testing/tests/ui/no-block.stderr new file mode 100644 index 000000000..8bc63d2c9 --- /dev/null +++ b/testing/tests/ui/no-block.stderr @@ -0,0 +1,29 @@ +error: cannot find block `a` + --> tests/ui/no-block.rs:7:13 + | +7 | block = "a" + | ^^^ + +error: cannot find block `a` + --> tests/ui/no-block.rs:12:43 + | +12 | #[template(path = "no-block.txt", block = "a")] + | ^^^ + +error: cannot find block `a` + --> tests/ui/no-block.rs:16:56 + | +16 | #[template(path = "no-block-with-include.txt", block = "a")] + | ^^^ + +error: cannot find block `a` + --> tests/ui/no-block.rs:20:64 + | +20 | #[template(path = "no-block-with-include-times-2.txt", block = "a")] + | ^^^ + +error: cannot find block `a` + --> tests/ui/no-block.rs:24:62 + | +24 | #[template(path = "no-block-with-base-template.txt", block = "a")] + | ^^^