diff --git a/.github/pr_test.yml b/.github/workflows/pr_test.yml similarity index 93% rename from .github/pr_test.yml rename to .github/workflows/pr_test.yml index 84f9b00..458a129 100644 --- a/.github/pr_test.yml +++ b/.github/workflows/pr_test.yml @@ -2,9 +2,9 @@ name: Run Tests on Pull Request on: push: - branches: [ "trunk" ] + branches: [ "main" ] pull_request: - branches: [ "trunk" ] + branches: [ "main" ] workflow_dispatch: diff --git a/src/main/kotlin/Compilation.kt b/src/main/kotlin/Compilation.kt index 36ccb7e..1f01cab 100644 --- a/src/main/kotlin/Compilation.kt +++ b/src/main/kotlin/Compilation.kt @@ -13,17 +13,26 @@ internal fun compilationForAssertations( assertAction: SymbolProcessorEnvironment.(Resolver) -> Unit, ): List { assert(sources.any { it.isNotBlank() }) + return compile(sources.toSomeClasses(), assertAction) +} + +@OptIn(ExperimentalCompilerApi::class) +private fun compile( + sourceFiles: List, + assertAction: SymbolProcessorEnvironment.(Resolver) -> Unit, +): List { + 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.toSomeClasses(): List = mapIndexed { index, name -> @@ -32,6 +41,11 @@ internal fun List.toSomeClasses(): List = mapIndexed { index ) } +internal fun compilationForAssertations( + source: File, + assertAction: SymbolProcessorEnvironment.(Resolver) -> Unit, +): List = compile(listOf(SourceFile.fromPath(source)), assertAction) + private fun String.toSomeClass(index: Int) = SourceFile.kotlin("SomeClass$index.kt", this) @OptIn(ExperimentalCompilerApi::class) diff --git a/src/main/kotlin/dsl.kt b/src/main/kotlin/dsl.kt index 3112c67..db1672a 100644 --- a/src/main/kotlin/dsl.kt +++ b/src/main/kotlin/dsl.kt @@ -10,3 +10,10 @@ infix fun String.compile(action: SymbolProcessorEnvironment.(Resolver) -> Unit): infix fun List.compile(action: SymbolProcessorEnvironment.(Resolver) -> Unit): List { return compilationForAssertations(this, action) } + +infix fun File.compile(action: SymbolProcessorEnvironment.(Resolver) -> Unit): List { + if (exists()) { + return compilationForAssertations(this, action) + } + throw IllegalArgumentException("File $this does not exist") +} diff --git a/src/test/kotlin/CompilationForAssertationTest.kt b/src/test/kotlin/CompilationForAssertationTest.kt index ddbc553..bf96b4b 100644 --- a/src/test/kotlin/CompilationForAssertationTest.kt +++ b/src/test/kotlin/CompilationForAssertationTest.kt @@ -1,4 +1,5 @@ import org.junit.jupiter.api.assertThrows +import java.io.File import kotlin.test.Test import kotlin.test.assertTrue @@ -29,7 +30,7 @@ class CompilationForAssertationTest { } @Test - fun `blank sourse should trow error`() { + fun `blank source should trow error`() { assertThrows { "" compile { assertTrue { true } @@ -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 { + File("") compile {} + } + } } diff --git a/src/test/kotlin/FilesForTests/CorrectFile.kt b/src/test/kotlin/FilesForTests/CorrectFile.kt new file mode 100644 index 0000000..c39949c --- /dev/null +++ b/src/test/kotlin/FilesForTests/CorrectFile.kt @@ -0,0 +1,7 @@ +package FilesForTests + +class CorrectFile { + fun helloWorld(): String { + return "Hello World!" + } +}