@@ -30,7 +30,7 @@ def test_loading_missing_templates(self):
3030 # contain template files
3131 with tempfile .TemporaryDirectory () as tmp_dir :
3232 # Attempt to load an HTML template from our custom template directory
33- template = self .hs .config .read_templates (["sso_error.html" ], tmp_dir )[0 ]
33+ template = self .hs .config .read_templates (["sso_error.html" ], ( tmp_dir ,) )[0 ]
3434
3535 # If no errors, we should've gotten the default template instead
3636
@@ -60,7 +60,7 @@ def test_loading_custom_templates(self):
6060
6161 # Attempt to load the template from our custom template directory
6262 template = (
63- self .hs .config .read_templates ([template_filename ], tmp_dir )
63+ self .hs .config .read_templates ([template_filename ], ( tmp_dir ,) )
6464 )[0 ]
6565
6666 # Render the template
@@ -74,8 +74,66 @@ def test_loading_custom_templates(self):
7474 "Template file did not contain our test string" ,
7575 )
7676
77+ def test_multiple_custom_template_directories (self ):
78+ """Tests that directories are searched in the right order if multiple custom
79+ template directories are provided.
80+ """
81+ # Create two temporary directories on the filesystem.
82+ tempdirs = [
83+ tempfile .TemporaryDirectory (),
84+ tempfile .TemporaryDirectory (),
85+ ]
86+
87+ # Create one template in each directory, whose content is the index of the
88+ # directory in the list.
89+ template_filename = "my_template.html.j2"
90+ for i in range (len (tempdirs )):
91+ tempdir = tempdirs [i ]
92+ template_path = os .path .join (tempdir .name , template_filename )
93+
94+ with open (template_path , "w" ) as fp :
95+ fp .write (str (i ))
96+ fp .flush ()
97+
98+ # Retrieve the template.
99+ template = (
100+ self .hs .config .read_templates (
101+ [template_filename ],
102+ (td .name for td in tempdirs ),
103+ )
104+ )[0 ]
105+
106+ # Test that we got the template we dropped in the first directory in the list.
107+ self .assertEqual (template .render (), "0" )
108+
109+ # Add another template, this one only in the second directory in the list, so we
110+ # can test that the second directory is still searched into when no matching file
111+ # could be found in the first one.
112+ other_template_name = "my_other_template.html.j2"
113+ other_template_path = os .path .join (tempdirs [1 ].name , other_template_name )
114+
115+ with open (other_template_path , "w" ) as fp :
116+ fp .write ("hello world" )
117+ fp .flush ()
118+
119+ # Retrieve the template.
120+ template = (
121+ self .hs .config .read_templates (
122+ [other_template_name ],
123+ (td .name for td in tempdirs ),
124+ )
125+ )[0 ]
126+
127+ # Test that the file has the expected content.
128+ self .assertEqual (template .render (), "hello world" )
129+
130+ # Cleanup the temporary directories manually since we're not using a context
131+ # manager.
132+ for td in tempdirs :
133+ td .cleanup ()
134+
77135 def test_loading_template_from_nonexistent_custom_directory (self ):
78136 with self .assertRaises (ConfigError ):
79137 self .hs .config .read_templates (
80- ["some_filename.html" ], "a_nonexistent_directory"
138+ ["some_filename.html" ], ( "a_nonexistent_directory" ,)
81139 )
0 commit comments