Skip to content

Commit

Permalink
build: add ktlint (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
monosoul authored Nov 11, 2023
1 parent c77d606 commit f399c5a
Show file tree
Hide file tree
Showing 61 changed files with 2,283 additions and 2,105 deletions.
3 changes: 2 additions & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"org.junit.jupiter:",
"io.strikt:",
"io.mockk:",
"ch.qos.logback:"
"ch.qos.logback:",
"com.pinterest.ktlint:ktlint-cli:"
]
},
{
Expand Down
1 change: 1 addition & 0 deletions artifact-tests/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
`kotlin-convention`
`linter-convention`
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@ import org.testcontainers.containers.output.ToStringConsumer
import org.testcontainers.containers.startupcheck.IndefiniteWaitOneShotStartupCheckStrategy

class GradleContainer(
dockerSocketPath: String = "/var/run/docker.sock",
dockerSocketPath: String = "/var/run/docker.sock",
) : GenericContainer<GradleContainer>("gradle:jdk17-alpine") {

private val toStringLogConsumer = ToStringConsumer()
val output: String get() = toStringLogConsumer.toUtf8String()
val projectPath = "/home/gradle/project"

init {
withLogConsumer(
Slf4jLogConsumer(LoggerFactory.getLogger("GradleContainer[$dockerImageName]"))
Slf4jLogConsumer(LoggerFactory.getLogger("GradleContainer[$dockerImageName]")),
)
withLogConsumer(toStringLogConsumer)
addFsBind("build/local-repository", "$projectPath/local-repository")
addFsBind("/var/run/docker.sock", dockerSocketPath)
withWorkingDirectory(projectPath)

withStartupCheckStrategy(
IndefiniteWaitOneShotStartupCheckStrategy()
IndefiniteWaitOneShotStartupCheckStrategy(),
)
}

private fun addFsBind(hostPath: String, containerPath: String) =
addFileSystemBind(hostPath, containerPath, READ_ONLY, SHARED)
private fun addFsBind(
hostPath: String,
containerPath: String,
) = addFileSystemBind(hostPath, containerPath, READ_ONLY, SHARED)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import strikt.assertions.contains
import strikt.assertions.isSuccess

class JooqCodegenLoggingLevelsTest {

@ParameterizedTest
@ValueSource(strings = ["", "--quiet"])
fun `should not have jOOQ codegen warnings with non verbose log levels`(logLevel: String) {
// given
val gradleContainer = GradleContainer().apply {
setUp()
val arguments = listOfNotNull(
"gradle",
"generateJooqClasses",
logLevel.takeIf { it.isNotBlank() }
).toTypedArray()
withCommand(*arguments)
}
val gradleContainer =
GradleContainer().apply {
setUp()
val arguments =
listOfNotNull(
"gradle",
"generateJooqClasses",
logLevel.takeIf { it.isNotBlank() },
).toTypedArray()
withCommand(*arguments)
}

// when & then
expect {
Expand All @@ -41,10 +42,11 @@ class JooqCodegenLoggingLevelsTest {
@ValueSource(strings = ["--info", "--warn", "--debug"])
fun `should have jOOQ codegen warnings with verbose log levels`(logLevel: String) {
// given
val gradleContainer = GradleContainer().apply {
setUp()
withCommand("gradle", "generateJooqClasses", logLevel)
}
val gradleContainer =
GradleContainer().apply {
setUp()
withCommand("gradle", "generateJooqClasses", logLevel)
}

// when & then
expect {
Expand All @@ -64,59 +66,59 @@ class JooqCodegenLoggingLevelsTest {
withCopyToContainer(
Transferable.of(
"""
import org.jooq.meta.jaxb.Logging
import org.jooq.meta.jaxb.Logging
plugins {
id("dev.monosoul.jooq-docker") version "${Versions.PLUGIN_VERSION}"
}
plugins {
id("dev.monosoul.jooq-docker") version "${Versions.PLUGIN_VERSION}"
}
repositories {
mavenCentral()
}
repositories {
mavenCentral()
}
tasks {
generateJooqClasses {
withContainer {
image {
name = "postgres:14.4-alpine"
}
tasks {
generateJooqClasses {
withContainer {
image {
name = "postgres:14.4-alpine"
}
}
}
}
dependencies {
jooqCodegen("org.postgresql:postgresql:42.3.6")
}
""".trimIndent()
dependencies {
jooqCodegen("org.postgresql:postgresql:42.3.6")
}
""".trimIndent(),
),
"$projectPath/build.gradle.kts"
"$projectPath/build.gradle.kts",
)
withCopyToContainer(
Transferable.of(
"""
pluginManagement {
repositories {
maven {
name = "localBuild"
url = uri("./local-repository")
}
mavenCentral()
gradlePluginPortal {
content {
excludeGroup("org.jooq")
excludeGroup("org.flywaydb")
excludeGroupByRegex("com\\.fasterxml.*")
excludeGroupByRegex("com\\.google.*")
excludeGroupByRegex("org\\.junit.*")
excludeGroupByRegex("net\\.java.*")
excludeGroupByRegex("jakarta.*")
}
pluginManagement {
repositories {
maven {
name = "localBuild"
url = uri("./local-repository")
}
mavenCentral()
gradlePluginPortal {
content {
excludeGroup("org.jooq")
excludeGroup("org.flywaydb")
excludeGroupByRegex("com\\.fasterxml.*")
excludeGroupByRegex("com\\.google.*")
excludeGroupByRegex("org\\.junit.*")
excludeGroupByRegex("net\\.java.*")
excludeGroupByRegex("jakarta.*")
}
}
}
""".trimIndent()
}
""".trimIndent(),
),
"$projectPath/settings.gradle.kts"
"$projectPath/settings.gradle.kts",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import strikt.assertions.contains
import strikt.assertions.isSuccess

class PluginWorksArtifactTest {

/**
* Runs the plugin in a containerized environment to make sure the artifact produced actually works.
* Running in a container is necessary to make sure host's testcontainers.properties file doesn't interfere
Expand All @@ -16,20 +15,21 @@ class PluginWorksArtifactTest {
@Test
fun `should be possible to load the plugin and generate jooq classes`() {
// given
val gradleContainer = GradleContainer().apply {
withCopyToContainer(forClasspathResource("/testproject"), projectPath)
withCopyToContainer(
forClasspathResource("/.testcontainers.properties.template"),
"/root/.testcontainers.properties"
)
withEnv("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY", "org.testcontainers.dockerclient.UnixSocketClientProviderStrategy")
withCommand(
"gradle",
"classes",
"--info",
"--stacktrace",
)
}
val gradleContainer =
GradleContainer().apply {
withCopyToContainer(forClasspathResource("/testproject"), projectPath)
withCopyToContainer(
forClasspathResource("/.testcontainers.properties.template"),
"/root/.testcontainers.properties",
)
withEnv("TESTCONTAINERS_DOCKER_CLIENT_STRATEGY", "org.testcontainers.dockerclient.UnixSocketClientProviderStrategy")
withCommand(
"gradle",
"classes",
"--info",
"--stacktrace",
)
}

// when & then
expect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.utility.DockerImageName

class PostgresContainer(
image: String = "postgres:14.4-alpine"
image: String = "postgres:14.4-alpine",
) : PostgreSQLContainer<PostgresContainer>(DockerImageName.parse(image))
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import strikt.assertions.contains
import strikt.assertions.isSuccess

class WorksAfterFailingToFindDockerArtifactTest {

@Test
fun `should be possible to generate jooq classes even after it failed on first attempt`() {
// given
val gradleContainer = GradleContainer(dockerSocketPath = "/var/run/docker-alt.sock").apply {
withEnv("TESTCONTAINERS_RYUK_DISABLED", "true")
withCopyToContainer(forClasspathResource("/testproject"), projectPath)
withCopyToContainer(forClasspathResource("/gradle_run.sh"), "/gradle_run.sh")
withCommand("/gradle_run.sh")
}
val gradleContainer =
GradleContainer(dockerSocketPath = "/var/run/docker-alt.sock").apply {
withEnv("TESTCONTAINERS_RYUK_DISABLED", "true")
withCopyToContainer(forClasspathResource("/testproject"), projectPath)
withCopyToContainer(forClasspathResource("/gradle_run.sh"), "/gradle_run.sh")
withCommand("/gradle_run.sh")
}

// when & then
expect {
Expand Down
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
`coverage-convention`
`shadow-convention`
`test-fixtures-convention`
`linter-convention`
}

group = "dev.monosoul.jooq"
Expand All @@ -16,7 +17,7 @@ tasks {
relocate("docker.client.strategy", "${project.group}.docker.client.strategy")
relocate(
"TESTCONTAINERS_DOCKER_CLIENT_STRATEGY",
"${project.group.toString().uppercase().replace(".", "_")}_TESTCONTAINERS_DOCKER_CLIENT_STRATEGY"
"${project.group.toString().uppercase().replace(".", "_")}_TESTCONTAINERS_DOCKER_CLIENT_STRATEGY",
)
}

Expand Down
93 changes: 93 additions & 0 deletions buildSrc/src/main/kotlin/linter-convention.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE
import org.gradle.api.attributes.Bundling.EXTERNAL

plugins {
kotlin("jvm")
}

val Project.libs: VersionCatalog
get() = rootProject.extensions.getByType<VersionCatalogsExtension>().named("libs")

val ktlint: Configuration by configurations.creating

dependencies {
ktlint(libs.findLibrary("ktlint").get()) {
attributes {
attributes {
attribute(BUNDLING_ATTRIBUTE, getObjects().named(Bundling::class, EXTERNAL))
}
}
}
}

tasks {
val sourceInputFiles = fileTree(mapOf("dir" to "src", "include" to "**/*.kt"))

val ktlintCheckSources by registering(JavaExec::class) {
inputs.files(sourceInputFiles)

description = "Check Kotlin code style."
group = "ktlint"
classpath = ktlint
mainClass.set("com.pinterest.ktlint.Main")
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
args("--color", "src/**/*.kt")
}

val ktlintFormatSources by registering(JavaExec::class) {
inputs.files(sourceInputFiles)

description = "Fix Kotlin code style deviations."
group = "ktlint"
classpath = ktlint
mainClass.set("com.pinterest.ktlint.Main")
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
args("--color", "-F", "src/**/*.kt")
}

val buildscriptInputFiles = fileTree(mapOf("dir" to ".", "include" to "*.gradle.kts"))

val ktlintCheckBuildScript by registering(JavaExec::class) {
inputs.files(buildscriptInputFiles)

description = "Check buildscript code style."
group = "ktlint"
classpath = ktlint
mainClass.set("com.pinterest.ktlint.Main")
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
args("--color", "*.gradle.kts")
}

val ktlintFormatBuildScript by registering(JavaExec::class) {
inputs.files(buildscriptInputFiles)

description = "Fix buildscript code style deviations."
group = "ktlint"
classpath = ktlint
mainClass.set("com.pinterest.ktlint.Main")
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
args("--color", "-F", "*.gradle.kts")
}

val ktlintCheckAll by registering {
dependsOn(ktlintCheckBuildScript, ktlintCheckSources)

description = "Check all files code style."
group = "ktlint"
}

register("ktlintFormatAll") {
dependsOn(ktlintFormatBuildScript, ktlintFormatSources)

description = "Fix all code style deviations."
group = "ktlint"
}

check {
dependsOn(ktlintCheckAll)
}

withType<Test> {
shouldRunAfter(ktlintCheckBuildScript, ktlintCheckSources)
}
}
1 change: 1 addition & 0 deletions extra-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
plugins {
`kotlin-dsl`
`kotlin-convention`
`linter-convention`
}

dependencies {
Expand Down
Loading

0 comments on commit f399c5a

Please sign in to comment.