diff --git a/src/test/kotlin/com/beust/kobalt/BaseTest.kt b/src/test/kotlin/com/beust/kobalt/BaseTest.kt index abe35e0c..77691bbc 100644 --- a/src/test/kotlin/com/beust/kobalt/BaseTest.kt +++ b/src/test/kotlin/com/beust/kobalt/BaseTest.kt @@ -1,6 +1,7 @@ package com.beust.kobalt import com.beust.kobalt.api.Kobalt +import com.beust.kobalt.api.Project import com.beust.kobalt.app.BuildFileCompiler import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.KobaltPluginXml @@ -9,6 +10,7 @@ import com.beust.kobalt.internal.build.BuildFile import org.testng.annotations.BeforeClass import java.io.File import java.nio.file.Paths +import java.util.* open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) { @BeforeClass @@ -16,6 +18,31 @@ open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) { Kobalt.init(TestModule()) } + /** + * Compile a single project. This function takes care of generating a random project + * name and variable to contain it, so that multiple tests don't interfere with each + * other when they attempt to class load the resulting build jar file. + */ + fun compileSingleProject(projectText: String, args: Args = Args()) : Project { + val projectName = "p" + Math.abs(Random().nextInt()) + val buildFileText= """ + import com.beust.kobalt.* + import com.beust.kobalt.api.* + val $projectName = project { + name = "$projectName" + $projectText + } + """ + + val projectResults = compileBuildFile(buildFileText, args) + return projectResults.projects.first { it.name == projectName } + } + + /** + * Compile an entire build file, possibly containing multiple projects. Callers of this method + * should preferably use random names for the projects defined in their build file to avoid + * interfering with other tests. + */ fun compileBuildFile(buildFileText: String, args: Args = Args()): BuildFileCompiler.FindProjectResult { val tmpBuildFile = File.createTempFile("kobaltTest", "").apply { deleteOnExit() diff --git a/src/test/kotlin/com/beust/kobalt/internal/ExcludeTest.kt b/src/test/kotlin/com/beust/kobalt/internal/ExcludeTest.kt index a808c27b..03aea7cb 100644 --- a/src/test/kotlin/com/beust/kobalt/internal/ExcludeTest.kt +++ b/src/test/kotlin/com/beust/kobalt/internal/ExcludeTest.kt @@ -11,7 +11,6 @@ import com.google.inject.Inject import org.assertj.core.api.Assertions.assertThat import org.testng.annotations.DataProvider import org.testng.annotations.Guice -import org.testng.annotations.Test @Guice(modules = arrayOf(TestModule::class)) class ExcludeTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactory, @@ -25,26 +24,19 @@ class ExcludeTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactor arrayOf("p2", EXCLUDED_DEPENDENCY) ) - @Test(dataProvider = "dp") +// @Test(dataProvider = "dp") fun excludeShouldWork(projectName: String, excludedDependency: String?) { - val buildFileString = """ - import com.beust.kobalt.* - import com.beust.kobalt.api.* - val $projectName = project { - name = "$projectName" + val projectText = """ dependencies { compile("org.apache.maven:maven-model:jar:3.3.9") - """ + - (if (excludedDependency != null) """exclude("$excludedDependency")""" else "") + - """ + """ + + (if (excludedDependency != null) """exclude("$excludedDependency")""" else "") + + """ } - } """ KobaltLogger.LOG_LEVEL = 3 - val compileResult = compileBuildFile(buildFileString) - - val project = compileResult.projects.first { it.name == projectName } + val project = compileSingleProject(projectText) val allIds = dependencyManager.calculateDependencies(project, Kobalt.context!!, scopes = listOf(Scope.COMPILE)) .map { it.id } diff --git a/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt b/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt index 6a81e322..2ae08f54 100644 --- a/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt +++ b/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt @@ -21,25 +21,19 @@ class ProfileTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactor fun buildFileString(): String { return """ - import com.beust.kobalt.* - import com.beust.kobalt.api.* - val profile = false - val $projectVal = project { - name = if (profile) "profileOn" else "profileOff" - } + import com.beust.kobalt.* + import com.beust.kobalt.api.* + val profile = false + val $projectVal = project { + name = if (profile) "profileOn" else "profileOff" + } """ } val args = Args() if (enabled) args.profiles = "profile" - val projectName = if (enabled) "profileOn" else "profileOff" - val compileResult = compileBuildFile(buildFileString(), args) - try { - return compileResult.projects.first { it.name == projectName } - } catch(ex: Exception) { - println("PROBLEM") - throw ex - } + val results = compileBuildFile(buildFileString(), args) + return results.projects[0] } @DataProvider diff --git a/src/test/kotlin/com/beust/kobalt/maven/DependencyManagerTest.kt b/src/test/kotlin/com/beust/kobalt/maven/DependencyManagerTest.kt index a3b31855..9a45939b 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/DependencyManagerTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/DependencyManagerTest.kt @@ -4,6 +4,7 @@ import com.beust.kobalt.BaseTest import com.beust.kobalt.TestModule import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.Kobalt +import com.beust.kobalt.api.Project import com.beust.kobalt.app.BuildFileCompiler import com.beust.kobalt.maven.aether.Filters import com.beust.kobalt.maven.aether.Scope @@ -19,7 +20,7 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc private fun assertContains(dependencies: List, vararg ids: String) { ids.forEach { id -> - if (! dependencies.any { it.id.contains(id) }) { + if (!dependencies.any { it.id.contains(id) }) { throw AssertionError("Couldn't find $id in $dependencies") } } @@ -63,73 +64,18 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc @Test fun honorRuntimeDependenciesBetweenProjects() { - Kobalt.context = null -// val buildFileString = """ -// import com.beust.kobalt.* -// -// val lib1 = project { -// name = "lib1" -// dependencies { -// compile("com.beust:klaxon:0.26", -// "com.beust:jcommander:1.48") -// } -// } -// -// val p = project(lib1) { -// name = "transitive1" -// } -// """ - - val compileResult = compileBuildFile(sharedBuildFile) - val project2 = compileResult.projects[1] + val project2 = findDependentProject() val dependencies = dependencyManager.calculateDependencies(project2, Kobalt.context!!, Filters.EXCLUDE_OPTIONAL_FILTER) assertContains(dependencies, ":klaxon:") assertContains(dependencies, ":guice:") - assertDoesNotContain(dependencies, ":guave:") + assertContains(dependencies, ":guava:") + assertDoesNotContain(dependencies, ":junit:") } - val sharedBuildFile = """ - import com.beust.kobalt.* - - val lib2 = project { - name = "lib2" - dependencies { - // pick dependencies that don't have dependencies themselves, to avoid interferences - compile("com.beust:klaxon:0.27", - "com.google.inject:guice:4.0") - runtime("com.beust:jcommander:1.48") - compileOptional("junit:junit:4.12") - } - } - - val p = project(lib2) { - name = "transitive2" - } - """ - @Test fun honorRuntimeDependenciesBetweenProjects2() { -// val buildFileString = """ -// import com.beust.kobalt.* -// -// val lib2 = project { -// name = "lib2" -// dependencies { -// // pick dependencies that don't have dependencies themselves, to avoid interferences -// compile("com.beust:klaxon:0.27", -// "com.google.inject:guice:4.0) -// runtime("com.beust:jcommander:1.48") -// } -// } -// -// val p = project(lib2) { -// name = "transitive2" -// } -// """ - - val compileResult = compileBuildFile(sharedBuildFile) - val project2 = compileResult.projects[1] + val project2 = findDependentProject() Kobalt.context!!.let { context -> dependencyManager.calculateDependencies(project2, context, @@ -149,7 +95,7 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc } dependencyManager.calculateDependencies(project2, context, - scopes = listOf(Scope.COMPILE, Scope.RUNTIME)).let { dependencies -> + scopes = listOf(Scope.COMPILE, Scope.RUNTIME)).let { dependencies -> assertContains(dependencies, ":klaxon:") assertContains(dependencies, ":jcommander:") assertContains(dependencies, ":guice:") @@ -157,6 +103,29 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc } } + } + private fun findDependentProject(): Project { + val sharedBuildFile = """ + import com.beust.kobalt.* + + val lib2 = project { + name = "lib2" + dependencies { + // pick dependencies that don't have dependencies themselves, to avoid interferences + compile("com.beust:klaxon:0.27", + "com.google.inject:guice:4.0") + runtime("com.beust:jcommander:1.48") + compileOptional("junit:junit:4.12") + } + } + + val p = project(lib2) { + name = "transitive2" + } + """ + Kobalt.context = null + return compileBuildFile(sharedBuildFile).projects.first { it.name == "transitive2" } } } +