From b32bbcb9ad3181abf141dfafda343522e3ba9195 Mon Sep 17 00:00:00 2001 From: Viktor Kolomeyko Date: Tue, 10 Oct 2023 16:10:35 +0100 Subject: [PATCH] CORE-17606, CORE-15885: Remove `SetCurrentNodeCommand` As well as dependency on `com.github.stefanbirkner:system-lambda` library. --- .../kotlin/net/corda/cli/application/Boot.kt | 8 +--- .../commands/SetCurrentNodeCommand.kt | 34 --------------- .../corda/cli/application/dto/NodeProfile.kt | 10 ----- .../net/corda/cli/application/utils/Files.kt | 26 ------------ .../commands/SetCurrentNodeCommandTest.kt | 42 ------------------- .../corda/cli/application/utils/FilesTest.kt | 33 --------------- plugins/example/build.gradle | 1 - .../cli/plugins/examples/ExamplePluginTest.kt | 40 +++++++++++++++--- 8 files changed, 36 insertions(+), 158 deletions(-) delete mode 100644 app/src/main/kotlin/net/corda/cli/application/commands/SetCurrentNodeCommand.kt delete mode 100644 app/src/main/kotlin/net/corda/cli/application/dto/NodeProfile.kt delete mode 100644 app/src/main/kotlin/net/corda/cli/application/utils/Files.kt delete mode 100644 app/src/test/kotlin/net/corda/cli/application/commands/SetCurrentNodeCommandTest.kt delete mode 100644 app/src/test/kotlin/net/corda/cli/application/utils/FilesTest.kt diff --git a/app/src/main/kotlin/net/corda/cli/application/Boot.kt b/app/src/main/kotlin/net/corda/cli/application/Boot.kt index 5eb25f7..154961f 100644 --- a/app/src/main/kotlin/net/corda/cli/application/Boot.kt +++ b/app/src/main/kotlin/net/corda/cli/application/Boot.kt @@ -1,9 +1,7 @@ package net.corda.cli.application import net.corda.cli.api.CordaCliPlugin -import net.corda.cli.application.commands.SetCurrentNodeCommand import net.corda.cli.application.logger.LoggerStream -import net.corda.cli.application.utils.Files import org.pf4j.CompoundPluginDescriptorFinder import org.pf4j.DefaultPluginManager import org.pf4j.ManifestPluginDescriptorFinder @@ -17,8 +15,7 @@ fun main(vararg args: String) { } @CommandLine.Command( - name = "corda-cli", - subcommands = [SetCurrentNodeCommand::class] + name = "corda-cli" ) class App { @CommandLine.Option(names = ["-h", "--help", "-?", "-help"], usageHelp = true, description = ["Display help and exit."]) @@ -45,9 +42,6 @@ object Boot { // Setup loggers to redirect sysOut and sysErr LoggerStream.redirectSystemAndErrorOut() - // create storage dir if it doesn't exist - Files.cliHomeDir().mkdirs() - // Find and load the CLI plugins val pluginsDir = System.getProperty("pf4j.pluginsDir", "./plugins") val pluginManager = PluginManager(listOf(Paths.get(pluginsDir))) diff --git a/app/src/main/kotlin/net/corda/cli/application/commands/SetCurrentNodeCommand.kt b/app/src/main/kotlin/net/corda/cli/application/commands/SetCurrentNodeCommand.kt deleted file mode 100644 index 4636f89..0000000 --- a/app/src/main/kotlin/net/corda/cli/application/commands/SetCurrentNodeCommand.kt +++ /dev/null @@ -1,34 +0,0 @@ -package net.corda.cli.application.commands - -import net.corda.cli.application.utils.Files -import org.yaml.snakeyaml.Yaml -import picocli.CommandLine -import java.io.FileInputStream -import java.util.concurrent.Callable -import java.io.PrintWriter - - -@CommandLine.Command( - name = "set-node", - description = ["Sets the current target for http requests."], - mixinStandardHelpOptions = true -) -class SetCurrentNodeCommand : Callable { - - @CommandLine.Spec - lateinit var spec: CommandLine.Model.CommandSpec - - @CommandLine.Option(names = ["-t", "--target-url"], description = ["The Url of the target."], required = true) - var url: String? = null - - private val yaml = Yaml() - private val data: MutableMap = yaml.load(FileInputStream(Files.profile)) - - override fun call(): Int { - url?.let { data.put("url", it) } - val writer = PrintWriter(Files.profile) - yaml.dump(data, writer) - writer.close() - return 0 - } -} diff --git a/app/src/main/kotlin/net/corda/cli/application/dto/NodeProfile.kt b/app/src/main/kotlin/net/corda/cli/application/dto/NodeProfile.kt deleted file mode 100644 index edc084d..0000000 --- a/app/src/main/kotlin/net/corda/cli/application/dto/NodeProfile.kt +++ /dev/null @@ -1,10 +0,0 @@ -package net.corda.cli.application.dto - -data class NodeProfile( - var urlRoot: String -) -{ - override fun toString(): String { - return "Node Profile Url Root: $urlRoot" - } -} \ No newline at end of file diff --git a/app/src/main/kotlin/net/corda/cli/application/utils/Files.kt b/app/src/main/kotlin/net/corda/cli/application/utils/Files.kt deleted file mode 100644 index bb83e05..0000000 --- a/app/src/main/kotlin/net/corda/cli/application/utils/Files.kt +++ /dev/null @@ -1,26 +0,0 @@ -package net.corda.cli.application.utils - -import java.io.File -import java.nio.file.Paths - -class Files { - companion object { - - fun cliHomeDir(): File { - return if (System.getenv("CORDA_CLI_HOME_DIR").isNullOrEmpty()) { - Paths.get(System.getProperty("user.home"), "/.corda/cli/").toFile() - } else { - Paths.get(System.getenv("CORDA_CLI_HOME_DIR")).toFile() - } - } - - val profile: File by lazy { - val profileFile = Paths.get(cliHomeDir().path, "/profile.yaml").toFile() - if (!profileFile.exists()) { - profileFile.createNewFile() - profileFile.writeText("default:") - } - return@lazy profileFile - } - } -} diff --git a/app/src/test/kotlin/net/corda/cli/application/commands/SetCurrentNodeCommandTest.kt b/app/src/test/kotlin/net/corda/cli/application/commands/SetCurrentNodeCommandTest.kt deleted file mode 100644 index 597806f..0000000 --- a/app/src/test/kotlin/net/corda/cli/application/commands/SetCurrentNodeCommandTest.kt +++ /dev/null @@ -1,42 +0,0 @@ -package net.corda.cli.application.commands - -// TODO: https://r3-cev.atlassian.net/browse/CORE-15885 -//import net.corda.cli.application.App -//import net.corda.cli.application.utils.Files -//import org.junit.jupiter.api.Assertions.assertEquals -//import org.junit.jupiter.api.Test -//import org.yaml.snakeyaml.Yaml -//import picocli.CommandLine -//import java.io.FileInputStream -//import java.io.FileWriter -//import java.util.* - - -class SetCurrentNodeCommandTest { - -// @Test -// fun setCurrentNodeUrlTest() { -// -// val randomPrefix = UUID.randomUUID() -// -// withEnvironmentVariable( -// "CORDA_CLI_HOME_DIR", -// "build/test-data/.cli-host/${randomPrefix}/" -// ).execute { -// Files.cliHomeDir().mkdirs() -// -// val testUrl = "www.${UUID.randomUUID()}.com" -// var data: MutableMap = mutableMapOf(Pair("url", "emptyUrl")) -// val yaml = Yaml() -// -// yaml.dump(data, FileWriter(Files.profile)) -// -// CommandLine( -// App() -// ).execute("set-node", "-t=$testUrl") -// -// data = yaml.load(FileInputStream(Files.profile)) -// assertEquals(testUrl, data["url"]) -// } -// } -} \ No newline at end of file diff --git a/app/src/test/kotlin/net/corda/cli/application/utils/FilesTest.kt b/app/src/test/kotlin/net/corda/cli/application/utils/FilesTest.kt deleted file mode 100644 index c87df63..0000000 --- a/app/src/test/kotlin/net/corda/cli/application/utils/FilesTest.kt +++ /dev/null @@ -1,33 +0,0 @@ -package net.corda.cli.application.utils - -// TODO: https://r3-cev.atlassian.net/browse/CORE-15885 -//import org.junit.jupiter.api.Assertions.assertEquals -//import org.junit.jupiter.api.Test -//import java.nio.file.Paths - -class FilesTest { -// -// @Test -// fun testCliHomeDirNoEnvVar() { -// -// withEnvironmentVariable("CORDA_CLI_HOME_DIR", "").execute { -// assertEquals( -// Paths.get(System.getProperty("user.home"), "/.corda/cli/").toFile().absolutePath, -// Files.cliHomeDir().absolutePath -// ) -// } -// } -// -// @Test -// fun testCliHomeDirWithEnvVar() { -// -// val workingDir = Paths.get("").toAbsolutePath().toString() -// -// withEnvironmentVariable("CORDA_CLI_HOME_DIR", "build/test-data/.cli-host/").execute { -// assertEquals( -// Paths.get(workingDir,"build/test-data/.cli-host/").toString(), -// Files.cliHomeDir().absolutePath -// ) -// } -// } -} \ No newline at end of file diff --git a/plugins/example/build.gradle b/plugins/example/build.gradle index 01eaefa..b9179f7 100644 --- a/plugins/example/build.gradle +++ b/plugins/example/build.gradle @@ -10,7 +10,6 @@ dependencies { testImplementation project(":api") testImplementation "org.pf4j:pf4j:${pf4jVersion}" - testImplementation "com.github.stefanbirkner:system-lambda:1.2.1" testImplementation "org.junit.jupiter:junit-jupiter:${junitJupiterVersion}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}" testRuntimeOnly 'org.junit.platform:junit-platform-launcher' diff --git a/plugins/example/src/test/kotlin/net/corda/cli/plugins/examples/ExamplePluginTest.kt b/plugins/example/src/test/kotlin/net/corda/cli/plugins/examples/ExamplePluginTest.kt index 8517afe..35b9604 100644 --- a/plugins/example/src/test/kotlin/net/corda/cli/plugins/examples/ExamplePluginTest.kt +++ b/plugins/example/src/test/kotlin/net/corda/cli/plugins/examples/ExamplePluginTest.kt @@ -1,19 +1,49 @@ package net.corda.cli.plugins.examples -import com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErrNormalized -import com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutNormalized import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import picocli.CommandLine +import java.io.ByteArrayOutputStream +import java.io.PrintStream class ExamplePluginTest { + + companion object { + private fun tapSystemErr(function: () -> Int): String { + val initial = System.err + + val byteArrayOutputStream = ByteArrayOutputStream() + System.setErr(PrintStream(byteArrayOutputStream)) + try { + function() + } finally { + System.setErr(initial) + } + + return String(byteArrayOutputStream.toByteArray()).replace("\r\n", "\n") + } + + private fun tapSystemOut(function: () -> Int): String { + val initial = System.out + + val byteArrayOutputStream = ByteArrayOutputStream() + System.setOut(PrintStream(byteArrayOutputStream)) + try { + function() + } finally { + System.setOut(initial) + } + + return String(byteArrayOutputStream.toByteArray()).replace("\r\n", "\n") + } + } @Test fun testNoOptionCommand() { val app = ExamplePlugin.ExamplePluginEntry() - val outText = tapSystemErrNormalized { + val outText = tapSystemErr { CommandLine( app ).execute("") @@ -35,7 +65,7 @@ class ExamplePluginTest { fun testSubCommand() { val app = ExamplePlugin.ExamplePluginEntry() - val outText = tapSystemOutNormalized { + val outText = tapSystemOut { CommandLine( app ).execute("sub-command") @@ -48,7 +78,7 @@ class ExamplePluginTest { fun testUnknownCommand() { val app = ExamplePlugin.ExamplePluginEntry() - val outText = tapSystemErrNormalized { + val outText = tapSystemErr { CommandLine( app ).execute("unknown-command")