Skip to content

Commit

Permalink
test: Added functional test and logging for PrintTask file output
Browse files Browse the repository at this point in the history
  • Loading branch information
jmongard committed Jun 11, 2024
1 parent 61cc71d commit bb75e63
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ class GitSemverPluginFunctionalTest {
assertThat(result.output).containsPattern("added test files")
}

@Test
fun `can run printVersion to file task`() {
val projectDir = setupTestProject()

val result = run(projectDir, null, "-i", "printVersion", "--file", "v.txt")

assertThat(result.output).containsPattern("v.txt")
}

private fun setupTestProject(): File {
val semverSettings = """
semver {
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/git/semver/plugin/gradle/PrintTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git.semver.plugin.gradle
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.slf4j.LoggerFactory
import java.nio.charset.StandardCharsets
import java.nio.file.Paths
import javax.inject.Inject
Expand All @@ -11,6 +12,7 @@ import kotlin.io.path.writeText

open class PrintTask @Inject constructor(private val printout: () -> Any, desc: String) : DefaultTask() {
private var file:String? = null
private val logger = LoggerFactory.getLogger(PrintTask::class.java)

init {
group = GitSemverPlugin.VERSIONING_GROUP
Expand All @@ -36,6 +38,7 @@ open class PrintTask @Inject constructor(private val printout: () -> Any, desc:
private fun writeFile(fileName: String) {
val path = Paths.get(fileName).toAbsolutePath()
path.parent.createDirectories()
logger.info("Writing to $path")
path.writeText(printout().toString(), StandardCharsets.UTF_8)
}
}

0 comments on commit bb75e63

Please sign in to comment.