@@ -2622,6 +2622,59 @@ def test_extract_catalog_index_creates_entities_directory(self, tmp_path, mocker
26222622 entity_file = entities_dir / "test-entity.yaml"
26232623 assert entity_file .exists (), "Entity file should be copied"
26242624
2625+ def test_extract_catalog_index_removes_existing_destination (self , tmp_path , mocker , mock_oci_image ):
2626+ """Test that existing catalog-entities directory is removed before copying."""
2627+ catalog_mount = tmp_path / "catalog-mount"
2628+ catalog_mount .mkdir ()
2629+ catalog_entities_parent_dir = tmp_path / "existing-dir"
2630+ catalog_entities_parent_dir .mkdir ()
2631+
2632+ # Create an existing catalog-entities directory with old content
2633+ existing_entities_dir = catalog_entities_parent_dir / "catalog-entities"
2634+ existing_entities_dir .mkdir ()
2635+ old_file = existing_entities_dir / "old-file.yaml"
2636+ old_file .write_text ("old content" )
2637+ old_subdir = existing_entities_dir / "old-subdir"
2638+ old_subdir .mkdir ()
2639+ (old_subdir / "old-nested.yaml" ).write_text ("old nested content" )
2640+
2641+ # Verify old content exists
2642+ assert existing_entities_dir .exists ()
2643+ assert old_file .exists ()
2644+ assert old_subdir .exists ()
2645+
2646+ mocker .patch ('shutil.which' , return_value = '/usr/bin/skopeo' )
2647+
2648+ mock_result = mocker .Mock ()
2649+ mock_result .returncode = 0
2650+ mock_subprocess_run = create_mock_skopeo_copy (
2651+ mock_oci_image ['manifest_path' ],
2652+ mock_oci_image ['layer_tarball' ],
2653+ mock_result
2654+ )
2655+ mocker .patch ('subprocess.run' , side_effect = mock_subprocess_run )
2656+
2657+ install_dynamic_plugins .extract_catalog_index (
2658+ "quay.io/test/catalog-index:1.9" ,
2659+ str (catalog_mount ),
2660+ str (catalog_entities_parent_dir )
2661+ )
2662+
2663+ # Verify old content was removed
2664+ assert not old_file .exists (), "Old file should have been removed"
2665+ assert not old_subdir .exists (), "Old subdirectory should have been removed"
2666+
2667+ # Verify new content exists
2668+ entities_dir = catalog_entities_parent_dir / "catalog-entities"
2669+ assert entities_dir .exists (), "Catalog entities directory should exist"
2670+ entity_file = entities_dir / "test-entity.yaml"
2671+ assert entity_file .exists (), "New entity file should exist"
2672+ assert "kind: Component" in entity_file .read_text ()
2673+
2674+ # Verify old content is definitely gone
2675+ assert not (entities_dir / "old-file.yaml" ).exists (), "Old file should not exist"
2676+ assert not (entities_dir / "old-subdir" ).exists (), "Old subdirectory should not exist"
2677+
26252678 def test_extract_catalog_index_without_catalog_entities (self , tmp_path , mocker , capsys ):
26262679 """Test that extraction succeeds with warning if catalog-entities directory doesn't exist in image."""
26272680 import tarfile
@@ -2677,6 +2730,7 @@ def test_extract_catalog_index_without_catalog_entities(self, tmp_path, mocker,
26772730 # Verify warning was printed
26782731 captured = capsys .readouterr ()
26792732 assert 'WARNING' in captured .out
2733+ assert 'does not have a' in captured .out
26802734 assert 'catalog-entities/marketplace' in captured .out
26812735
26822736 # Verify catalog entities directory was not created
0 commit comments