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

Detekt issues #28

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ jobs:
distribution: 'zulu'
java-version: 17

- name: Build & Tests
run: ./gradlew build :linguine-generator:test :linguine-generator:functionalTest :linguine-runtime:allTests
- name: Detekt & Build & Tests
run: ./gradlew detekt build :linguine-generator:test :linguine-generator:functionalTest :linguine-runtime:allTests
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.cleverlance.linguine.linguinegenerator

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.nio.file.Path
import kotlin.test.BeforeTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir

class FileWriterTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package io.github.cleverlance.linguine.linguinegenerator

import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.nio.file.Paths
import kotlin.io.path.createTempDirectory
import kotlin.test.assertTrue
import org.gradle.testkit.runner.GradleRunner
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir

class LinguinePluginFunctionalTest {

@TempDir
lateinit var testProjectDir: File

private var buildSuccessOutput: String = "BUILD SUCCESSFUL"
private var gradleBuildFileName = "build.gradle.kts"
private val generateTaskName = "generateStrings"
private val buildSuccessOutput: String = "BUILD SUCCESSFUL"
private val gradleBuildFileName = "build.gradle.kts"

@Test
fun `plugin task executes successfully`() {
fun whenGenerateTaskExecutedThenCompletedSuccessfully() {
File(testProjectDir, gradleBuildFileName).apply {
writeText(
"""
Expand Down Expand Up @@ -49,14 +50,15 @@ class LinguinePluginFunctionalTest {
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withPluginClasspath()
.withArguments("generateStrings")
.withArguments(generateTaskName)
.build()

assert(result.output.contains(buildSuccessOutput))
}

@Suppress("LongMethod")
@Test
fun `plugin generates expected Kotlin file from JSON configuration`() {
fun whenGenerateTaskExecutedThenOutputsFileContainsExpectedContent() {
testProjectDir.resolve(gradleBuildFileName).apply {
writeText(
"""
Expand Down Expand Up @@ -89,7 +91,7 @@ class LinguinePluginFunctionalTest {

val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withArguments("generateStrings")
.withArguments(generateTaskName)
.withPluginClasspath()
.build()

Expand Down Expand Up @@ -136,7 +138,7 @@ class LinguinePluginFunctionalTest {
}

@Test
fun `plugin generates file at specified location with correct content`() {
fun whenGenerateTaskExecutedThenOutputFilePlacedInConfiguredPath() {
val testProjectDir = createTempDirectory().toFile()

File(testProjectDir, "settings.gradle.kts").writeText("")
Expand Down Expand Up @@ -173,7 +175,7 @@ class LinguinePluginFunctionalTest {
val result = GradleRunner.create()
.withProjectDir(testProjectDir)
.withPluginClasspath()
.withArguments("generateStrings")
.withArguments(generateTaskName)
.forwardOutput()
.build()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.cleverlance.linguine.linguinegenerator.filereader

import java.io.File
import org.gradle.internal.impldep.junit.framework.TestCase.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File

class FileReaderTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,7 @@ class FileContentGenerator(
private val fileContent: Map<String, String>,
) {

private companion object {
const val DEFAULT_INDENT = " "
val FORMAT_SPECIFIER_REGEX = Regex("%[0-9]*\\\$[sdf]|%[sdf]")
}

private val filePackage: String by lazy {
fun Path.isSourceDirectory(): Boolean = isDirectory()
&& (name == "kotlin" || name == "java")
&& parent?.parent?.name == "src"

var sourcePath: Path? = filePath
while (sourcePath != null && !sourcePath.isSourceDirectory()) {
sourcePath = sourcePath.parent
}

if (sourcePath == null) return@lazy "" // no package

val relativeDirectoryPath = filePath.parent.relativeTo(sourcePath)
relativeDirectoryPath.joinToString(separator = ".") { path -> path.name }
}
private val filePackage: String by lazy { getFilePackage(filePath) }

fun generateFileContent(root: Map<String, Any>): String {
return FileSpec.builder(filePackage, "")
Expand All @@ -53,6 +34,22 @@ class FileContentGenerator(
.toString()
}

private fun getFilePackage(filePath: Path): String {
fun Path.isSourceDirectory(): Boolean = isDirectory() &&
(name == "kotlin" || name == "java") &&
parent?.parent?.name == "src"

var sourcePath: Path? = filePath
while (sourcePath != null && !sourcePath.isSourceDirectory()) {
sourcePath = sourcePath.parent
}

if (sourcePath == null) return "" // no package

val relativeDirectoryPath = filePath.parent.relativeTo(sourcePath)
return relativeDirectoryPath.joinToString(separator = ".") { path -> path.name }
}

private fun TypeSpec.Builder.addObjectContent(root: Map<String, Any>): TypeSpec.Builder {
@Suppress("UNCHECKED_CAST") // presuming the structure of the map
root.forEach { (key, value) ->
Expand Down Expand Up @@ -115,4 +112,9 @@ class FileContentGenerator(
else -> Any::class
}
}

private companion object {
const val DEFAULT_INDENT = " "
val FORMAT_SPECIFIER_REGEX = Regex("%[0-9]*\\\$[sdf]|%[sdf]")
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.cleverlance.linguine.linguinegenerator

import io.github.cleverlance.linguine.linguinegenerator.filereader.FileType as LinguineFileType
import org.gradle.api.provider.Property as GradleProperty
import io.github.cleverlance.linguine.linguine_generator.BuildConfig
import io.github.cleverlance.linguine.linguinegenerator.filereader.FileReader
import org.gradle.api.DefaultTask
Expand All @@ -17,17 +19,10 @@ import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType
import org.gradle.work.Incremental
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import io.github.cleverlance.linguine.linguinegenerator.filereader.FileType as LinguineFileType
import org.gradle.api.provider.Property as GradleProperty

@Suppress("unused")
class LinguinePlugin : Plugin<Project> {

private companion object {
const val GENERATE_STRINGS_TASK_NAME = "generateStrings"
const val RUNTIME_DEPENDENCY = "${BuildConfig.GROUP}:linguine-runtime:${BuildConfig.VERSION}"
}

override fun apply(project: Project) {
val extension = project.extensions.create("linguineConfig", LinguineConfig::class.java)

Expand Down Expand Up @@ -73,7 +68,6 @@ class LinguinePlugin : Plugin<Project> {
configureGenerateStringsTask(project, extension)
}


private fun configureForJvm(project: Project, extension: LinguineConfig) {
project.dependencies {
add("implementation", RUNTIME_DEPENDENCY)
Expand All @@ -89,6 +83,11 @@ class LinguinePlugin : Plugin<Project> {
buildTasks.forEach { task -> task.dependsOn(GENERATE_STRINGS_TASK_NAME) }
}
}

private companion object {
const val GENERATE_STRINGS_TASK_NAME = "generateStrings"
const val RUNTIME_DEPENDENCY = "${BuildConfig.GROUP}:linguine-runtime:${BuildConfig.VERSION}"
}
}

@CacheableTask
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.cleverlance.linguine.linguineruntime.presentation

import co.touchlab.kermit.Logger
import kotlin.native.concurrent.ThreadLocal
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlin.native.concurrent.ThreadLocal

@ThreadLocal
internal object LocalisationProvider {
Expand Down