Skip to content

Commit 7d5a341

Browse files
committed
ctest: Dedent for directives in templates
Give a slightly better structure for our eyes to follow. Unfortunately we can't do anything for C files. This affects whitespace output in a way that isn't straightforward to resolve with `+`/`-`/`~` Askama directives, so also ensure that template output has a single trailing newline.
1 parent b581419 commit 7d5a341

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

ctest-next/src/generator.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use thiserror::Error;
1010
use crate::ffi_items::FfiItems;
1111
use crate::template::{CTestTemplate, RustTestTemplate};
1212
use crate::{
13-
expand, Const, Field, MapInput, Parameter, Result, Static, Struct, Type, VolatileItemKind,
13+
Const, Field, MapInput, Parameter, Result, Static, Struct, Type, VolatileItemKind, expand,
1414
};
1515

1616
/// A function that takes a mappable input and returns its mapping as Some, otherwise
@@ -600,33 +600,34 @@ impl TestGenerator {
600600
.unwrap_or_else(|| env::var("OUT_DIR").unwrap().into());
601601
let output_file_path = output_directory.join(output_file_path);
602602

603+
let ensure_trailing_newline = |s: &mut String| {
604+
s.truncate(s.trim_end().len());
605+
s.push('\n');
606+
};
607+
608+
let mut rust_file = RustTestTemplate::new(&ffi_items, self)
609+
.map_err(|e| GenerationError::TemplateRender("Rust".to_string(), e.to_string()))?
610+
.render()
611+
.map_err(|e| GenerationError::TemplateRender("Rust".to_string(), e.to_string()))?;
612+
ensure_trailing_newline(&mut rust_file);
613+
603614
// Generate the Rust side of the tests.
604615
File::create(output_file_path.with_extension("rs"))
605616
.map_err(GenerationError::OsError)?
606-
.write_all(
607-
RustTestTemplate::new(&ffi_items, self)
608-
.map_err(|e| {
609-
GenerationError::TemplateRender("Rust".to_string(), e.to_string())
610-
})?
611-
.render()
612-
.map_err(|e| {
613-
GenerationError::TemplateRender("Rust".to_string(), e.to_string())
614-
})?
615-
.as_bytes(),
616-
)
617+
.write_all(rust_file.as_bytes())
617618
.map_err(GenerationError::OsError)?;
618619

620+
let mut c_file = CTestTemplate::new(&ffi_items, self)
621+
.map_err(|e| GenerationError::TemplateRender("C".to_string(), e.to_string()))?
622+
.render()
623+
.map_err(|e| GenerationError::TemplateRender("C".to_string(), e.to_string()))?;
624+
ensure_trailing_newline(&mut c_file);
625+
619626
// Generate the C/Cxx side of the tests.
620627
let c_output_path = output_file_path.with_extension("c");
621628
File::create(&c_output_path)
622629
.map_err(GenerationError::OsError)?
623-
.write_all(
624-
CTestTemplate::new(&ffi_items, self)
625-
.map_err(|e| GenerationError::TemplateRender("C".to_string(), e.to_string()))?
626-
.render()
627-
.map_err(|e| GenerationError::TemplateRender("C".to_string(), e.to_string()))?
628-
.as_bytes(),
629-
)
630+
.write_all(c_file.as_bytes())
630631
.map_err(GenerationError::OsError)?;
631632

632633
Ok(output_file_path)

ctest-next/templates/test.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mod generated_tests {
4444
}
4545
}
4646

47-
{%- for const_cstr in ctx.const_cstr_tests +%}
47+
{%- for const_cstr in ctx.const_cstr_tests +%}
4848

4949
// Test that the string constant is the same in both Rust and C.
5050
// While fat pointers can't be translated, we instead use * const c_char.
@@ -62,9 +62,9 @@ mod generated_tests {
6262
check_same(val, c, "{{ const_cstr.rust_ident }} string");
6363
}
6464
}
65-
{%- endfor +%}
65+
{%- endfor +%}
6666

67-
{%- for constant in ctx.const_tests +%}
67+
{%- for constant in ctx.const_tests +%}
6868

6969
// Test that the value of the constant is the same in both Rust and C.
7070
// This performs a byte by byte comparision of the constant value.
@@ -85,7 +85,7 @@ mod generated_tests {
8585
}
8686
}
8787
}
88-
{%- endfor +%}
88+
{%- endfor +%}
8989
}
9090

9191
use generated_tests::*;
@@ -109,4 +109,3 @@ fn run_all() {
109109
{{ test }}();
110110
{%- endfor +%}
111111
}
112-

0 commit comments

Comments
 (0)