tmpltool 1.5.0

A fast and simple command-line template rendering tool using MiniJinja templates with environment variables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/// Integration tests for template include functionality
use std::env;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};
use tmpltool::render_template;

mod common;

// Global counter for unique test directories
static TEST_COUNTER: AtomicU32 = AtomicU32::new(0);

/// Helper to create a test directory
fn setup_test_env() -> PathBuf {
    let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
    let test_dir = env::temp_dir().join(format!(
        "tmpltool_include_test_{}_{}",
        std::process::id(),
        counter
    ));
    fs::create_dir_all(&test_dir).unwrap();
    test_dir
}

/// Helper to create a file in the test directory
fn create_file(dir: &Path, name: &str, content: &str) -> PathBuf {
    let file_path = dir.join(name);
    let mut file = fs::File::create(&file_path).unwrap();
    file.write_all(content.as_bytes()).unwrap();
    file_path
}

/// Helper to cleanup test directory
fn cleanup_test_env(test_dir: &Path) {
    let _ = fs::remove_dir_all(test_dir);
}

#[test]
fn test_simple_include() {
    let test_dir = setup_test_env();

    // Create partial template
    create_file(&test_dir, "partial.tmpltool", "Hello from partial!");

    // Create main template
    let main_template = create_file(
        &test_dir,
        "main.tmpltool",
        "Start\n{% include \"./partial.tmpltool\" %}\nEnd",
    );

    let output_file = test_dir.join("output.txt");

    // Render
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );

    assert!(
        result.is_ok(),
        "Template rendering failed: {:?}",
        result.err()
    );

    // Verify output
    let output = fs::read_to_string(&output_file).unwrap();
    assert_eq!(output, "Start\nHello from partial!\nEnd");

    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_with_env_vars() {
    let test_dir = setup_test_env();

    // Create partial template with env vars
    create_file(
        &test_dir,
        "partial.tmpltool",
        "User: {{ get_env(name=\"TEST_USER\", default=\"guest\") }}",
    );

    // Create main template
    let main_template = create_file(
        &test_dir,
        "main.tmpltool",
        "Header\n{% include \"./partial.tmpltool\" %}\nFooter",
    );

    let output_file = test_dir.join("output.txt");

    // Set environment variable
    unsafe {
        env::set_var("TEST_USER", "testuser");
    }

    // Render
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );

    assert!(
        result.is_ok(),
        "Template rendering failed: {:?}",
        result.err()
    );

    // Verify output
    let output = fs::read_to_string(&output_file).unwrap();
    assert_eq!(output, "Header\nUser: testuser\nFooter");

    // Cleanup env var
    unsafe {
        env::remove_var("TEST_USER");
    }
    cleanup_test_env(&test_dir);
}

#[test]
fn test_nested_includes() {
    let test_dir = setup_test_env();

    // Create level 2 template
    create_file(&test_dir, "level2.tmpltool", "Level 2 content");

    // Create level 1 template that includes level 2
    create_file(
        &test_dir,
        "level1.tmpltool",
        "Level 1 start\n{% include \"./level2.tmpltool\" %}\nLevel 1 end",
    );

    // Create main template that includes level 1
    let main_template = create_file(
        &test_dir,
        "main.tmpltool",
        "Main start\n{% include \"./level1.tmpltool\" %}\nMain end",
    );

    let output_file = test_dir.join("output.txt");

    // Render
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );

    assert!(
        result.is_ok(),
        "Template rendering failed: {:?}",
        result.err()
    );

    // Verify output
    let output = fs::read_to_string(&output_file).unwrap();
    assert_eq!(
        output,
        "Main start\nLevel 1 start\nLevel 2 content\nLevel 1 end\nMain end"
    );

    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_with_subdirectory() {
    let test_dir = setup_test_env();

    // Create subdirectory
    let subdir = test_dir.join("partials");
    fs::create_dir_all(&subdir).unwrap();

    // Create partial in subdirectory
    create_file(&subdir, "footer.tmpltool", "Footer content");

    // Create main template
    let main_template = create_file(
        &test_dir,
        "main.tmpltool",
        "Main content\n{% include \"./partials/footer.tmpltool\" %}",
    );

    let output_file = test_dir.join("output.txt");

    // Render
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );

    assert!(
        result.is_ok(),
        "Template rendering failed: {:?}",
        result.err()
    );

    // Verify output
    let output = fs::read_to_string(&output_file).unwrap();
    assert_eq!(output, "Main content\nFooter content");

    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_nonexistent_template() {
    let test_dir = setup_test_env();

    // Create main template that tries to include nonexistent file
    let main_template = create_file(
        &test_dir,
        "main.tmpltool",
        "{% include \"./nonexistent.tmpltool\" %}",
    );

    let output_file = test_dir.join("output.txt");

    // Render - should fail
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );

    assert!(result.is_err(), "Expected error for nonexistent template");
    let error = result.unwrap_err().to_string();
    assert!(
        error.contains("Failed to load template") || error.contains("nonexistent"),
        "Error message should mention failed load or nonexistent file: {}",
        error
    );

    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_parent_directory_blocked() {
    let test_dir = setup_test_env();

    // Create parent directory file
    create_file(&test_dir, "parent.tmpltool", "Parent content");

    // Create subdirectory
    let subdir = test_dir.join("subdir");
    fs::create_dir_all(&subdir).unwrap();

    // Create template in subdirectory that tries to include parent
    let main_template = create_file(
        &subdir,
        "main.tmpltool",
        "{% include \"../parent.tmpltool\" %}",
    );

    let output_file = test_dir.join("output.txt");

    // Render - should fail due to security
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None, // trust_mode = false
    );

    assert!(
        result.is_err(),
        "Expected error for parent directory access"
    );
    let error = result.unwrap_err().to_string();
    assert!(
        error.contains("Parent directory") || error.contains(".."),
        "Error message should mention parent directory restriction: {}",
        error
    );

    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_parent_directory_allowed_with_trust() {
    let test_dir = setup_test_env();

    // Create parent directory file
    create_file(&test_dir, "parent.tmpltool", "Parent content");

    // Create subdirectory
    let subdir = test_dir.join("subdir");
    fs::create_dir_all(&subdir).unwrap();

    // Create template in subdirectory that includes parent
    let main_template = create_file(
        &subdir,
        "main.tmpltool",
        "Start\n{% include \"../parent.tmpltool\" %}\nEnd",
    );

    let output_file = test_dir.join("output.txt");

    // Render with trust mode
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        true,
        None, // trust_mode = true
    );

    assert!(
        result.is_ok(),
        "Template rendering failed: {:?}",
        result.err()
    );

    // Verify output
    let output = fs::read_to_string(&output_file).unwrap();
    assert_eq!(output, "Start\nParent content\nEnd");

    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_absolute_path_blocked() {
    let test_dir = setup_test_env();

    // Create template that tries to use absolute path
    let main_template = create_file(&test_dir, "main.tmpltool", "{% include \"/etc/passwd\" %}");

    let output_file = test_dir.join("output.txt");

    // Render - should fail due to security
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None, // trust_mode = false
    );

    assert!(result.is_err(), "Expected error for absolute path");
    let error = result.unwrap_err().to_string();
    assert!(
        error.contains("Absolute paths are not allowed") || error.contains("Security"),
        "Error message should mention security restriction: {}",
        error
    );

    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_multiple_partials() {
    let test_dir = setup_test_env();

    // Create multiple partials
    create_file(&test_dir, "header.tmpltool", "=== Header ===");
    create_file(&test_dir, "content.tmpltool", "Main Content");
    create_file(&test_dir, "footer.tmpltool", "=== Footer ===");

    // Create main template that includes all
    let main_template = create_file(
        &test_dir,
        "main.tmpltool",
        "{% include \"./header.tmpltool\" %}\n{% include \"./content.tmpltool\" %}\n{% include \"./footer.tmpltool\" %}",
    );

    let output_file = test_dir.join("output.txt");

    // Render
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );

    assert!(
        result.is_ok(),
        "Template rendering failed: {:?}",
        result.err()
    );

    // Verify output
    let output = fs::read_to_string(&output_file).unwrap();
    assert_eq!(output, "=== Header ===\nMain Content\n=== Footer ===");

    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_with_conditionals() {
    let test_dir = setup_test_env();

    // Create partial
    create_file(&test_dir, "optional.tmpltool", "Optional content included");

    // Create main template with conditional include
    let main_template = create_file(
        &test_dir,
        "main.tmpltool",
        "{% set show = get_env(name=\"SHOW_OPTIONAL\", default=\"false\") %}\
        Start\n\
        {% if show == \"true\" %}\
        {% include \"./optional.tmpltool\" %}\n\
        {% endif %}\
        End",
    );

    let output_file = test_dir.join("output.txt");

    // Test without env var (should not include)
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );
    assert!(result.is_ok());
    let output = fs::read_to_string(&output_file).unwrap();
    assert_eq!(output, "Start\nEnd");

    // Test with env var (should include)
    unsafe {
        env::set_var("SHOW_OPTIONAL", "true");
    }
    let result = render_template(
        Some(main_template.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );
    assert!(result.is_ok());
    let output = fs::read_to_string(&output_file).unwrap();
    assert_eq!(output, "Start\nOptional content included\nEnd");

    unsafe {
        env::remove_var("SHOW_OPTIONAL");
    }
    cleanup_test_env(&test_dir);
}

#[test]
fn test_include_fixture_templates() {
    // Test using pre-created fixture templates
    let template_path = common::get_fixture_template("include_base.tmpltool");
    let output_file = common::get_test_file_path("include_base_output.txt");

    let result = render_template(
        Some(template_path.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );

    assert!(
        result.is_ok(),
        "Template rendering failed: {:?}",
        result.err()
    );

    // Verify output matches expected
    let output = fs::read_to_string(&output_file).unwrap();
    let expected = common::read_fixture_expected("include_base.txt");
    assert_eq!(output, expected);

    common::cleanup_test_file(&output_file);
}

#[test]
fn test_include_nested_fixture_templates() {
    // Test nested includes using fixture templates
    let template_path = common::get_fixture_template("include_nested_main.tmpltool");
    let output_file = common::get_test_file_path("include_nested_output.txt");

    let result = render_template(
        Some(template_path.to_str().unwrap()),
        Some(output_file.to_str().unwrap()),
        false,
        None,
    );

    assert!(
        result.is_ok(),
        "Template rendering failed: {:?}",
        result.err()
    );

    // Verify output matches expected
    let output = fs::read_to_string(&output_file).unwrap();
    let expected = common::read_fixture_expected("include_nested.txt");
    assert_eq!(output, expected);

    common::cleanup_test_file(&output_file);
}