Skip to content

Commit d6c26ae

Browse files
KON-583 Add possibility to create scope passing sets of items (#861)
* fix spotless and detekt * dix scopeFromFiles method * remove test from architecture * upd tests for `scopeFromFiles` * add tests for `scopeFromDirectories` * add tests for `scopeFromModules` * add tests for `scopeFromSourceSets` * upd kdocs
1 parent 0e8d719 commit d6c26ae

File tree

20 files changed

+400
-185
lines changed

20 files changed

+400
-185
lines changed

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/Architecture8Test.kt

-110
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/README.md

-25
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/project1/domain/DomainFirstClass.kt

-3
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/project1/domain/sample/DomainSecondClass.kt

-7
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/project1/presentation/PresentationFirstClass.kt

-3
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/project1/presentation/sample/PresentationSecondClass.kt

-7
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/project2/domain/DomainFirstClass.kt

-3
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/project2/domain/sample/DomainSecondClass.kt

-7
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/project2/presentation/PresentationFirstClass.kt

-3
This file was deleted.

lib/src/apiTest/kotlin/com/lemonappdev/konsist/architecture/assertarchitecture/architecture8/project2/presentation/sample/PresentationSecondClass.kt

-7
This file was deleted.

lib/src/main/kotlin/com/lemonappdev/konsist/api/container/KoScopeCreator.kt

+19
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ interface KoScopeCreator {
3333
*/
3434
fun scopeFromModule(moduleName: String, vararg moduleNames: String): KoScope
3535

36+
/**
37+
* Creates a [KoScope] containing all of Kotlin files in the module.
38+
* Method does return Kotlin files present in build directories such as "build" and "target".
39+
*
40+
* @param moduleNames Set of the module names.
41+
* @return a [KoScope] containing all of Kotlin files in the module.
42+
*/
43+
fun scopeFromModules(moduleNames: Set<String>): KoScope
44+
3645
/**
3746
* Creates a [KoScope] containing all of Kotlin files in the given package.
3847
* Method does return Kotlin files present in build directories such as "build" and "target".
@@ -55,6 +64,16 @@ interface KoScopeCreator {
5564
*/
5665
fun scopeFromSourceSet(sourceSetName: String, vararg sourceSetNames: String): KoScope
5766

67+
/**
68+
* Creates a [KoScope] containing all of Kotlin files in source set. If the source set is present in multiple modules
69+
* then all of them will be included.
70+
* Method does return Kotlin files present in build directories such as "build" and "target".
71+
*
72+
* @param sourceSetNames Set of the source set names.
73+
* @return a [KoScope] containing all of Kotlin files in source set.
74+
*/
75+
fun scopeFromSourceSets(sourceSetNames: Set<String>): KoScope
76+
5877
/**
5978
* Creates a [KoScope] containing all of Kotlin files in the production source sets.
6079
* The production source set is the source set which name does not start and ends with "test".

lib/src/main/kotlin/com/lemonappdev/konsist/core/container/KoScopeCreatorCore.kt

+22-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ internal class KoScopeCreatorCore : KoScopeCreator {
2525
}
2626

2727
override fun scopeFromModule(moduleName: String, vararg moduleNames: String): KoScope =
28-
(listOf(moduleName) + moduleNames)
28+
scopeFromModules(setOf(moduleName) + moduleNames)
29+
30+
override fun scopeFromModules(moduleNames: Set<String>): KoScope =
31+
moduleNames
2932
.flatMap { getFiles(it) }
3033
.let { KoScopeCore(it) }
3134

@@ -37,7 +40,10 @@ internal class KoScopeCreatorCore : KoScopeCreator {
3740
}
3841

3942
override fun scopeFromSourceSet(sourceSetName: String, vararg sourceSetNames: String): KoScope =
40-
(listOf(sourceSetName) + sourceSetNames)
43+
scopeFromSourceSets(setOf(sourceSetName) + sourceSetNames)
44+
45+
override fun scopeFromSourceSets(sourceSetNames: Set<String>): KoScope =
46+
sourceSetNames
4147
.flatMap { getFiles(sourceSetName = it) }
4248
.let { KoScopeCore(it) }
4349

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

151157
override fun scopeFromFiles(paths: Set<String>): KoScope {
152-
val koFiles = paths
158+
val files = paths
153159
.map { getAbsolutePath(it) }
154160
.map { File(it) }
155161
.onEach {
156162
require(it.exists()) { "File does not exist: ${it.absolutePath}" }
157163
require(it.isFile) { "Path is a directory, but should be a file: ${it.absolutePath}" }
158164
}
165+
166+
val notKotlinFiles = files
167+
.filterNot { it.isKotlinFile }
159168
.map { it.toKoFile() }
160169

161-
return KoScopeCore(koFiles)
170+
val koFiles = getKoFiles(files)
171+
172+
return KoScopeCore(koFiles + notKotlinFiles)
162173
}
163174

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

239+
private fun getKoFiles(files: List<File>) = projectKotlinFiles.filter {
240+
files.any { file ->
241+
file.path == it.path
242+
}
243+
}
244+
228245
companion object {
229246
private const val TEST_NAME_IN_PATH = "test"
230247
private const val ROOT_MODULE_NAME = "root"
231248
}
232-
}
249+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.lemonappdev.konsist.container.from
2+
3+
import com.lemonappdev.konsist.api.Konsist
4+
import com.lemonappdev.konsist.helper.ext.fileSeparator
5+
import com.lemonappdev.konsist.helper.ext.mapToFilePaths
6+
import com.lemonappdev.konsist.helper.ext.toOsSeparator
7+
import com.lemonappdev.konsist.helper.util.PathProvider.appIntegrationTestSourceSetDirectory
8+
import com.lemonappdev.konsist.helper.util.PathProvider.appMainSourceSetDirectory
9+
import org.amshove.kluent.shouldBeEqualTo
10+
import org.amshove.kluent.shouldThrow
11+
import org.amshove.kluent.withMessage
12+
import org.junit.jupiter.api.Test
13+
14+
class KoScopeFromDirectoriesTest {
15+
16+
@Test
17+
fun `scopeFromDirectories`() {
18+
// given
19+
val paths = setOf(
20+
"app/src/main/kotlin/com/lemonappdev/sample/".toOsSeparator(),
21+
"app/src/integrationTest/kotlin/com/lemonappdev/sample/".toOsSeparator(),
22+
)
23+
val sut = Konsist
24+
.scopeFromDirectories(paths)
25+
.mapToFilePaths()
26+
27+
// then
28+
sut.shouldBeEqualTo(
29+
listOf(
30+
"$appIntegrationTestSourceSetDirectory/sample/AppClassTest.kt",
31+
"$appIntegrationTestSourceSetDirectory/sample/data/AppDataClassTest.kt",
32+
"$appMainSourceSetDirectory/sample/AppClass.kt",
33+
"$appMainSourceSetDirectory/sample/data/AppDataClass.kt",
34+
).toOsSeparator(),
35+
)
36+
}
37+
38+
@Test
39+
fun `scopeFromDirectories throws exception if path does not exist`() {
40+
// given
41+
val paths = setOf("app/src/main/kotlin/com/lemonappdev/nonExisting/".toOsSeparator())
42+
43+
val func =
44+
{ Konsist.scopeFromDirectories(paths) }
45+
46+
// then
47+
val message = "Directory does not exist: $appMainSourceSetDirectory${fileSeparator}nonExisting$fileSeparator"
48+
func shouldThrow IllegalArgumentException::class withMessage message
49+
}
50+
51+
@Test
52+
fun `scopeFromDirectories throws exception if path points to file`() {
53+
// given
54+
val paths = setOf("app/src/main/kotlin/com/lemonappdev/sample/AppClass.kt".toOsSeparator())
55+
56+
val func =
57+
{
58+
Konsist.scopeFromDirectories(paths)
59+
}
60+
61+
// then
62+
val message =
63+
"Path is a file, but should be a directory: $appMainSourceSetDirectory${fileSeparator}sample${fileSeparator}AppClass.kt"
64+
func shouldThrow IllegalArgumentException::class withMessage message
65+
}
66+
}

test-projects/konsist-path-tester/app/src/integrationTest/kotlin/com/lemonappdev/konsist/container/from/KoScopeFromFilesTest.kt

+13-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test
1313
class KoScopeFromFilesTest {
1414

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

3434
@Test
35-
fun `scopeFromFile throws exception if path does not exist`() {
35+
fun `scopeFromFiles throws exception if path does not exist`() {
3636
// given
37+
val files = setOf(
38+
"app/src/main/kotlin/com/lemonappdev/NonExistingTest.kt".toOsSeparator()
39+
)
40+
3741
val func =
38-
{ Konsist.scopeFromFile("app/src/main/kotlin/com/lemonappdev/NonExistingTest.kt".toOsSeparator()) }
42+
{ Konsist.scopeFromFiles(files) }
3943

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

4549
@Test
46-
fun `scopeFromFile throws exception if path points to directory`() {
50+
fun `scopeFromFiles throws exception if path points to directory`() {
4751
// given
48-
val func = { Konsist.scopeFromFile("app/src/main/kotlin/com/lemonappdev/sample".toOsSeparator()) }
52+
val files = setOf(
53+
"app/src/main/kotlin/com/lemonappdev/sample".toOsSeparator()
54+
)
55+
56+
val func = { Konsist.scopeFromFiles(files) }
4957

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

0 commit comments

Comments
 (0)