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

KON-312 Add Konsist Test Which Check If All Snippets Are Used #159

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.lemonappdev.konsist.core

import com.lemonappdev.konsist.api.Konsist
import com.lemonappdev.konsist.core.filesystem.PathProvider
import com.lemonappdev.konsist.core.util.KotlinFileParser
import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Test
import java.io.File

class SnippetKonsistTest {
@Test
fun `every snippet is used in tests`() {
// given
val snippets = File("$rootProjectPath/lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/")
.walk()
.filter { it.isKotlinSnippetFile }
.map { it.name.removeSuffix(".kttxt") }
.toSet()

val r1 = Regex("""getSnippetFile\("(.+)"\)""")
val r2 = Regex("""arguments\("([^"]+)"""")
val withGetSnippetMethod = snippetNamesFromFiles(r1, "getSnippetFile(\"", "\")")
val withArgument = snippetNamesFromFiles(r2, "arguments(\"", "\"")
val snippetNamesUsedInTests = (withGetSnippetMethod + withArgument).toSet()

// then
val actual = snippets - snippetNamesUsedInTests
actual shouldBeEqualTo emptySet()
}

private fun snippetNamesFromFiles(regex: Regex, prefix: String, suffix: String) = snippetPackageScope
.files()
.map { it.text }
.flatMap { regex.findAll(it) }
.map { it.value }
.map { it.removePrefix(prefix) }
.map { it.removeSuffix(suffix) }

companion object {
private val snippetPackageScope = Konsist.scopeFromTest(sourceSetName = "integrationTest")
private val pathProvider: PathProvider by lazy { PathProvider.getInstance() }
private val rootProjectPath = pathProvider.rootProjectPath
private val File.isKotlinSnippetFile get() = isFile && name.endsWith(KotlinFileParser.KOTLIN_SNIPPET_FILE_EXTENSION)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ class PathProvider(
companion object {
private val pathVerifier = PathVerifier()

internal fun getInstance() = PathProvider(KoFileFactory(), ProjectRootDirProviderFactory(pathVerifier))
fun getInstance() = PathProvider(KoFileFactory(), ProjectRootDirProviderFactory(pathVerifier))
}
}