@@ -66,6 +66,30 @@ def simple_regmap() -> csr.Map:
6666 )
6767
6868
69+ @pytest .fixture
70+ def custom_template (tmp_path : Path ) -> Path :
71+ """Create a custom template file directly in tmp_path and return its absolute path."""
72+ template_name = "custom_template_fixture.md.j2"
73+ template_content = (
74+ """"" \
75+ # Custom Template Fixture Test
76+
77+ Registers:
78+ {% for item in regmap.items %}
79+ - {{ item.name }}
80+ {% endfor %}
81+
82+ {% if cfg.extra['my_param'] is defined %}
83+ Custom Param: {{ cfg.extra['my_param'] }}
84+ {% endif %}
85+ """
86+ ""
87+ ) # Raw string literal to handle backslashes correctly
88+ template_file = tmp_path / template_name
89+ template_file .write_text (template_content )
90+ return template_file .resolve () # Return absolute path
91+
92+
6993def test_default_generation (tmp_path : Path , simple_regmap : csr .Map ) -> None :
7094 """Test default Markdown generation."""
7195 config = csr .MarkdownGenerator .Config ()
@@ -215,25 +239,13 @@ def test_wavedrom_dump_json(tmp_path: Path, simple_regmap: csr.Map) -> None:
215239 assert {f for f in expected_data_dir .iterdir () if f .suffix == ".json" } == expected_json_files
216240
217241
218- def test_custom_template_and_extra_param (tmp_path : Path , simple_regmap : csr .Map ) -> None :
242+ def test_custom_template_and_extra_param (tmp_path : Path , simple_regmap : csr .Map , custom_template : Path ) -> None :
219243 """Test generation with a custom template and extra config parameters."""
220- custom_template_name = "custom_template.md.j2"
221- custom_template_content = """
222- # Custom Template Test
223-
224- Registers:
225- {% for item in regmap.items %}
226- - {{ item.name }}
227- {% endfor %}
228-
229- Custom Param: {{ cfg.extra['my_param'] }}
230- """
231- custom_template_file = tmp_path / custom_template_name
232- custom_template_file .write_text (custom_template_content )
244+ template_file = custom_template
233245
234246 custom_key = "my_param"
235247 custom_value = "hello_world"
236- config = csr .MarkdownGenerator .Config (template_name = str (custom_template_file ), extra = {custom_key : custom_value })
248+ config = csr .MarkdownGenerator .Config (template_name = str (template_file ), extra = {custom_key : custom_value })
237249
238250 gen = csr .MarkdownGenerator (
239251 label = "test_md_gen_custom_tmpl" ,
@@ -250,8 +262,34 @@ def test_custom_template_and_extra_param(tmp_path: Path, simple_regmap: csr.Map)
250262
251263 # Check the content of the generated file
252264 content = expected_file .read_text ()
253- assert "# Custom Template Test" in content
265+ assert "# Custom Template Fixture Test" in content
254266 assert "Registers:" in content
255267 assert "- reg1" in content
256268 assert "- reg2" in content
257269 assert f"Custom Param: { custom_value } " in content
270+
271+
272+ def test_custom_template_searchpath (tmp_path : Path , simple_regmap : csr .Map , custom_template : Path ) -> None :
273+ """Test generation with a custom template specified via search path."""
274+ template_file = custom_template # Fixture now returns the absolute path
275+ templates_dir = template_file .parent
276+ template_name = template_file .name
277+
278+ # Configure the generator to use the custom template directory and name
279+ config = csr .MarkdownGenerator .Config (
280+ template_searchpaths = [templates_dir ],
281+ template_name = template_name ,
282+ )
283+
284+ gen = csr .MarkdownGenerator (
285+ label = "test_md_gen_searchpath" ,
286+ register_map = simple_regmap ,
287+ config = config ,
288+ output_dir = tmp_path ,
289+ )
290+
291+ # Running the generator should succeed without TemplateNotFound
292+ try :
293+ list (gen ())
294+ except csr .GeneratorTemplateError as e :
295+ pytest .fail (f"Template generation failed: { e } " )
0 commit comments