diff --git a/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/FolderMover.kt b/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/FolderMover.kt index 84f2621323..3dfb491b2a 100644 --- a/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/FolderMover.kt +++ b/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/FolderMover.kt @@ -16,7 +16,7 @@ class FolderMover(private val project: Project) { private var toMove: List? = null fun move(moveFrom: String?, moveTo: String?): Project? { - if ((moveFrom == null) || (moveTo == null)) { + if ((moveFrom.isNullOrEmpty()) || (moveTo.isNullOrEmpty())) { logger.error("In order to move nodes, both source and destination need to be set.") return null } diff --git a/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/ParserDialog.kt b/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/ParserDialog.kt index a66b578778..4abc1cf6b5 100644 --- a/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/ParserDialog.kt +++ b/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/ParserDialog.kt @@ -45,23 +45,23 @@ class ParserDialog { private fun collectSetRootArguments(): Array { val setRoot: String = - KInquirer.promptInput(message = "What path within project to be extracted?") + KInquirer.promptInput(message = "What path within project to be extracted as the new root?") val outputFileName = collectOutputFileName() return arrayOf("--set-root=$setRoot", "--output-file=$outputFileName") } private fun collectMoveNodesArguments(): Array { val moveFrom: String = - KInquirer.promptInput(message = "What are the nodes to be moved?") + KInquirer.promptInput(message = "What path should be moved (containing children will be moved also)?") val moveTo: String = - KInquirer.promptInput(message = "Where to move them?") + KInquirer.promptInput(message = "What is the target path to move them?") val outputFileName = collectOutputFileName() return arrayOf("--move-from=$moveFrom", "--move-to=$moveTo", "--output-file=$outputFileName") } private fun collectRemoveNodesArguments(): Array { val remove: String = - KInquirer.promptInput(message = "What are the nodes to be removed?") + KInquirer.promptInput(message = "What is the path of the nodes to be removed?") val outputFileName = collectOutputFileName() return arrayOf("--remove=$remove", "--output-file=$outputFileName") } diff --git a/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifier.kt b/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifier.kt index f417e75908..cf600736b5 100644 --- a/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifier.kt +++ b/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifier.kt @@ -33,7 +33,7 @@ class StructureModifier( @CommandLine.Parameters(arity = "0..1", paramLabel = "FILE", description = ["input project file"]) private var source: File? = null - @CommandLine.Option(names = ["-s", "--set-root"], description = ["path within project to be extracted"]) + @CommandLine.Option(names = ["-s", "--set-root"], description = ["path within project to be extracted as the new roo"]) private var setRoot: String? = null @CommandLine.Option( @@ -76,6 +76,10 @@ class StructureModifier( } override fun call(): Unit? { + if (isMoreThanOneActionSpecified()) { + logger.warn("More than one action specified - aborting execution.") + } + project = readProject() ?: return null when { @@ -94,6 +98,15 @@ class StructureModifier( return null } + private fun isMoreThanOneActionSpecified(): Boolean { + var actionCount = 0 + actionCount += if (setRoot != null) 1 else 0 + actionCount += if (printLevels != null) 1 else 0 + actionCount += if (moveFrom != null) 1 else 0 + actionCount += if (remove.isNotEmpty()) 1 else 0 + return actionCount > 1 + } + private fun readProject(): Project? { if (source == null) { return ProjectDeserializer.deserializeProject(input) diff --git a/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifierAction.kt b/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifierAction.kt index 2bc1b64cab..02e895f479 100644 --- a/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifierAction.kt +++ b/analysis/filter/StructureModifier/src/main/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifierAction.kt @@ -2,7 +2,7 @@ package de.maibornwolff.codecharta.filter.structuremodifier enum class StructureModifierAction(val descripton: String) { PRINT_STRUCTURE("Print the structure of the project"), - SET_ROOT("Extract a subproject"), - MOVE_NODES("Reorder nodes inside the project"), + SET_ROOT("Extract a sub path as the new root"), + MOVE_NODES("Move nodes within the project"), REMOVE_NODES("Remove nodes") } diff --git a/analysis/filter/StructureModifier/src/test/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifierTest.kt b/analysis/filter/StructureModifier/src/test/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifierTest.kt index b7c0b2fe73..f4f73bd911 100644 --- a/analysis/filter/StructureModifier/src/test/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifierTest.kt +++ b/analysis/filter/StructureModifier/src/test/kotlin/de/maibornwolff/codecharta/filter/structuremodifier/StructureModifierTest.kt @@ -3,8 +3,11 @@ package de.maibornwolff.codecharta.filter.structuremodifier import de.maibornwolff.codecharta.serialization.ProjectDeserializer import de.maibornwolff.codecharta.util.InputHelper import io.mockk.every +import io.mockk.mockk import io.mockk.mockkObject import io.mockk.unmockkAll +import mu.KLogger +import mu.KotlinLogging import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test @@ -176,4 +179,42 @@ class StructureModifierTest { assertThat(cliResult).doesNotContain(file1) assertThat(cliResult).doesNotContain(file2) } + + @Test + fun `should log warning when more than one action is specified`() { + // given + val file1 = "/root/src/main/file1.java" + val file2 = "/root/src/main/file2.java" + val nodesToRemove = listOf(file1, file2) + + val loggerMock = mockk() + val warningMessagesLogged = mutableListOf() + mockkObject(KotlinLogging) + every { KotlinLogging.logger(any<(() -> Unit)>()) } returns loggerMock + every { loggerMock.warn(capture(warningMessagesLogged)) } returns Unit + + // when + executeForOutput("", arrayOf("src/test/resources/sample_project.cc.json", "--remove", "$nodesToRemove", "--set-root", "$nodesToRemove")) + + // then + assertThat(warningMessagesLogged).isNotEmpty() + } + + @Test + fun `should log error when move-from but not move-to is specified`() { + // given + val folderToMove = "/root/src/main" + + val loggerMock = mockk() + val errorMessagesLogged = mutableListOf() + mockkObject(KotlinLogging) + every { KotlinLogging.logger(any<(() -> Unit)>()) } returns loggerMock + every { loggerMock.error(capture(errorMessagesLogged)) } returns Unit + + // when + executeForOutput("", arrayOf("src/test/resources/sample_project.cc.json", "--move-from", folderToMove, "--move-to", "")) + + // then + assertThat(errorMessagesLogged).isNotEmpty() + } }