Skip to content

Commit 778b7d2

Browse files
Merge pull request #729 from GuillaumeGomez/fix-print
Fix print = "code"
2 parents 050be24 + 9dcf264 commit 778b7d2

4 files changed

Lines changed: 57 additions & 1 deletion

File tree

askama_derive/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,10 +480,15 @@ fn build_template_item(
480480
}
481481

482482
if input.print == Print::Ast || input.print == Print::All {
483-
eprintln!("{:?}", templates[&input.path].nodes());
483+
eprintln!("== Askama AST ==\n{:?}", templates[&input.path].nodes());
484484
}
485485

486486
let size_hint = template_to_string(buf, &input, &contexts, heritage.as_ref(), tmpl_kind)?;
487+
488+
if input.print == Print::Code || input.print == Print::All {
489+
eprintln!("== Askama code ==\n{}", buf.to_token_stream());
490+
}
491+
487492
Ok(size_hint)
488493
}
489494

testing/tests/print.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::path::Path;
2+
use std::process::Command;
3+
4+
#[test]
5+
fn test_print_config() {
6+
let Ok(cargo_home) = std::env::var("CARGO_MANIFEST_DIR") else {
7+
panic!(">> cannot get `CARGO_MANIFEST_DIR` env variable");
8+
};
9+
let output = Command::new("cargo")
10+
.arg("check")
11+
.current_dir(Path::new(&cargo_home).join("tests/print"))
12+
.output()
13+
.unwrap();
14+
15+
let content = String::from_utf8_lossy(&output.stderr);
16+
17+
if !output.status.success() {
18+
panic!("Failed to build `tests/print`: {content}");
19+
}
20+
21+
// There should be two of each (one for "all" and one for "ast"/"code").
22+
assert_eq!(content.split("== Askama AST ==").count(), 3);
23+
assert_eq!(content.split("== Askama code ==").count(), 3);
24+
}

testing/tests/print/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "print"
3+
edition = "2024"
4+
5+
[lib]
6+
path = "./lib.rs"
7+
8+
[dependencies]
9+
askama = { path = "../../../askama" }
10+
11+
[workspace]

testing/tests/print/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This test (coupled with `testing/tests/print.rs`) ensures that `print = "..."`
2+
// works as expected.
3+
4+
use askama::Template;
5+
6+
#[derive(Template)]
7+
#[template(source = r#"{% let x = 12 %}{{x}}"#, ext = "txt", print = "all")]
8+
struct PrintAll;
9+
10+
#[derive(Template)]
11+
#[template(source = r#"{% let x = 12 %}{{x}}"#, ext = "txt", print = "ast")]
12+
struct PrintAst;
13+
14+
#[derive(Template)]
15+
#[template(source = r#"{% let x = 12 %}{{x}}"#, ext = "txt", print = "code")]
16+
struct PrintCode;

0 commit comments

Comments
 (0)