2020
2121import os
2222import re
23- from pathlib import Path
23+ from tempfile import TemporaryDirectory
2424from unittest import TestCase
2525
2626from docx import Document
@@ -34,7 +34,9 @@ class TestDocxGenerator(TestCase):
3434 def setUp (self ) -> None :
3535 self ._base_path = os .path .join (os .getcwd (), 'test/component' )
3636 self ._template_path = 'templates'
37- self ._results_path = 'results'
37+ self ._results_directory = TemporaryDirectory (dir = self ._base_path )
38+ # TODO code smell, we shouldn't have to do that. This shows the generate_docx method is too complex to use
39+ self ._results_path = self ._results_directory .name .split ('/' )[- 1 ]
3840
3941 self ._output_filenames = {
4042 'basic_template_result' : 'basic_template_result.docx' ,
@@ -52,16 +54,10 @@ def setUp(self) -> None:
5254 'unclosed_jinja_control_tag_result' : 'unclosed_jinja_control_tag_result.docx'
5355 }
5456
55- Path (self ._base_path , self ._results_path ).mkdir (exist_ok = True )
5657 self ._subject = DocxGenerator (logger_mode = 'DEBUG' )
5758
5859 def tearDown (self ) -> None :
59- return None
60- for filename in self ._output_filenames .values ():
61- try :
62- os .remove (os .path .join (self ._results_path , filename ))
63- except FileNotFoundError :
64- pass
60+ self ._results_directory .cleanup ()
6561
6662 def test_should_generate_docx_from_basic_template (self ):
6763 text_to_add = 'Report Name'
@@ -209,6 +205,8 @@ def test_should_not_fail_with_specific_markdown(self):
209205 'text_for_code_block' : markdown_text2
210206 }
211207
208+ print ('----------------------------------' )
209+ print (self ._results_path )
212210 self ._subject .generate_docx (
213211 self ._base_path ,
214212 os .path .join (self ._template_path , 'markdown_filter_template.docx' ),
0 commit comments