1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-25 07:57:12 -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

@ -28,7 +28,6 @@ class Plugins @Inject constructor (val taskManagerProvider : Provider<TaskManage
val depManager: DependencyManager,
val settings: KobaltSettings,
val executors: KobaltExecutors,
val pluginInfo: PluginInfo,
val incrementalManagerFactory: IncrementalManager.IFactory,
val taskManager: TaskManager) {
@ -171,6 +170,9 @@ class Plugins @Inject constructor (val taskManagerProvider : Provider<TaskManage
val dependencies = arrayListOf<IClasspathDependency>()
// @Inject
// lateinit var pluginInfo: PluginInfo
fun installPlugins(dependencies: List<IClasspathDependency>, scriptClassLoader: ClassLoader) {
val executor = executors.newExecutor("Plugins", 5)
dependencies.forEach {
@ -191,6 +193,8 @@ class Plugins @Inject constructor (val taskManagerProvider : Provider<TaskManage
// The plug-in is pointing to a jar file, read kobalt-plugin.xml from it
JarUtils.extractTextFile(JarFile(file), PluginInfo.PLUGIN_XML)
}
val pluginInfo = Kobalt.INJECTOR.getInstance(PluginInfo::class.java)
if (pluginXml != null) {
val pluginClassLoader = URLClassLoader(arrayOf(file.toURI().toURL()))
val thisPluginInfo = PluginInfo.readPluginXml(pluginXml, pluginClassLoader, scriptClassLoader)

View file

@ -17,7 +17,7 @@ import javax.inject.Singleton
@Singleton
class TaskManager @Inject constructor(val args: Args,
val incrementalManagerFactory: IncrementalManager.IFactory,
val pluginInfo: PluginInfo, val kobaltLog: ParallelLogger) {
val kobaltLog: ParallelLogger) {
private val dependsOn = TreeMultimap.create<String, String>()
private val reverseDependsOn = TreeMultimap.create<String, String>()
private val runBefore = TreeMultimap.create<String, String>()
@ -80,6 +80,9 @@ class TaskManager @Inject constructor(val args: Args,
}
}
// @Inject
// lateinit var pluginInfo: PluginInfo
fun runTargets(passedTaskNames: List<String>, allProjects: List<Project>): RunTargetResult {
// Check whether tasks passed at command line exist
passedTaskNames.forEach {
@ -87,6 +90,7 @@ class TaskManager @Inject constructor(val args: Args,
throw KobaltException("Unknown task: $it")
}
val pluginInfo = Kobalt.INJECTOR.getInstance(PluginInfo::class.java)
var taskInfos = calculateDependentTaskNames(passedTaskNames, allProjects)
// Remove non existing tasks (e.g. dynamic task defined for a single project)

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)