From a6c82ba28bd1974f78b7ac11266a846aea006f85 Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Mon, 13 Jul 2020 12:44:05 +0300 Subject: [PATCH 1/9] Added debug variable and the corresponding conditionals for printing and logging. --- src/main/kotlin/ktee/KTee.kt | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ktee/KTee.kt b/src/main/kotlin/ktee/KTee.kt index e627681..9b6f047 100644 --- a/src/main/kotlin/ktee/KTee.kt +++ b/src/main/kotlin/ktee/KTee.kt @@ -1,7 +1,9 @@ package ktee +import ktee.KTee.Companion.debug import org.slf4j.Logger +class KTee { companion object { var debug = true } } /** * Prints the value to the stdout and returns the same value. Useful when chaining @@ -13,7 +15,7 @@ import org.slf4j.Logger * */ fun T.tee(marker: String = ""): T { - println(marker + this) + if (debug) println(marker + this) return this } @@ -23,7 +25,7 @@ fun T.tee(marker: String = ""): T { * */ inline fun T.tee(fn: (T) -> String): T { - println(fn(this)) + if (debug) println(fn(this)) return this } @@ -31,7 +33,7 @@ inline fun T.tee(fn: (T) -> String): T { * logs the value to the given logger at info level. Message can be customized using message parameter */ fun T.teeToInfo(logger: Logger, message: String = "{}"): T { - logger.info(message, this) + if (debug) logger.info(message, this) return this } @@ -40,7 +42,7 @@ fun T.teeToInfo(logger: Logger, message: String = "{}"): T { * */ inline fun T.teeToInfo(logger: Logger, fn: (T) -> String): T { - logger.info(fn(this), this) + if (debug) logger.info(fn(this), this) return this } @@ -48,7 +50,7 @@ inline fun T.teeToInfo(logger: Logger, fn: (T) -> String): T { * logs the value to the given logger at info level. Message can be customized using message parameter */ fun T.teeToDebug(logger: Logger, message: String = "{}"): T { - logger.debug(message, this) + if (debug) logger.debug(message, this) return this } @@ -57,7 +59,7 @@ fun T.teeToDebug(logger: Logger, message: String = "{}"): T { * */ inline fun T.teeToDebug(logger: Logger, fn: (T) -> String): T { - logger.debug(fn(this), this) + if (debug) logger.debug(fn(this), this) return this } @@ -65,7 +67,7 @@ inline fun T.teeToDebug(logger: Logger, fn: (T) -> String): T { * logs the value to the given logger at trace level. Message can be customized using message parameter */ fun T.teeToTrace(logger: Logger, message: String = "{}"): T { - logger.trace(message, this) + if (debug) logger.trace(message, this) return this } @@ -74,7 +76,7 @@ fun T.teeToTrace(logger: Logger, message: String = "{}"): T { * */ inline fun T.teeToTrace(logger: Logger, fn: (T) -> String): T { - logger.trace(fn(this), this) + if (debug) logger.trace(fn(this), this) return this } From 6fff5623c8655558ba49f8f2838492796f2b1cc0 Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Mon, 13 Jul 2020 13:47:43 +0300 Subject: [PATCH 2/9] Refactored extension functions. --- src/main/kotlin/ktee/KTee.kt | 46 +++++++++++------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/ktee/KTee.kt b/src/main/kotlin/ktee/KTee.kt index 9b6f047..5a7cecd 100644 --- a/src/main/kotlin/ktee/KTee.kt +++ b/src/main/kotlin/ktee/KTee.kt @@ -14,71 +14,53 @@ class KTee { companion object { var debug = true } } * myList.map(fn).tee(">>> ").reduce(fn) * */ -fun T.tee(marker: String = ""): T { - if (debug) println(marker + this) - return this -} +fun T.tee(marker: String = "") = apply { if (debug) println(marker + this) } /** * * executes the lambda with the value of the chain and writes the * */ -inline fun T.tee(fn: (T) -> String): T { - if (debug) println(fn(this)) - return this -} +inline fun T.tee(fn: (T) -> String) = apply { if (debug) println(fn(this)) } /** * logs the value to the given logger at info level. Message can be customized using message parameter */ -fun T.teeToInfo(logger: Logger, message: String = "{}"): T { - if (debug) logger.info(message, this) - return this -} +fun T.teeToInfo(logger: Logger, message: String = "{}") + = apply { if (debug) logger.info(message, this) } /** * Evaluates the lambda and logs the result (of evaluation) to the given logger at info level * */ -inline fun T.teeToInfo(logger: Logger, fn: (T) -> String): T { - if (debug) logger.info(fn(this), this) - return this -} +inline fun T.teeToInfo(logger: Logger, fn: (T) -> String) + = apply { if (debug) logger.info(fn(this), this) } /** * logs the value to the given logger at info level. Message can be customized using message parameter */ -fun T.teeToDebug(logger: Logger, message: String = "{}"): T { - if (debug) logger.debug(message, this) - return this -} +fun T.teeToDebug(logger: Logger, message: String = "{}") + = apply { if (debug) logger.debug(message, this) } /** * Evaluates the lambda and logs the result (of evaluation) to the given logger at debug level * */ -inline fun T.teeToDebug(logger: Logger, fn: (T) -> String): T { - if (debug) logger.debug(fn(this), this) - return this -} +inline fun T.teeToDebug(logger: Logger, fn: (T) -> String) + = apply { if (debug) logger.debug(fn(this), this) } /** * logs the value to the given logger at trace level. Message can be customized using message parameter */ -fun T.teeToTrace(logger: Logger, message: String = "{}"): T { - if (debug) logger.trace(message, this) - return this -} +fun T.teeToTrace(logger: Logger, message: String = "{}") + = apply { if (debug) logger.trace(message, this) } /** * Evaluates the lambda and logs the result (of evaluation) to the given logger at trace level * */ -inline fun T.teeToTrace(logger: Logger, fn: (T) -> String): T { - if (debug) logger.trace(fn(this), this) - return this -} +inline fun T.teeToTrace(logger: Logger, fn: (T) -> String) + = apply { if (debug) logger.trace(fn(this), this) } From a6b07882304e07a24aad50683a01bb9fadd098ee Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Mon, 13 Jul 2020 15:36:22 +0300 Subject: [PATCH 3/9] Upgraded to Kotlin 1.3.72. --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index e5d4261..3a33f32 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'org.jetbrains.kotlin.jvm' version '1.3.61' + id 'org.jetbrains.kotlin.jvm' version '1.3.72' } repositories { From ce780a89058786a39075657e6be77153313609c9 Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Thu, 16 Jul 2020 23:15:05 +0300 Subject: [PATCH 4/9] Disallowed KTee.debug to be changed twice. --- src/main/kotlin/ktee/KTee.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ktee/KTee.kt b/src/main/kotlin/ktee/KTee.kt index 5a7cecd..43a802a 100644 --- a/src/main/kotlin/ktee/KTee.kt +++ b/src/main/kotlin/ktee/KTee.kt @@ -3,7 +3,23 @@ package ktee import ktee.KTee.Companion.debug import org.slf4j.Logger -class KTee { companion object { var debug = true } } +class KTee { + companion object { + var debugChanged = false + private set + + /** + * If debug is set to false, all tee + * functions won't output anything. + * + * Variable can be set only once! + */ + var debug = true + @Synchronized set(value) { + if(!debugChanged) { debugChanged = true; field = value } + else throw IllegalStateException("Variable debug has already been set once.") + } + } } /** * Prints the value to the stdout and returns the same value. Useful when chaining From 14a1d472d7a6e23ebb6aab057d75822bf60164a1 Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Thu, 16 Jul 2020 23:53:47 +0300 Subject: [PATCH 5/9] Created tests for when debug is false. --- src/test/kotlin/ktee/KTeeTest.kt | 57 ++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/test/kotlin/ktee/KTeeTest.kt b/src/test/kotlin/ktee/KTeeTest.kt index 8b1378f..2d11c60 100644 --- a/src/test/kotlin/ktee/KTeeTest.kt +++ b/src/test/kotlin/ktee/KTeeTest.kt @@ -4,6 +4,7 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFailsWith import kotlin.test.assertTrue class KTeeTest { @@ -64,4 +65,60 @@ class KTeeTest { outputs.forEach { assertTrue { it.endsWith("hello myval\n") } } } + @Test + fun `should throw IllegalStateException`() { + assertFailsWith { + KTee.debug = false; KTee.debug = false } + } + + @Test + fun `should not write to stdout`() { + if (!KTee.debugChanged) KTee.debug = false + assertEquals("", trapOut { "myval".tee() }) + } + + @Test + fun `should evaluate lambda and not write to stdout`() { + if (!KTee.debugChanged) KTee.debug = false + assertEquals("", trapOut { "myval".tee { v -> "value is $v" } }) + } + + @Test + fun `should not write to logger`() { + if (!KTee.debugChanged) KTee.debug = false + assertTrue(trapErr { "myval".teeToInfo(logger) } == "") + } + + @Test + fun `should not log with info level`() { + if (!KTee.debugChanged) KTee.debug = false + val outputs = listOf( + trapErr { "myval".teeToInfo(logger, "hello {}") }, + trapErr { "myval".teeToInfo(logger) { "hello $it" } }, + trapErr { "myval".teeToInfo(logger) { "hello {}" } } + ) + outputs.forEach { assertTrue { !it.contains("INFO") } } + } + + @Test + fun `should not log with trace level`() { + if (!KTee.debugChanged) KTee.debug = false + val outputs = listOf( + trapErr { "myval".teeToTrace(logger, "hello {}") }, + trapErr { "myval".teeToTrace(logger) { "hello $it" } }, + trapErr { "myval".teeToTrace(logger) { "hello {}" } } + ) + outputs.forEach { assertTrue { it == "" } } + } + + @Test + fun `should not log with debug level`() { + if (!KTee.debugChanged) KTee.debug = false + val outputs = listOf( + trapErr { "myval".teeToDebug(logger, "hello {}") }, + trapErr { "myval".teeToDebug(logger) { "hello $it" } }, + trapErr { "myval".teeToDebug(logger) { "hello {}" } } + ) + outputs.forEach { assertTrue { !it.contains("DEBUG") } } + } } From cf6a4fffdceb6cab53fb07b29afd20628c5ae448 Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Fri, 17 Jul 2020 00:00:22 +0300 Subject: [PATCH 6/9] Replaced hardcoded "\n" with System.lineSeparator(), so the tests can run in CR LF systems. --- src/test/kotlin/ktee/KTeeTest.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/kotlin/ktee/KTeeTest.kt b/src/test/kotlin/ktee/KTeeTest.kt index 2d11c60..c480031 100644 --- a/src/test/kotlin/ktee/KTeeTest.kt +++ b/src/test/kotlin/ktee/KTeeTest.kt @@ -19,17 +19,17 @@ class KTeeTest { @Test fun `should write to stdout`() { - assertEquals("myval\n", trapOut { "myval".tee() }) + assertEquals("myval" + System.lineSeparator(), trapOut { "myval".tee() }) } @Test fun `should evaluate lambda and write to stdout`() { - assertEquals("value is myval\n", trapOut { "myval".tee { v -> "value is $v" } }) + assertEquals("value is myval" + System.lineSeparator(), trapOut { "myval".tee { v -> "value is $v" } }) } @Test fun `should write to logger`() { - assertTrue(trapErr { "myval".teeToInfo(logger) }.endsWith("myval\n")) + assertTrue(trapErr { "myval".teeToInfo(logger) }.endsWith("myval" + System.lineSeparator())) } @Test @@ -40,18 +40,18 @@ class KTeeTest { trapErr { "myval".teeToInfo(logger) { "hello {}" } } ) outputs.forEach { assertTrue { it.contains("INFO") } } - outputs.forEach { assertTrue { it.endsWith("hello myval\n") } } + outputs.forEach { assertTrue { it.endsWith("hello myval" + System.lineSeparator()) } } } @Test - fun `should log with trace() level`() { + fun `should log with trace level`() { val outputs = listOf( trapErr { "myval".teeToTrace(logger, "hello {}") }, trapErr { "myval".teeToTrace(logger) { "hello $it" } }, trapErr { "myval".teeToTrace(logger) { "hello {}" } } ) outputs.forEach { assertTrue { it.contains("TRACE") } } - outputs.forEach { assertTrue { it.endsWith("hello myval\n") } } + outputs.forEach { assertTrue { it.endsWith("hello myval" + System.lineSeparator()) } } } @Test @@ -62,7 +62,7 @@ class KTeeTest { trapErr { "myval".teeToDebug(logger) { "hello {}" } } ) outputs.forEach { assertTrue { it.contains("DEBUG") } } - outputs.forEach { assertTrue { it.endsWith("hello myval\n") } } + outputs.forEach { assertTrue { it.endsWith("hello myval" + System.lineSeparator()) } } } @Test From 600211a7eda5c08771389565334c4bc40dfa58b6 Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Fri, 17 Jul 2020 00:19:27 +0300 Subject: [PATCH 7/9] Minor typo in test functions. --- src/test/kotlin/ktee/KTeeTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/ktee/KTeeTest.kt b/src/test/kotlin/ktee/KTeeTest.kt index c480031..5502a0b 100644 --- a/src/test/kotlin/ktee/KTeeTest.kt +++ b/src/test/kotlin/ktee/KTeeTest.kt @@ -78,7 +78,7 @@ class KTeeTest { } @Test - fun `should evaluate lambda and not write to stdout`() { + fun `should not evaluate lambda or write to stdout`() { if (!KTee.debugChanged) KTee.debug = false assertEquals("", trapOut { "myval".tee { v -> "value is $v" } }) } From dd6833ecd44c12ef3b71ae005f180b11d8ccb96e Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Wed, 15 Dec 2021 21:00:29 +0200 Subject: [PATCH 8/9] Reverted "Created tests for when debug is false.". This reverts commit 5c96e9b4 (cherry picked from commit 091249cdd549f6f5c0f2916a3cff5907d8a255a1) --- src/test/kotlin/ktee/KTeeTest.kt | 57 -------------------------------- 1 file changed, 57 deletions(-) diff --git a/src/test/kotlin/ktee/KTeeTest.kt b/src/test/kotlin/ktee/KTeeTest.kt index 5502a0b..b9d47d0 100644 --- a/src/test/kotlin/ktee/KTeeTest.kt +++ b/src/test/kotlin/ktee/KTeeTest.kt @@ -4,7 +4,6 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertFailsWith import kotlin.test.assertTrue class KTeeTest { @@ -65,60 +64,4 @@ class KTeeTest { outputs.forEach { assertTrue { it.endsWith("hello myval" + System.lineSeparator()) } } } - @Test - fun `should throw IllegalStateException`() { - assertFailsWith { - KTee.debug = false; KTee.debug = false } - } - - @Test - fun `should not write to stdout`() { - if (!KTee.debugChanged) KTee.debug = false - assertEquals("", trapOut { "myval".tee() }) - } - - @Test - fun `should not evaluate lambda or write to stdout`() { - if (!KTee.debugChanged) KTee.debug = false - assertEquals("", trapOut { "myval".tee { v -> "value is $v" } }) - } - - @Test - fun `should not write to logger`() { - if (!KTee.debugChanged) KTee.debug = false - assertTrue(trapErr { "myval".teeToInfo(logger) } == "") - } - - @Test - fun `should not log with info level`() { - if (!KTee.debugChanged) KTee.debug = false - val outputs = listOf( - trapErr { "myval".teeToInfo(logger, "hello {}") }, - trapErr { "myval".teeToInfo(logger) { "hello $it" } }, - trapErr { "myval".teeToInfo(logger) { "hello {}" } } - ) - outputs.forEach { assertTrue { !it.contains("INFO") } } - } - - @Test - fun `should not log with trace level`() { - if (!KTee.debugChanged) KTee.debug = false - val outputs = listOf( - trapErr { "myval".teeToTrace(logger, "hello {}") }, - trapErr { "myval".teeToTrace(logger) { "hello $it" } }, - trapErr { "myval".teeToTrace(logger) { "hello {}" } } - ) - outputs.forEach { assertTrue { it == "" } } - } - - @Test - fun `should not log with debug level`() { - if (!KTee.debugChanged) KTee.debug = false - val outputs = listOf( - trapErr { "myval".teeToDebug(logger, "hello {}") }, - trapErr { "myval".teeToDebug(logger) { "hello $it" } }, - trapErr { "myval".teeToDebug(logger) { "hello {}" } } - ) - outputs.forEach { assertTrue { !it.contains("DEBUG") } } - } } From 38b28ffa3c8a471bf0a82a3f91b65c1efbd9438e Mon Sep 17 00:00:00 2001 From: Aristos Pasalides Date: Wed, 15 Dec 2021 23:12:36 +0200 Subject: [PATCH 9/9] Implemented No-Op artifact. (cherry picked from commit 876c111bcc4f21f707cd9763c2cea5daa72b849b) --- build.gradle | 38 +++++++++++++++++++++++++++++++++--- src/main/kotlin/ktee/KTee.kt | 35 ++++++++------------------------- src/noop/kotlin/ktee/KTee.kt | 16 +++++++++++++++ 3 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 src/noop/kotlin/ktee/KTee.kt diff --git a/build.gradle b/build.gradle index 7cf43d5..5d2b11c 100644 --- a/build.gradle +++ b/build.gradle @@ -17,15 +17,25 @@ tasks { } } +sourceSets { + noop { + java.srcDir 'src/noop/kotlin' + } +} + dependencies { implementation platform('org.jetbrains.kotlin:kotlin-bom') implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' - implementation 'org.slf4j:slf4j-api:1.7.28' - testImplementation 'org.slf4j:slf4j-simple:1.7.28' + implementation 'org.slf4j:slf4j-api:1.7.32' + testImplementation 'org.slf4j:slf4j-simple:1.7.32' + testImplementation 'org.jetbrains.kotlin:kotlin-test' testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5' + + noopImplementation 'org.slf4j:slf4j-nop:1.7.32' + noopImplementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8' } task dokkaJar(type: Jar) { @@ -41,8 +51,28 @@ task sourcesJar(type: Jar) { from(sourceSets.getByName("main").allSource) } +task noopSources(type: Jar) { + archiveClassifier.set("sources") + from(sourceSets.noop.allSource) +} + +task noopBinary(type: Jar) { + archiveClassifier.set("") + from(sourceSets.noop.output) +} + publishing { publications { + noop(MavenPublication) { + artifact noopSources + artifact noopBinary + + pom { + name = "ktee-noop" + artifactId = "ktee-noop" + } + } + mavenJava(MavenPublication) { from components.java artifact sourcesJar @@ -50,7 +80,8 @@ publishing { pom { name = "ktee" - description = "KTee is Tee for Kotlin code pipelines. If you love the unix command line tee, you know what we mean." + description = "KTee is Tee for Kotlin code pipelines. " + + "If you love the unix command line tee, you know what we mean." url.set("https://github.com/medly/ktee") licenses { license { @@ -103,4 +134,5 @@ signing { def signingPassword = System.getenv("OSSRH_SIGNING_PASSPHRASE") useInMemoryPgpKeys(signingKey, signingPassword) sign publishing.publications.mavenJava + sign publishing.publications.noop } diff --git a/src/main/kotlin/ktee/KTee.kt b/src/main/kotlin/ktee/KTee.kt index 43a802a..533a311 100644 --- a/src/main/kotlin/ktee/KTee.kt +++ b/src/main/kotlin/ktee/KTee.kt @@ -1,26 +1,7 @@ package ktee -import ktee.KTee.Companion.debug import org.slf4j.Logger -class KTee { - companion object { - var debugChanged = false - private set - - /** - * If debug is set to false, all tee - * functions won't output anything. - * - * Variable can be set only once! - */ - var debug = true - @Synchronized set(value) { - if(!debugChanged) { debugChanged = true; field = value } - else throw IllegalStateException("Variable debug has already been set once.") - } - } } - /** * Prints the value to the stdout and returns the same value. Useful when chaining * methods. For example: @@ -30,53 +11,53 @@ class KTee { * myList.map(fn).tee(">>> ").reduce(fn) * */ -fun T.tee(marker: String = "") = apply { if (debug) println(marker + this) } +fun T.tee(marker: String = "") = apply { println(marker + this) } /** * * executes the lambda with the value of the chain and writes the * */ -inline fun T.tee(fn: (T) -> String) = apply { if (debug) println(fn(this)) } +inline fun T.tee(fn: (T) -> String) = apply { println(fn(this)) } /** * logs the value to the given logger at info level. Message can be customized using message parameter */ fun T.teeToInfo(logger: Logger, message: String = "{}") - = apply { if (debug) logger.info(message, this) } + = apply { logger.info(message, this) } /** * Evaluates the lambda and logs the result (of evaluation) to the given logger at info level * */ inline fun T.teeToInfo(logger: Logger, fn: (T) -> String) - = apply { if (debug) logger.info(fn(this), this) } + = apply { logger.info(fn(this), this) } /** * logs the value to the given logger at info level. Message can be customized using message parameter */ fun T.teeToDebug(logger: Logger, message: String = "{}") - = apply { if (debug) logger.debug(message, this) } + = apply { logger.debug(message, this) } /** * Evaluates the lambda and logs the result (of evaluation) to the given logger at debug level * */ inline fun T.teeToDebug(logger: Logger, fn: (T) -> String) - = apply { if (debug) logger.debug(fn(this), this) } + = apply { logger.debug(fn(this), this) } /** * logs the value to the given logger at trace level. Message can be customized using message parameter */ fun T.teeToTrace(logger: Logger, message: String = "{}") - = apply { if (debug) logger.trace(message, this) } + = apply { logger.trace(message, this) } /** * Evaluates the lambda and logs the result (of evaluation) to the given logger at trace level * */ inline fun T.teeToTrace(logger: Logger, fn: (T) -> String) - = apply { if (debug) logger.trace(fn(this), this) } + = apply { logger.trace(fn(this), this) } diff --git a/src/noop/kotlin/ktee/KTee.kt b/src/noop/kotlin/ktee/KTee.kt new file mode 100644 index 0000000..6aa9d28 --- /dev/null +++ b/src/noop/kotlin/ktee/KTee.kt @@ -0,0 +1,16 @@ +package ktee + +import org.slf4j.Logger + +inline fun T.tee(fn: (T) -> String) = this +inline fun T.tee(marker: String = "") = this +inline fun T.teeToInfo(logger: Logger, fn: (T) -> String) = this +inline fun T.teeToDebug(logger: Logger, fn: (T) -> String) = this +inline fun T.teeToTrace(logger: Logger, fn: (T) -> String) = this +inline fun T.teeToInfo(logger: Logger, message: String = "{}") = this +inline fun T.teeToDebug(logger: Logger, message: String = "{}") = this +inline fun T.teeToTrace(logger: Logger, message: String = "{}") = this + + + +