1212from django_bird .manifest import default_manifest_path
1313from django_bird .manifest import generate_asset_manifest
1414from django_bird .manifest import load_asset_manifest
15+ from django_bird .manifest import normalize_path
1516from django_bird .manifest import save_asset_manifest
1617from tests .utils import TestComponent
1718
@@ -139,6 +140,43 @@ def mock_open_with_error(*args, **kwargs):
139140 assert loaded_manifest is None
140141
141142
143+ def test_normalize_path_site_packages ():
144+ site_pkg_path = "/usr/local/lib/python3.12/site-packages/django_third_party_pkg/components/templates/bird/button.html"
145+
146+ normalized = normalize_path (site_pkg_path )
147+
148+ assert (
149+ normalized == "pkg:django_third_party_pkg/components/templates/bird/button.html"
150+ )
151+
152+
153+ def test_normalize_path_project_base_dir ():
154+ base_dir = "/home/user/project"
155+ project_path = f"{ base_dir } /app/templates/invoices/pending_invoice_list.html"
156+
157+ with override_settings (BASE_DIR = base_dir ):
158+ normalized = normalize_path (project_path )
159+
160+ assert normalized == "app:app/templates/invoices/pending_invoice_list.html"
161+
162+
163+ def test_normalize_path_other_absolute_dir ():
164+ other_path = "/some/random/external/path.html"
165+
166+ normalized = normalize_path (other_path )
167+
168+ assert normalized .startswith ("ext:" )
169+ assert "path.html" in normalized
170+
171+
172+ def test_normalize_path_relative_dir ():
173+ rel_path = "relative/path.html"
174+
175+ normalized = normalize_path (rel_path )
176+
177+ assert normalized == rel_path
178+
179+
142180def test_generate_asset_manifest (templates_dir , registry ):
143181 template1 = templates_dir / "test_manifest1.html"
144182 template1 .write_text ("""
@@ -181,12 +219,21 @@ def test_generate_asset_manifest(templates_dir, registry):
181219
182220 manifest = generate_asset_manifest ()
183221
184- all_keys = list (manifest .keys ())
185- template1_key = [k for k in all_keys if str (template1 ) in k ][0 ]
186- template2_key = [k for k in all_keys if str (template2 ) in k ][0 ]
222+ for key in manifest .keys ():
223+ # Keys should not be absolute paths starting with /
224+ assert not key .startswith ("/" ), f"Found absolute path in manifest: { key } "
225+
226+ template1_components = []
227+ template2_components = []
228+
229+ for key , components in manifest .items ():
230+ if "test_manifest1.html" in key :
231+ template1_components = components
232+ elif "test_manifest2.html" in key :
233+ template2_components = components
187234
188- assert sorted (manifest [ template1_key ] ) == sorted (["button" , "card" ])
189- assert sorted (manifest [ template2_key ] ) == sorted (["accordion" , "tab" ])
235+ assert sorted (template1_components ) == sorted (["button" , "card" ])
236+ assert sorted (template2_components ) == sorted (["accordion" , "tab" ])
190237
191238
192239def test_save_and_load_asset_manifest (tmp_path ):
@@ -195,6 +242,11 @@ def test_save_and_load_asset_manifest(tmp_path):
195242 "/path/to/template2.html" : ["accordion" , "tab" ],
196243 }
197244
245+ expected_normalized_data = {
246+ normalize_path ("/path/to/template1.html" ): ["button" , "card" ],
247+ normalize_path ("/path/to/template2.html" ): ["accordion" , "tab" ],
248+ }
249+
198250 output_path = tmp_path / "test-manifest.json"
199251
200252 save_asset_manifest (test_manifest_data , output_path )
@@ -204,7 +256,7 @@ def test_save_and_load_asset_manifest(tmp_path):
204256 with open (output_path ) as f :
205257 loaded_data = json .load (f )
206258
207- assert loaded_data == test_manifest_data
259+ assert loaded_data == expected_normalized_data
208260
209261
210262def test_default_manifest_path ():
0 commit comments