Skip to content

Commit

Permalink
Update input questions #2866
Browse files Browse the repository at this point in the history
  • Loading branch information
fritschldwg committed Dec 18, 2023
1 parent 20414df commit 83ac997
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FolderMover(private val project: Project) {
private var toMove: List<MutableNode>? = 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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ class ParserDialog {

private fun collectSetRootArguments(): Array<String> {
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<String> {
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<String> {
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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<KLogger>()
val warningMessagesLogged = mutableListOf<String>()
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<KLogger>()
val errorMessagesLogged = mutableListOf<String>()
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()
}
}

0 comments on commit 83ac997

Please sign in to comment.