1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Tests can now run full builds.

This commit is contained in:
Cedric Beust 2017-04-06 11:31:05 -07:00
parent 24d902bfbf
commit ba98592f49
6 changed files with 75 additions and 28 deletions

View file

@ -1,5 +1,6 @@
package com.beust.kobalt
import com.beust.jcommander.JCommander
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.Project
import com.beust.kobalt.app.BuildFileCompiler
@ -10,11 +11,13 @@ import com.beust.kobalt.internal.build.SingleFileBuildSources
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.log
import org.testng.annotations.BeforeClass
import org.testng.annotations.Guice
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*
@Guice(modules = arrayOf(TestModule::class))
open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) {
@BeforeClass
fun bc() {
@ -90,8 +93,12 @@ open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) {
fun createTemporaryProjectDirectory() = KFiles.fixSlashes(Files.createTempDirectory("kobaltTest").toFile())
fun createProject(projectInfo: ProjectInfo) : File {
class ProjectDescription(val path: File, val name: String, val version: String)
fun createProject(projectInfo: ProjectInfo) : ProjectDescription {
val root = Files.createTempDirectory("kobalt-test").toFile()
val projectName = "p" + Math.abs(Random().nextInt())
val version = "1.0"
fun createFile(root: File, f: String, text: String) : File {
val file = File(root, f)
@ -100,21 +107,38 @@ open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) {
return file
}
createFile(root, "kobalt/src/Build.kt", projectInfo.buildFile.text(root.absolutePath))
createFile(root, "kobalt/src/Build.kt",
projectInfo.buildFile.text(root.absolutePath, projectName, version))
projectInfo.files.forEach {
createFile(root, it.path, it.content)
}
return root
return ProjectDescription(root, projectName, version)
}
class LaunchProjectResult(val projectInfo: ProjectInfo, val projectDescription: ProjectDescription,
val result: Int)
fun launchProject(projectInfo: ProjectInfo, commandLine: Array<String>) : LaunchProjectResult {
val project = createProject(projectInfo)
println("Project: $project")
val main = Kobalt.INJECTOR.getInstance(Main::class.java)
val args = Args()
val jc = JCommander(args).apply { parse(*commandLine) }
args.buildFile = project.path.absolutePath + "/kobalt/src/Build.kt"
val result = Main.launchMain(main, jc, args, arrayOf("assemble"))
return LaunchProjectResult(projectInfo, project, result)
}
}
class BuildFile(val imports: List<String>, val projectText: String) {
fun text(projectDirectory: String) : String {
val projectName = "p" + Math.abs(Random().nextInt())
fun text(projectDirectory: String, projectName: String, version: String) : String {
val bottom = """
val $projectName = project {
name = "$projectName"
version = "$version"
directory = "$projectDirectory"
$projectText
}

View file

@ -0,0 +1,28 @@
package com.beust.kobalt.internal
import com.beust.kobalt.BaseTest
import com.beust.kobalt.BuildFile
import com.beust.kobalt.ProjectFile
import com.beust.kobalt.ProjectInfo
import com.beust.kobalt.misc.KFiles
import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test
import java.io.File
class BuildFilesTest : BaseTest() {
@Test
fun shouldGenerateArtifact() {
val projectInfo = ProjectInfo(
BuildFile(listOf("com.beust.kobalt.plugin.packaging.*"), "assemble{jar{}}"),
listOf(ProjectFile("src/main/kotlin/A.kt", "val a = \"foo\"")))
val result = launchProject(projectInfo, arrayOf("assemble"))
val project = result.projectDescription
val jarFile = File(KFiles.joinDir(project.path.absolutePath, "kobaltBuild/libs", project.name + "-"
+ project.version + ".jar"))
assertThat(jarFile).exists()
}
}

View file

@ -1,23 +1,15 @@
package com.beust.kobalt.internal
import com.beust.kobalt.TestModule
import com.beust.kobalt.BaseTest
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.project
import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.BeforeClass
import org.testng.annotations.DataProvider
import org.testng.annotations.Guice
import org.testng.annotations.Test
@Guice(modules = arrayOf(TestModule::class))
class BuildOrderTest {
class BuildOrderTest : BaseTest() {
val taskManager: TaskManager get() = Kobalt.INJECTOR.getInstance(TaskManager::class.java)
@BeforeClass
fun beforeClass() {
Kobalt.init(TestModule())
}
private fun toExpectedList(vararg projectNames: Int) = projectNames.map { "p$it:assemble" }.toList()
@DataProvider

View file

@ -1,13 +1,14 @@
package com.beust.kobalt.internal
import com.beust.kobalt.BaseTest
import com.beust.kobalt.TestConfig
import com.beust.kobalt.TestModule
import com.beust.kobalt.api.*
import com.beust.kobalt.api.ITestJvmFlagContributor
import com.beust.kobalt.api.ITestJvmFlagInterceptor
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.project
import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.BeforeClass
import org.testng.annotations.Guice
import org.testng.annotations.Test
import javax.inject.Inject
@ -15,8 +16,7 @@ import javax.inject.Inject
/**
* Test ITestJvmFlagContributor and ITestJvmFlagInterceptor.
*/
@Guice(modules = arrayOf(TestModule::class))
class DependencyTest @Inject constructor(val context: KobaltContext) {
class DependencyTest @Inject constructor(val context: KobaltContext) : BaseTest() {
private fun isWindows() = System.getProperty("os.name").toLowerCase().contains("ndows")
private val A_JAR = if (isWindows()) "c:\\tmp\\a.jar" else "/tmp/a.jar"
private val B_JAR = if (isWindows()) "c:\\tmp\\b.jar" else "/tmp/b.jar"
@ -37,11 +37,6 @@ class DependencyTest @Inject constructor(val context: KobaltContext) {
}
}
@BeforeClass
fun beforeClass() {
Kobalt.init(TestModule())
}
private fun runTest(pluginInfo: IPluginInfo, expected: List<String>) {
val result = TestNgRunner().calculateAllJvmArgs(project, context, TestConfig(project),
classpath, pluginInfo)