11"""Tests for the template module."""
22
3- import tempfile
4- from pathlib import Path
5-
63import pytest
74from weav .template import TemplateError , compile_template , find_template
85
96
10- def test_find_template_direct_path ():
7+ def test_find_template_direct_path (tmp_path ):
118 """Test finding template by direct file path."""
12- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".j2" , delete = False ) as f :
13- f .write ("Hello {{ name }}" )
14- f .flush ()
15- _loader , name = find_template (f .name )
16- Path (f .name ).unlink ()
17-
18- assert name == Path (f .name ).name
9+ template = tmp_path / "test.j2"
10+ template .write_text ("Hello {{ name }}" )
11+ _loader , name = find_template (str (template ))
12+ assert name == template .name
1913
2014
2115def test_find_template_not_found ():
@@ -24,65 +18,41 @@ def test_find_template_not_found():
2418 find_template ("nonexistent_template.j2" )
2519
2620
27- def test_compile_template_basic ():
21+ def test_compile_template_basic (tmp_path ):
2822 """Test basic template compilation."""
29- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".j2" , delete = False ) as f :
30- f .write ("Hello {{ name }}!" )
31- f .flush ()
32- result = compile_template (f .name , [], ["name=World" ])
33- Path (f .name ).unlink ()
34-
23+ template = tmp_path / "test.j2"
24+ template .write_text ("Hello {{ name }}!" )
25+ result = compile_template (str (template ), [], ["name=World" ])
3526 assert result == "Hello World!"
3627
3728
38- def test_compile_template_with_yaml ():
29+ def test_compile_template_with_yaml (tmp_path ):
3930 """Test template compilation with YAML data."""
40- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".j2" , delete = False ) as template_file :
41- template_file .write ("{{ greeting }}, {{ name }}!" )
42- template_file .flush ()
43-
44- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".yaml" , delete = False ) as data_file :
45- data_file .write ("greeting: Hello\n name: World\n " )
46- data_file .flush ()
47-
48- result = compile_template (template_file .name , [data_file .name ], [])
49- Path (data_file .name ).unlink ()
50- Path (template_file .name ).unlink ()
51-
31+ template = tmp_path / "test.j2"
32+ template .write_text ("{{ greeting }}, {{ name }}!" )
33+ data_file = tmp_path / "data.yaml"
34+ data_file .write_text ("greeting: Hello\n name: World\n " )
35+ result = compile_template (str (template ), [str (data_file )], [])
5236 assert result == "Hello, World!"
5337
5438
55- def test_compile_template_keyval_overrides_yaml ():
39+ def test_compile_template_keyval_overrides_yaml (tmp_path ):
5640 """Test that keyval overrides YAML data."""
57- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".j2" , delete = False ) as template_file :
58- template_file .write ("{{ name }}" )
59- template_file .flush ()
60-
61- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".yaml" , delete = False ) as data_file :
62- data_file .write ("name: FromYAML\n " )
63- data_file .flush ()
64-
65- result = compile_template (template_file .name , [data_file .name ], ["name=FromKeyval" ])
66- Path (data_file .name ).unlink ()
67- Path (template_file .name ).unlink ()
68-
41+ template = tmp_path / "test.j2"
42+ template .write_text ("{{ name }}" )
43+ data_file = tmp_path / "data.yaml"
44+ data_file .write_text ("name: FromYAML\n " )
45+ result = compile_template (str (template ), [str (data_file )], ["name=FromKeyval" ])
6946 assert result == "FromKeyval"
7047
7148
72- def test_compile_template_wrapped_list ():
49+ def test_compile_template_wrapped_list (tmp_path ):
7350 """Test template compilation with wrapped list data."""
74- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".j2" , delete = False ) as template_file :
75- template_file .write ("{% for item in items %}{{ item }} {% endfor %}" )
76- template_file .flush ()
77-
78- with tempfile .NamedTemporaryFile (mode = "w" , suffix = ".yaml" , delete = False ) as data_file :
79- data_file .write ("- apple\n - banana\n - cherry\n " )
80- data_file .flush ()
81-
82- result = compile_template (template_file .name , [f"items={ data_file .name } " ], [])
83- Path (data_file .name ).unlink ()
84- Path (template_file .name ).unlink ()
85-
51+ template = tmp_path / "test.j2"
52+ template .write_text ("{% for item in items %}{{ item }} {% endfor %}" )
53+ data_file = tmp_path / "data.yaml"
54+ data_file .write_text ("- apple\n - banana\n - cherry\n " )
55+ result = compile_template (str (template ), [f"items={ data_file } " ], [])
8656 assert "apple" in result
8757 assert "banana" in result
8858 assert "cherry" in result
0 commit comments