Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dependency io.github.microutils:kotlin-logging-jvm to v3 #1529

Merged
merged 11 commits into from
Oct 31, 2022
14 changes: 0 additions & 14 deletions diktat-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,6 @@
<groupId>com.pinterest.ktlint</groupId>
<artifactId>ktlint-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.cqfn.diktat.common.config.reader

import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging
import org.slf4j.Logger

import java.io.BufferedReader
import java.io.IOException
Expand Down Expand Up @@ -58,9 +57,6 @@ abstract class JsonResourceConfigReader<T> {
protected abstract fun parseResource(fileStream: BufferedReader): T

companion object {
/**
* A [Logger] that can be used
*/
val log: Logger = KotlinLogging.loggerWithKtlintConfig(JsonResourceConfigReader::class)
private val log = KotlinLogging.loggerWithKtlintConfig(JsonResourceConfigReader::class)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import org.cqfn.diktat.common.utils.loggerWithKtlintConfig

import com.charleskorn.kaml.Yaml
import com.charleskorn.kaml.YamlConfiguration
import mu.KLogger
import mu.KotlinLogging
import org.slf4j.Logger

import java.io.BufferedReader
import java.io.File
Expand Down Expand Up @@ -111,7 +111,7 @@ open class RulesConfigReader(override val classLoader: ClassLoader) : JsonResour
/**
* A [Logger] that can be used
*/
val log: Logger = KotlinLogging.loggerWithKtlintConfig(RulesConfigReader::class)
val log: KLogger = KotlinLogging.loggerWithKtlintConfig(RulesConfigReader::class)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package org.cqfn.diktat.common.utils

import com.pinterest.ktlint.core.initKtLintKLogger
import mu.KotlinLogging
import org.slf4j.LoggerFactory
import kotlin.reflect.KClass

/**
Expand All @@ -25,4 +24,4 @@ fun KotlinLogging.loggerWithKtlintConfig(func: () -> Unit) =
* @return a logger
*/
fun KotlinLogging.loggerWithKtlintConfig(clazz: KClass<*>) =
logger(LoggerFactory.getLogger(clazz.java)).initKtLintKLogger()
logger(clazz.java.name).initKtLintKLogger()
14 changes: 0 additions & 14 deletions diktat-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,6 @@
<groupId>io.github.microutils</groupId>
<artifactId>kotlin-logging-jvm</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.cqfn.diktat.ruleset.utils.*

import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION
Expand Down Expand Up @@ -38,51 +39,53 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(

private fun searchNode(node: ASTNode, configuration: EmptyBlockStyleConfiguration) {
val newNode = node.findLBrace()?.treeParent ?: return
checkEmptyBlock(newNode, configuration)
if (!isAllowedEmptyBlock(newNode) && newNode.isBlockEmpty()) {
checkEmptyBlock(newNode, configuration)
}
}

private fun isNewLine(node: ASTNode) =
node.findChildByType(WHITE_SPACE)?.text?.contains("\n") ?: false

private fun isAllowedEmptyBlock(node: ASTNode) = node.treeParent.isOverridden() ||
isAnonymousSamClass(node) ||
Fixed Show fixed Hide fixed
isLambdaUsedAsFunction(node) ||
Fixed Show fixed Hide fixed
isKotlinLogging(node)

@Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION")
private fun checkEmptyBlock(node: ASTNode, configuration: EmptyBlockStyleConfiguration) {
if (node.treeParent.isOverridden() || isAnonymousSamClass(node) || isLambdaUsedAsFunction(node)) {
return
}
if (node.isBlockEmpty()) {
if (!configuration.emptyBlockExist) {
EMPTY_BLOCK_STRUCTURE_ERROR.warn(configRules, emitWarn, isFixMode, "empty blocks are forbidden unless it is function with override keyword",
node.startOffset, node)
} else {
node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let {
// Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE)
if (isNewLine(node)) {
val freeText = "do not put newlines in empty lambda"
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) {
val whiteSpaceNode = node.findChildByType(WHITE_SPACE)
whiteSpaceNode?.let {
node.replaceChild(whiteSpaceNode, PsiWhiteSpaceImpl(" "))
}
if (!configuration.emptyBlockExist) {
EMPTY_BLOCK_STRUCTURE_ERROR.warn(configRules, emitWarn, isFixMode, "empty blocks are forbidden unless it is function with override keyword",
node.startOffset, node)
} else {
node.findParentNodeWithSpecificType(ElementType.LAMBDA_ARGUMENT)?.let {
// Lambda body is always has a BLOCK -> run { } - (LBRACE, WHITE_SPACE, BLOCK "", RBRACE)
if (isNewLine(node)) {
val freeText = "do not put newlines in empty lambda"
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, freeText, node.startOffset, node) {
val whiteSpaceNode = node.findChildByType(WHITE_SPACE)
whiteSpaceNode?.let {
node.replaceChild(whiteSpaceNode, PsiWhiteSpaceImpl(" "))
}
}
return
}
val space = node.findChildByType(RBRACE)!!.treePrev
if (configuration.emptyBlockNewline && !space.text.contains("\n")) {
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "different style for empty block",
node.startOffset, node) {
if (space.elementType == WHITE_SPACE) {
(space.treeNext as LeafPsiElement).rawReplaceWithText("\n")
} else {
node.addChild(PsiWhiteSpaceImpl("\n"), space.treeNext)
}
}
} else if (!configuration.emptyBlockNewline && space.text.contains("\n")) {
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "different style for empty block",
node.startOffset, node) {
node.removeChild(space)
return
}
val space = node.findChildByType(RBRACE)!!.treePrev
if (configuration.emptyBlockNewline && !space.text.contains("\n")) {
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "different style for empty block",
node.startOffset, node) {
if (space.elementType == WHITE_SPACE) {
(space.treeNext as LeafPsiElement).rawReplaceWithText("\n")
} else {
node.addChild(PsiWhiteSpaceImpl("\n"), space.treeNext)
}
}
} else if (!configuration.emptyBlockNewline && space.text.contains("\n")) {
EMPTY_BLOCK_STRUCTURE_ERROR.warnAndFix(configRules, emitWarn, isFixMode, "different style for empty block",
node.startOffset, node) {
node.removeChild(space)
}
}
}
}
Expand Down Expand Up @@ -118,6 +121,13 @@ class EmptyBlock(configRules: List<RulesConfig>) : DiktatRule(
}
}

private fun isKotlinLogging(node: ASTNode): Boolean = node.findParentNodeWithSpecificType(DOT_QUALIFIED_EXPRESSION)
?.text
?.replace(" ", "")
.let {
it == "KotlinLogging.logger{}"
}

/**
* [RuleConfiguration] for empty blocks formatting
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,19 @@ class EmptyBlockWarnTest : LintTestBase(::EmptyBlock) {
rulesConfigList = rulesConfigListEmptyBlockExist
)
}

@Test
@Tag(WarningNames.EMPTY_BLOCK_STRUCTURE_ERROR)
fun `should not trigger on KotlinLogging logger`() {
lintMethod(
"""
|import mu.KotlinLogging
|
|fun some() {
| val log = KotlinLogging.logger {}
| log.info { "test" }
|}
""".trimMargin(),
)
}
}
10 changes: 0 additions & 10 deletions diktat-ruleset/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@
<artifactId>kotlin-compiler-embeddable</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
8 changes: 0 additions & 8 deletions diktat-test-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
package org.cqfn.diktat.test.framework.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;

public class StreamGobbler extends Thread {
private static final Logger log = LoggerFactory.getLogger(StreamGobbler.class);
private final InputStream inputStream;
private final String streamType;
private final BiConsumer<Exception, String> exceptionHandler;
private final ArrayList<String> result;
private volatile boolean isStopped = false;

/**
* @param inputStream the InputStream to be consumed
* @param streamType the stream type (should be OUTPUT or ERROR)
* @param streamType the stream type (should be OUTPUT or ERROR)
* @param exceptionHandler the exception handler
*/
public StreamGobbler(final InputStream inputStream, final String streamType) {
public StreamGobbler(
final InputStream inputStream,
final String streamType,
final BiConsumer<Exception, String> exceptionHandler
) {
this.inputStream = inputStream;
this.streamType = streamType;
this.exceptionHandler = exceptionHandler;
this.result = new ArrayList<>();
}

Expand All @@ -43,7 +47,7 @@ synchronized public void run() {
this.result.add(line);
}
} catch (IOException ex) {
log.error("Failed to consume and display the input stream of type " + streamType + ".", ex);
exceptionHandler.accept(ex, "Failed to consume and display the input stream of type " + streamType + ".");
} finally {
this.isStopped = true;
notify();
Expand All @@ -55,7 +59,7 @@ synchronized public List<String> getContent() {
try {
wait();
} catch (InterruptedException e) {
log.error("Cannot get content of output stream", e);
exceptionHandler.accept(e, "Cannot get content of output stream");
}
}
return this.result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cqfn.diktat.test.framework.common

import org.slf4j.LoggerFactory
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging

import java.io.IOException

Expand All @@ -15,14 +16,18 @@ class LocalCommandExecutor internal constructor(private val command: String) {
*/
fun executeCommand(): ExecutionResult {
try {
log.info("Executing command: {}", command)
log.info { "Executing command: $command" }
val process = Runtime.getRuntime().exec(command)
process.outputStream.close()
val inputStream = process.inputStream
val outputGobbler = StreamGobbler(inputStream, "OUTPUT")
val outputGobbler = StreamGobbler(inputStream, "OUTPUT") { msg, ex ->
log.error(ex, msg)
}
outputGobbler.start()
val errorStream = process.errorStream
val errorGobbler = StreamGobbler(errorStream, "ERROR")
val errorGobbler = StreamGobbler(errorStream, "ERROR") { msg, ex ->
log.error(ex, msg)
}
errorGobbler.start()
return ExecutionResult(outputGobbler.content, errorGobbler.content)
} catch (ex: IOException) {
Expand All @@ -32,6 +37,7 @@ class LocalCommandExecutor internal constructor(private val command: String) {
}

companion object {
private val log = LoggerFactory.getLogger(LocalCommandExecutor::class.java)
@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val log = KotlinLogging.loggerWithKtlintConfig {}
Fixed Show fixed Hide fixed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package org.cqfn.diktat.test.framework.config

import org.cqfn.diktat.common.cli.CliArgument
import org.cqfn.diktat.common.config.reader.JsonResourceConfigReader
import org.cqfn.diktat.common.utils.loggerWithKtlintConfig
import mu.KotlinLogging

import org.apache.commons.cli.CommandLine
import org.apache.commons.cli.CommandLineParser
import org.apache.commons.cli.DefaultParser
import org.apache.commons.cli.HelpFormatter
import org.apache.commons.cli.Options
import org.apache.commons.cli.ParseException
import org.slf4j.LoggerFactory

import java.io.BufferedReader
import java.io.IOException
Expand Down Expand Up @@ -94,6 +95,7 @@ class TestArgumentsReader(
}

companion object {
private val log = LoggerFactory.getLogger(TestArgumentsReader::class.java)
@Suppress("EMPTY_BLOCK_STRUCTURE_ERROR")
private val log = KotlinLogging.loggerWithKtlintConfig {}
Fixed Show fixed Hide fixed
}
}
Loading