Skip to content

Commit

Permalink
Remove absolute file path handling #2866
Browse files Browse the repository at this point in the history
  • Loading branch information
fritschldwg committed Dec 14, 2023
1 parent 9e154f1 commit 0ebc0ee
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,26 @@ class NodeRemover(private val project: Project) {

private fun removeNodes(paths: List<List<String>>): List<MutableNode> {
val rootNode = project.rootNode.toMutableNode()

for (path in paths) {
var currentNode = rootNode
val rootElementIndex = path.indexOfFirst { it == "root" }
val pathSegmentsWithoutRoot = path.subList(rootElementIndex + 1, path.size)
for ((index, pathSegment) in pathSegmentsWithoutRoot.withIndex()) {
val isLastSegment = index == pathSegmentsWithoutRoot.lastIndex
if (path[0] != "root") {
logger.warn("Path to node has to start with root: ${path.joinToString("/")}")
continue
}

val pathWithoutRoot = path.drop(1)
for ((index, pathSegment) in pathWithoutRoot.withIndex()) {
val isLastSegment = index == pathWithoutRoot.lastIndex
if (isLastSegment) {
currentNode.children.removeIf { it.name == pathSegment }
} else {
currentNode = currentNode.children.find { it.name == pathSegment } ?: break
val childNode: MutableNode? = currentNode.children.find { it.name == pathSegment }
if (childNode != null) {
currentNode = childNode
} else {
logger.warn("Path to node not found: ${path.joinToString("/")}")
break
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,45 +155,4 @@ class NodeRemoverTest {
Assertions.assertThat(folderToKeep).isNotNull()
Assertions.assertThat(folderToRemove).isNull()
}

@Test
fun `Should correctly remove node when arguments starting with a forward slash get converted to absolute paths (git-bash)`() {
// given
val bufferedReader = File("src/test/resources/merged_project.cc.json").bufferedReader()
val mergedProject = ProjectDeserializer.deserializeProject(bufferedReader)
val subProjectExtractor = NodeRemover(mergedProject)
val pathToRemove = "/root/src/test/java/io"
val pathToRemoveAbsolute = "C:/Users/User/AppData/Local/Programs/Git$pathToRemove"

// when
val result = subProjectExtractor.remove(arrayOf(pathToRemoveAbsolute))
val folderToRemove = result.rootNode.children.find { it.name == "src" }
?.children?.find { it.name == "test" }
?.children?.find { it.name == "java" }
?.children?.find { it.name == "io" }

// then
Assertions.assertThat(folderToRemove).isNull()
}

@Test
fun `Should correctly remove node when remove value is absolute path and contains an additional 'root' path element`() {
// given
val bufferedReader = File("src/test/resources/merged_project.cc.json").bufferedReader()
val mergedProject = ProjectDeserializer.deserializeProject(bufferedReader)
val subProjectExtractor = NodeRemover(mergedProject)
val pathToRemove = "/root/src/test/java/io/root"
val pathToRemoveAbsolute = "C:/Users/root/AppData/Local/Programs/Git$pathToRemove"

// when
val result = subProjectExtractor.remove(arrayOf(pathToRemoveAbsolute))
val folderToRemove = result.rootNode.children.find { it.name == "src" }
?.children?.find { it.name == "test" }
?.children?.find { it.name == "java" }
?.children?.find { it.name == "io" }
?.children?.find { it.name == "root" }

// then
Assertions.assertThat(folderToRemove).isNull()
}
}

0 comments on commit 0ebc0ee

Please sign in to comment.