Skip to content

Commit 02cc13d

Browse files
committed
Handle removing resources without parents
1 parent a109ca4 commit 02cc13d

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/commoncode/resource.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,9 @@ def remove_resource(self, resource):
906906
parent = resource.parent(self)
907907
if TRACE:
908908
logger_debug(" parent", parent)
909-
parent.children_names.remove(resource.name)
910-
parent.save(self)
909+
if parent:
910+
parent.children_names.remove(resource.name)
911+
parent.save(self)
911912

912913
# remove resource proper
913914
self._remove_resource(resource)

tests/test_resource.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ def test_walk_skip_root_basic(self):
101101
]
102102
assert [(r.name, r.is_file) for r in results] == expected
103103

104+
def test_remove_resource_without_parent(self):
105+
test_codebase = self.get_test_loc("resource/codebase")
106+
codebase = Codebase(test_codebase)
107+
resource = codebase.get_resource("codebase/dir/that")
108+
parent = resource.parent(codebase)
109+
110+
codebase.resources_by_path.pop(parent.path)
111+
112+
removed_paths = codebase.remove_resource(resource)
113+
114+
assert removed_paths == {resource.location}
115+
assert resource.path not in codebase.resources_by_path
116+
104117
def test_walk_filtered_with_filtered_root(self):
105118
test_codebase = self.get_test_loc("resource/codebase")
106119
codebase = Codebase(test_codebase)

0 commit comments

Comments
 (0)