From ba98592f4902a20485fa6a18db52601cf8d4f23a Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Thu, 6 Apr 2017 11:31:05 -0700 Subject: [PATCH] Tests can now run full builds. --- .../main/kotlin/com/beust/kobalt/Plugins.kt | 6 +++- .../com/beust/kobalt/internal/TaskManager.kt | 6 +++- src/test/kotlin/com/beust/kobalt/BaseTest.kt | 34 ++++++++++++++++--- .../beust/kobalt/internal/BuildFilesTest.kt | 28 +++++++++++++++ .../beust/kobalt/internal/BuildOrderTest.kt | 12 ++----- .../beust/kobalt/internal/DependencyTest.kt | 17 ++++------ 6 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 src/test/kotlin/com/beust/kobalt/internal/BuildFilesTest.kt diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Plugins.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Plugins.kt index 914ddbaa..0102dd8b 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Plugins.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Plugins.kt @@ -28,7 +28,6 @@ class Plugins @Inject constructor (val taskManagerProvider : Provider() +// @Inject +// lateinit var pluginInfo: PluginInfo + fun installPlugins(dependencies: List, scriptClassLoader: ClassLoader) { val executor = executors.newExecutor("Plugins", 5) dependencies.forEach { @@ -191,6 +193,8 @@ class Plugins @Inject constructor (val taskManagerProvider : Provider() private val reverseDependsOn = TreeMultimap.create() private val runBefore = TreeMultimap.create() @@ -80,6 +80,9 @@ class TaskManager @Inject constructor(val args: Args, } } +// @Inject +// lateinit var pluginInfo: PluginInfo + fun runTargets(passedTaskNames: List, allProjects: List): 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) diff --git a/src/test/kotlin/com/beust/kobalt/BaseTest.kt b/src/test/kotlin/com/beust/kobalt/BaseTest.kt index f5e97df2..66516465 100644 --- a/src/test/kotlin/com/beust/kobalt/BaseTest.kt +++ b/src/test/kotlin/com/beust/kobalt/BaseTest.kt @@ -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) : 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, 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 } diff --git a/src/test/kotlin/com/beust/kobalt/internal/BuildFilesTest.kt b/src/test/kotlin/com/beust/kobalt/internal/BuildFilesTest.kt new file mode 100644 index 00000000..8c783ef9 --- /dev/null +++ b/src/test/kotlin/com/beust/kobalt/internal/BuildFilesTest.kt @@ -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() + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/beust/kobalt/internal/BuildOrderTest.kt b/src/test/kotlin/com/beust/kobalt/internal/BuildOrderTest.kt index cd660049..1ef53824 100644 --- a/src/test/kotlin/com/beust/kobalt/internal/BuildOrderTest.kt +++ b/src/test/kotlin/com/beust/kobalt/internal/BuildOrderTest.kt @@ -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 diff --git a/src/test/kotlin/com/beust/kobalt/internal/DependencyTest.kt b/src/test/kotlin/com/beust/kobalt/internal/DependencyTest.kt index 74db351e..521653fd 100644 --- a/src/test/kotlin/com/beust/kobalt/internal/DependencyTest.kt +++ b/src/test/kotlin/com/beust/kobalt/internal/DependencyTest.kt @@ -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) { val result = TestNgRunner().calculateAllJvmArgs(project, context, TestConfig(project), classpath, pluginInfo)