Skip to content

Commit c3eca3f

Browse files
committed
fix: detektAll findings
1 parent bf96e4a commit c3eca3f

12 files changed

Lines changed: 105 additions & 39 deletions

File tree

build-plugin/plugin/src/main/kotlin/net/thunderbird/gradle/plugin/quality/detekt/DetektPlugin.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class DetektPlugin : Plugin<Project> {
5252
private fun Project.configureDetektTasks() {
5353
with(tasks) {
5454
withType<Detekt>().configureEach {
55+
if (name.contains("androidHostTest", ignoreCase = true)) {
56+
enabled = false
57+
}
58+
5559
jvmTarget = ProjectConfig.Compiler.jvmTarget.target
5660

5761
exclude(defaultExcludes)
@@ -66,6 +70,10 @@ class DetektPlugin : Plugin<Project> {
6670
}
6771

6872
withType<DetektCreateBaselineTask>().configureEach {
73+
if (name.contains("androidHostTest", ignoreCase = true)) {
74+
enabled = false
75+
}
76+
6977
jvmTarget = ProjectConfig.Compiler.jvmTarget.target
7078

7179
exclude(defaultExcludes)
@@ -98,6 +106,7 @@ private val defaultExcludes = listOf(
98106
"**/.gradle/**",
99107
"**/.idea/**",
100108
"**/build/**",
109+
"**/generated/**",
101110
".github/**",
102111
"gradle/**",
103112
)

cli/weblate-cli/src/main/kotlin/net/thunderbird/cli/weblate/ComponentConfigDiff.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ private class SetField(
9797
val actualValue = selector(actual)
9898

9999
return if (expectedValue.toSet() != actualValue.toSet()) {
100-
listDiff(name, expectedValue, actualValue, indentLevel)
100+
listDiff(
101+
name = name,
102+
expected = expectedValue,
103+
actual = actualValue,
104+
indentLevel = indentLevel,
105+
)
101106
} else {
102107
null
103108
}
@@ -142,7 +147,12 @@ private class MultilineField(
142147
val actualValue = selector(actual)
143148

144149
return if (expectedValue != actualValue) {
145-
multilineDiff(name, expectedValue, actualValue, indentLevel)
150+
multilineDiff(
151+
name = name,
152+
expected = expectedValue,
153+
actual = actualValue,
154+
indentLevel = indentLevel,
155+
)
146156
} else {
147157
null
148158
}

core/common/src/commonMain/kotlin/net/thunderbird/core/common/state/debug/StatePrettyPrinterVocabulary.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import net.thunderbird.core.common.state.debug.extension.prependIndent
99
* Provides constants and formatting functions used by [StatePrettyPrinter] to produce
1010
* human-readable representations of state machine transitions and history dumps.
1111
*/
12+
@Suppress("TooManyFunctions")
1213
internal object StatePrettyPrinterVocabulary {
1314
const val STATE_CHANGE_MARKER = "->"
1415
const val STATE_NO_CHANGE_MARKER = ""

core/configstore/api/src/commonMain/kotlin/net/thunderbird/core/configstore/ConfigDefinition.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ package net.thunderbird.core.configstore
44
* A definition of how to manage a configuration for specific type.
55
*
66
* The id of the configuration is used as a unique identifier to distinguish it from other configurations and
7-
* is used by the [net.thunderbird.core.configstore.backend.ConfigBackendProvider] to retrieve the correct configuration backend.
7+
* is used by the [net.thunderbird.core.configstore.backend.ConfigBackendProvider] to retrieve the correct
8+
* configuration backend.
89
*
9-
* This allows configurations to selectively share a backend, which can be useful for performance or organizational purposes.
10-
* It also allows for the creation of multiple configurations that can be managed independently, even if they share the same backend.
10+
* This allows configurations to selectively share a backend, which can be useful for performance or organizational
11+
* purposes.
12+
* It also allows for the creation of multiple configurations that can be managed independently, even if they share
13+
* the same backend.
1114
*
1215
* @param T The type of the configuration object.
1316
*
@@ -22,7 +25,8 @@ interface ConfigDefinition<T> {
2225
/**
2326
* The id of the configuration.
2427
*
25-
* It is used by the [net.thunderbird.core.configstore.backend.ConfigBackendProvider] to retrieve the correct configuration backend.
28+
* It is used by the [net.thunderbird.core.configstore.backend.ConfigBackendProvider] to retrieve the correct
29+
* configuration backend.
2630
*/
2731
val id: ConfigId
2832

core/configstore/impl-backend/src/commonMain/kotlin/net/thunderbird/core/configstore/backend/DefaultDataStoreConfigBackend.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class DefaultDataStoreConfigBackend(
3939
config
4040
}
4141

42+
@Suppress("CyclomaticComplexMethod")
4243
override suspend fun update(keys: List<ConfigKey<*>>, transform: (Config) -> Config) {
4344
val current = read(keys).first()
4445
val updated = transform(current)

core/file/src/commonMain/kotlin/net/thunderbird/core/file/command/CopyCommand.kt

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package net.thunderbird.core.file.command
22

33
import com.eygraber.uri.Uri
44
import kotlinx.io.Buffer
5+
import kotlinx.io.IOException
6+
import kotlinx.io.RawSink
7+
import kotlinx.io.RawSource
58
import net.thunderbird.core.file.FileOperationError
69
import net.thunderbird.core.file.FileSystemManager
710
import net.thunderbird.core.file.WriteMode
@@ -15,46 +18,77 @@ internal class CopyCommand(
1518
private val destinationUri: Uri,
1619
) : FileCommand<Unit> {
1720
override suspend fun invoke(fs: FileSystemManager): Outcome<Unit, FileOperationError> {
18-
// Open endpoints
1921
val source = fs.openSource(sourceUri)
20-
?: return Outcome.Failure(
22+
val sink = fs.openSink(destinationUri, WriteMode.Truncate)
23+
24+
return if (source == null) {
25+
Outcome.Failure(
2126
FileOperationError.Unavailable(sourceUri, "Unable to open source: $sourceUri"),
2227
)
23-
val sink = fs.openSink(destinationUri, WriteMode.Truncate)
24-
?: return Outcome.Failure(
28+
} else if (sink == null) {
29+
Outcome.Failure(
2530
FileOperationError.Unavailable(destinationUri, "Unable to open destination: $destinationUri"),
2631
)
32+
} else {
33+
copy(source, sink)
34+
}
35+
}
36+
37+
private fun copy(source: RawSource, sink: RawSink): Outcome<Unit, FileOperationError> {
38+
val buffer = Buffer()
2739

2840
return try {
29-
val buffer = Buffer()
30-
while (true) {
31-
val read = try {
32-
source.readAtMostTo(buffer, BUFFER_SIZE)
33-
} catch (e: Exception) {
34-
return Outcome.Failure(FileOperationError.ReadFailed(sourceUri, e.message), cause = e)
35-
}
36-
if (read <= 0L) break
37-
try {
38-
sink.write(buffer, read)
39-
} catch (e: Exception) {
40-
return Outcome.Failure(FileOperationError.WriteFailed(destinationUri, e.message), cause = e)
41-
}
42-
}
43-
try {
44-
sink.flush()
45-
} catch (e: Exception) {
46-
return Outcome.Failure(FileOperationError.WriteFailed(destinationUri, e.message), cause = e)
47-
}
41+
copyToSink(source, sink, buffer)
42+
flushSink(sink)
4843
Outcome.Success(Unit)
49-
} catch (e: Exception) {
44+
} catch (e: FileOperationException) {
45+
Outcome.Failure(e.error, cause = e.cause)
46+
} catch (e: IOException) {
5047
Outcome.Failure(FileOperationError.Unknown(e.message), cause = e)
5148
} finally {
49+
closeQuietly(source, sink)
50+
}
51+
}
52+
53+
private fun copyToSink(source: RawSource, sink: RawSink, buffer: Buffer) {
54+
while (true) {
55+
val read = try {
56+
source.readAtMostTo(buffer, BUFFER_SIZE)
57+
} catch (e: IOException) {
58+
throw FileOperationException(FileOperationError.ReadFailed(sourceUri, e.message), e)
59+
}
60+
61+
if (read <= 0L) break
62+
5263
try {
53-
source.close()
54-
} catch (_: Exception) {}
55-
try {
56-
sink.close()
57-
} catch (_: Exception) {}
64+
sink.write(buffer, read)
65+
} catch (e: IOException) {
66+
throw FileOperationException(FileOperationError.WriteFailed(destinationUri, e.message), e)
67+
}
68+
}
69+
}
70+
71+
private fun flushSink(sink: RawSink) {
72+
try {
73+
sink.flush()
74+
} catch (e: IOException) {
75+
throw FileOperationException(FileOperationError.WriteFailed(destinationUri, e.message), e)
76+
}
77+
}
78+
79+
private class FileOperationException(
80+
val error: FileOperationError,
81+
override val cause: Throwable?,
82+
) : Exception(error.toString(), cause)
83+
84+
private fun closeQuietly(source: AutoCloseable?, sink: AutoCloseable?) {
85+
try {
86+
source?.close()
87+
} catch (_: Exception) {
88+
}
89+
try {
90+
sink?.close()
91+
} catch (_: Exception) {
5892
}
5993
}
6094

core/file/src/commonMain/kotlin/net/thunderbird/core/file/command/CreateDirectoriesCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.thunderbird.core.file.command
22

33
import com.eygraber.uri.Uri
4+
import kotlinx.io.IOException
45
import net.thunderbird.core.file.FileOperationError
56
import net.thunderbird.core.file.FileSystemManager
67
import net.thunderbird.core.outcome.Outcome
@@ -15,7 +16,7 @@ internal class CreateDirectoriesCommand(
1516
return try {
1617
fs.createDirectories(dirUri)
1718
Outcome.Success(Unit)
18-
} catch (e: Exception) {
19+
} catch (e: IOException) {
1920
Outcome.Failure(
2021
error = FileOperationError.Unavailable(dirUri, e.message ?: "Unable to create directory: $dirUri"),
2122
cause = e,

core/file/src/jvmMain/kotlin/net/thunderbird/core/file/JvmFileSystemManager.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ class JvmFileSystemManager : FileSystemManager {
4646
if (!file.delete() && file.exists()) {
4747
throw IOException("Unable to delete file at: $uri")
4848
}
49-
} catch (error: Exception) {
49+
} catch (error: IOException) {
5050
throw IOException("Unable to delete file at: $uri", error)
5151
}
5252
}
5353

54+
@Suppress("TooGenericExceptionCaught")
5455
override fun createDirectories(uri: Uri) {
5556
try {
5657
val file = File(uri.toURI())

core/logging/api/src/commonMain/kotlin/net/thunderbird/core/logging/LogLevel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package net.thunderbird.core.logging
1414
*
1515
* @param priority The priority of the log level, where a lower priority indicates a more verbose level.
1616
*/
17+
@Suppress("MagicNumber")
1718
enum class LogLevel(
1819
val priority: Int,
1920
) {

core/logging/impl-legacy/src/commonMain/kotlin/net/thunderbird/core/logging/legacy/Log.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import net.thunderbird.core.logging.LogTag
66
import net.thunderbird.core.logging.Logger
77

88
/**
9-
* A static logging utility that implements [net.thunderbird.core.logging.Logger] and delegates to a [net.thunderbird.core.logging.Logger] implementation.
9+
* A static logging utility that implements [net.thunderbird.core.logging.Logger] and delegates to a
10+
* [net.thunderbird.core.logging.Logger] implementation.
1011
*
1112
* You can initialize it in your application startup code, for example:
1213
*
@@ -27,6 +28,7 @@ import net.thunderbird.core.logging.Logger
2728
message = "Use a net.thunderbird.core.logging.Logger instance via dependency injection instead. " +
2829
"This class will be removed in a future release.",
2930
)
31+
@Suppress("TooManyFunctions")
3032
object Log : Logger {
3133

3234
lateinit var logger: Logger
@@ -143,6 +145,7 @@ object Log : Logger {
143145
logger.error(message = { formatMessage(message, args) }, throwable = t)
144146
}
145147

148+
@Suppress("SpreadOperator", "TooGenericExceptionCaught")
146149
private fun formatMessage(message: String?, args: Array<out Any?>): String {
147150
return if (message == null) {
148151
""

0 commit comments

Comments
 (0)