Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/pr_test.yml → .github/workflows/pr_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Run Tests on Pull Request

on:
push:
branches: [ "trunk" ]
branches: [ "main" ]
pull_request:
branches: [ "trunk" ]
branches: [ "main" ]

workflow_dispatch:

Expand Down
32 changes: 23 additions & 9 deletions src/main/kotlin/Compilation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ internal fun compilationForAssertations(
assertAction: SymbolProcessorEnvironment.(Resolver) -> Unit,
): List<File> {
assert(sources.any { it.isNotBlank() })
return compile(sources.toSomeClasses(), assertAction)
}

@OptIn(ExperimentalCompilerApi::class)
private fun compile(
sourceFiles: List<SourceFile>,
assertAction: SymbolProcessorEnvironment.(Resolver) -> Unit,
): List<File> {
assert(sourceFiles.isNotEmpty())
val testKspProcessorProvider = TestKspProcessor.provider(assertAction)
return compilation(
sourceFiles = sources.toSomeClasses(),
val result = compilation(
sourceFiles = sourceFiles,
processorProvider = testKspProcessorProvider,
)
.compile()
.also {
if (KotlinCompilation.ExitCode.OK == it.exitCode) return emptyList()
error(it.messages)
}
.generatedFiles.toList()
).compile()

if (result.exitCode == KotlinCompilation.ExitCode.OK) {
return result.generatedFiles.toList()
}

error(result.messages)
}

internal fun List<String>.toSomeClasses(): List<SourceFile> = mapIndexed { index, name ->
Expand All @@ -32,6 +41,11 @@ internal fun List<String>.toSomeClasses(): List<SourceFile> = mapIndexed { index
)
}

internal fun compilationForAssertations(
source: File,
assertAction: SymbolProcessorEnvironment.(Resolver) -> Unit,
): List<File> = compile(listOf(SourceFile.fromPath(source)), assertAction)

private fun String.toSomeClass(index: Int) = SourceFile.kotlin("SomeClass$index.kt", this)

@OptIn(ExperimentalCompilerApi::class)
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/dsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ infix fun String.compile(action: SymbolProcessorEnvironment.(Resolver) -> Unit):
infix fun List<String>.compile(action: SymbolProcessorEnvironment.(Resolver) -> Unit): List<File> {
return compilationForAssertations(this, action)
}

infix fun File.compile(action: SymbolProcessorEnvironment.(Resolver) -> Unit): List<File> {
if (exists()) {
return compilationForAssertations(this, action)
}
throw IllegalArgumentException("File $this does not exist")
}
17 changes: 16 additions & 1 deletion src/test/kotlin/CompilationForAssertationTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import org.junit.jupiter.api.assertThrows
import java.io.File
import kotlin.test.Test
import kotlin.test.assertTrue

Expand Down Expand Up @@ -29,7 +30,7 @@ class CompilationForAssertationTest {
}

@Test
fun `blank sourse should trow error`() {
fun `blank source should trow error`() {
assertThrows<AssertionError> {
"" compile {
assertTrue { true }
Expand All @@ -48,4 +49,18 @@ class CompilationForAssertationTest {
incorrectCode compile {}
}
}

@Test
fun `correct file should compile`() {
File("src/test/kotlin/FilesForTests/CorrectFile.kt") compile {
assertTrue { true }
}
}

@Test
fun `wrong file should not compile`() {
assertThrows<IllegalArgumentException> {
File("") compile {}
}
}
}
7 changes: 7 additions & 0 deletions src/test/kotlin/FilesForTests/CorrectFile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package FilesForTests

class CorrectFile {
fun helloWorld(): String {
return "Hello World!"
}
}