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-583 Add possibility to create scope passing sets of items #861

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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
Expand Up @@ -33,6 +33,15 @@ interface KoScopeCreator {
*/
fun scopeFromModule(moduleName: String, vararg moduleNames: String): KoScope

/**
* Creates a [KoScope] containing all of Kotlin files in the module.
* Method does return Kotlin files present in build directories such as "build" and "target".
*
* @param moduleNames Set of the module names.
* @return a [KoScope] containing all of Kotlin files in the module.
*/
fun scopeFromModules(moduleNames: Set<String>): KoScope

/**
* Creates a [KoScope] containing all of Kotlin files in the given package.
* Method does return Kotlin files present in build directories such as "build" and "target".
Expand All @@ -55,6 +64,16 @@ interface KoScopeCreator {
*/
fun scopeFromSourceSet(sourceSetName: String, vararg sourceSetNames: String): KoScope

/**
* Creates a [KoScope] containing all of Kotlin files in source set. If the source set is present in multiple modules
* then all of them will be included.
* Method does return Kotlin files present in build directories such as "build" and "target".
*
* @param sourceSetNames Set of the source set names.
* @return a [KoScope] containing all of Kotlin files in source set.
*/
fun scopeFromSourceSets(sourceSetNames: Set<String>): KoScope

/**
* Creates a [KoScope] containing all of Kotlin files in the production source sets.
* The production source set is the source set which name does not start and ends with "test".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ internal class KoScopeCreatorCore : KoScopeCreator {
}

override fun scopeFromModule(moduleName: String, vararg moduleNames: String): KoScope =
(listOf(moduleName) + moduleNames)
scopeFromModules(setOf(moduleName) + moduleNames)

override fun scopeFromModules(moduleNames: Set<String>): KoScope =
moduleNames
.flatMap { getFiles(it) }
.let { KoScopeCore(it) }

Expand All @@ -37,7 +40,10 @@ internal class KoScopeCreatorCore : KoScopeCreator {
}

override fun scopeFromSourceSet(sourceSetName: String, vararg sourceSetNames: String): KoScope =
(listOf(sourceSetName) + sourceSetNames)
scopeFromSourceSets(setOf(sourceSetName) + sourceSetNames)

override fun scopeFromSourceSets(sourceSetNames: Set<String>): KoScope =
sourceSetNames
.flatMap { getFiles(sourceSetName = it) }
.let { KoScopeCore(it) }

Expand Down Expand Up @@ -149,16 +155,21 @@ internal class KoScopeCreatorCore : KoScopeCreator {
override fun scopeFromFile(path: String, vararg paths: String): KoScope = scopeFromFiles(setOf(path) + paths)

override fun scopeFromFiles(paths: Set<String>): KoScope {
val koFiles = paths
val files = paths
.map { getAbsolutePath(it) }
.map { File(it) }
.onEach {
require(it.exists()) { "File does not exist: ${it.absolutePath}" }
require(it.isFile) { "Path is a directory, but should be a file: ${it.absolutePath}" }
}

val notKotlinFiles = files
.filterNot { it.isKotlinFile }
.map { it.toKoFile() }

return KoScopeCore(koFiles)
val koFiles = getKoFiles(files)

return KoScopeCore(koFiles + notKotlinFiles)
}

private fun getAbsolutePath(projectPath: String): String = "$projectRootPath$sep$projectPath"
Expand Down Expand Up @@ -225,8 +236,14 @@ internal class KoScopeCreatorCore : KoScopeCreator {
.map { it.toKoFile() }
.toList()

private fun getKoFiles(files: List<File>) = projectKotlinFiles.filter {
files.any { file ->
file.path == it.path
}
}

companion object {
private const val TEST_NAME_IN_PATH = "test"
private const val ROOT_MODULE_NAME = "root"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.lemonappdev.konsist.container.from

import com.lemonappdev.konsist.api.Konsist
import com.lemonappdev.konsist.helper.ext.fileSeparator
import com.lemonappdev.konsist.helper.ext.mapToFilePaths
import com.lemonappdev.konsist.helper.ext.toOsSeparator
import com.lemonappdev.konsist.helper.util.PathProvider.appIntegrationTestSourceSetDirectory
import com.lemonappdev.konsist.helper.util.PathProvider.appMainSourceSetDirectory
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldThrow
import org.amshove.kluent.withMessage
import org.junit.jupiter.api.Test

class KoScopeFromDirectoriesTest {

@Test
fun `scopeFromDirectories`() {
// given
val paths = setOf(
"app/src/main/kotlin/com/lemonappdev/sample/".toOsSeparator(),
"app/src/integrationTest/kotlin/com/lemonappdev/sample/".toOsSeparator(),
)
val sut = Konsist
.scopeFromDirectories(paths)
.mapToFilePaths()

// then
sut.shouldBeEqualTo(
listOf(
"$appIntegrationTestSourceSetDirectory/sample/AppClassTest.kt",
"$appIntegrationTestSourceSetDirectory/sample/data/AppDataClassTest.kt",
"$appMainSourceSetDirectory/sample/AppClass.kt",
"$appMainSourceSetDirectory/sample/data/AppDataClass.kt",
).toOsSeparator(),
)
}

@Test
fun `scopeFromDirectories throws exception if path does not exist`() {
// given
val paths = setOf("app/src/main/kotlin/com/lemonappdev/nonExisting/".toOsSeparator())

val func =
{ Konsist.scopeFromDirectories(paths) }

// then
val message = "Directory does not exist: $appMainSourceSetDirectory${fileSeparator}nonExisting$fileSeparator"
func shouldThrow IllegalArgumentException::class withMessage message
}

@Test
fun `scopeFromDirectories throws exception if path points to file`() {
// given
val paths = setOf("app/src/main/kotlin/com/lemonappdev/sample/AppClass.kt".toOsSeparator())

val func =
{
Konsist.scopeFromDirectories(paths)
}

// then
val message =
"Path is a file, but should be a directory: $appMainSourceSetDirectory${fileSeparator}sample${fileSeparator}AppClass.kt"
func shouldThrow IllegalArgumentException::class withMessage message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test
class KoScopeFromFilesTest {

@Test
fun `scopeFromFile`() {
fun `scopeFromFiles`() {
// given
val files = setOf(
"/app/src/main/kotlin/com/lemonappdev/sample/AppClass.kt".toOsSeparator(),
Expand All @@ -32,20 +32,28 @@ class KoScopeFromFilesTest {
}

@Test
fun `scopeFromFile throws exception if path does not exist`() {
fun `scopeFromFiles throws exception if path does not exist`() {
// given
val files = setOf(
"app/src/main/kotlin/com/lemonappdev/NonExistingTest.kt".toOsSeparator()
)

val func =
{ Konsist.scopeFromFile("app/src/main/kotlin/com/lemonappdev/NonExistingTest.kt".toOsSeparator()) }
{ Konsist.scopeFromFiles(files) }

// then
val message = "File does not exist: $appMainSourceSetDirectory${fileSeparator}NonExistingTest.kt"
func shouldThrow IllegalArgumentException::class withMessage message
}

@Test
fun `scopeFromFile throws exception if path points to directory`() {
fun `scopeFromFiles throws exception if path points to directory`() {
// given
val func = { Konsist.scopeFromFile("app/src/main/kotlin/com/lemonappdev/sample".toOsSeparator()) }
val files = setOf(
"app/src/main/kotlin/com/lemonappdev/sample".toOsSeparator()
)

val func = { Konsist.scopeFromFiles(files) }

// then
val message = "Path is a directory, but should be a file: $appMainSourceSetDirectory${fileSeparator}sample"
Expand Down
Loading