Skip to content

Commit

Permalink
test: add test for a custom configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tassiluca committed Jul 3, 2024
1 parent cb2836f commit b2a2f45
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/io/github/ltassi/scalaqa/ScalaQAPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.ltassi.scalaqa

import org.gradle.api.Plugin
import org.gradle.api.Project
import com.diffplug.gradle.spotless.SpotlessExtension
import com.diffplug.gradle.spotless.SpotlessPlugin
import io.github.cosmicsilence.scalafix.ScalafixPlugin
import io.github.ltassi.scalaqa.configutations.ScalaFmtConfiguration
import org.gradle.api.Plugin
import org.gradle.api.Project

/** The scala QA plugin entry point. */
class ScalaQAPlugin : Plugin<Project> {
Expand All @@ -20,7 +20,7 @@ class ScalaQAPlugin : Plugin<Project> {
}

private fun Project.configureScalaFmt(configuration: ScalaFmtConfiguration) {
logger.info("Pickup scalafmt configuration from ${configuration.configFile.get()}")
logger.info("Picking up scalafmt configuration from ${configuration.configFile.get()}")
logger.info("Using scalafmt version ${configuration.version}")
configureExtension<SpotlessExtension> {
isEnforceCheck = true
Expand Down
12 changes: 9 additions & 3 deletions src/main/kotlin/io/github/ltassi/scalaqa/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package io.github.ltassi.scalaqa

import org.gradle.api.Project
import java.io.File

internal fun resource(path: String) = checkNotNull(Thread.currentThread().contextClassLoader.getResource(path)) {
"Unable to access resource $path"
}
internal fun resource(path: String): File = File(
checkNotNull(Thread.currentThread().contextClassLoader.getResource(path)) {
"Unable to access resource $path"
}.toURI(),
)

internal fun File.contains(filename: String): Boolean {
require(exists() && isDirectory) { "File $this does not exist or is not a directory" }
return listFiles()?.any { it.name == filename } ?: false
}

internal fun Project.resolveOrFromResource(filename: String): File =
if (rootDir.contains(filename)) rootDir.resolve(filename) else resource(filename)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.ltassi.scalaqa.configutations

import io.github.ltassi.scalaqa.contains
import io.github.ltassi.scalaqa.resource
import io.github.ltassi.scalaqa.resolveOrFromResource
import org.gradle.api.Project
import org.gradle.api.provider.Property
import java.io.File
Expand All @@ -10,15 +9,7 @@ import java.io.File
class ScalaFmtConfiguration(project: Project) {
/** The stringified path to the scalafmt configuration file. */
val configFile: Property<String> = project.objects.property(String::class.java)
.apply {
convention(
if (project.rootDir.contains(DEFAULT_SCALAFMT_CONFIG_FILE)) {
DEFAULT_SCALAFMT_CONFIG_FILE
} else {
resource(DEFAULT_SCALAFMT_CONFIG_FILE).path
}
)
}
.apply { convention(project.resolveOrFromResource(DEFAULT_SCALAFMT_CONFIG_FILE).path) }

/** Computes the version of scalafmt from [configFile]. */
internal val version = File(configFile.get())
Expand All @@ -27,6 +18,7 @@ class ScalaFmtConfiguration(project: Project) {
?: error("Missing required 'version' parameter in scalafmt configuration")

companion object {
/** The default scalafmt configuration file name. */
const val DEFAULT_SCALAFMT_CONFIG_FILE = ".scalafmt.conf"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ class ScalaQAPluginTest : FreeSpec({
"with formatting and linting issues" {
Testkit.projectTest("failing")
}

"with custom configuration" {
Testkit.projectTest("custom")
}
}
}) {
companion object {
private const val PROJECT_NAME = "gradle-scala-qa"

private fun Testkit.projectTest(folder: String) =
test(PROJECT_NAME, DEFAULT_TEST_FOLDER + folder, forwardOutput = false)
test(PROJECT_NAME, DEFAULT_TEST_FOLDER + folder, forwardOutput = true)
}
}
2 changes: 2 additions & 0 deletions src/test/resources/custom/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
preset = default
version = 3.0.0
12 changes: 12 additions & 0 deletions src/test/resources/custom/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
scala
id("io.github.ltassi.gradle-scala-qa")
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.scala-lang:scala3-library_3:3.5.1-RC1-bin-20240625-b3f113e-NIGHTLY")
}
17 changes: 17 additions & 0 deletions src/test/resources/custom/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests:
- description: "With custom configuration"
configuration:
tasks:
- clean
- check
options:
- --stacktrace
- --info
expectation:
result: success
outcomes:
success:
- spotlessScalaCheck
output:
contains:
- "Using scalafmt version 3.0.0"

0 comments on commit b2a2f45

Please sign in to comment.