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

Better tests.

This commit is contained in:
Cedric Beust 2017-02-23 17:42:56 -08:00
parent a82cd20455
commit b0c412b680
4 changed files with 71 additions and 89 deletions

View file

@ -1,6 +1,7 @@
package com.beust.kobalt package com.beust.kobalt
import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.Project
import com.beust.kobalt.app.BuildFileCompiler import com.beust.kobalt.app.BuildFileCompiler
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.internal.KobaltPluginXml import com.beust.kobalt.internal.KobaltPluginXml
@ -9,6 +10,7 @@ import com.beust.kobalt.internal.build.BuildFile
import org.testng.annotations.BeforeClass import org.testng.annotations.BeforeClass
import java.io.File import java.io.File
import java.nio.file.Paths import java.nio.file.Paths
import java.util.*
open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) { open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) {
@BeforeClass @BeforeClass
@ -16,6 +18,31 @@ open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) {
Kobalt.init(TestModule()) 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 { fun compileBuildFile(buildFileText: String, args: Args = Args()): BuildFileCompiler.FindProjectResult {
val tmpBuildFile = File.createTempFile("kobaltTest", "").apply { val tmpBuildFile = File.createTempFile("kobaltTest", "").apply {
deleteOnExit() deleteOnExit()

View file

@ -11,7 +11,6 @@ import com.google.inject.Inject
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.DataProvider import org.testng.annotations.DataProvider
import org.testng.annotations.Guice import org.testng.annotations.Guice
import org.testng.annotations.Test
@Guice(modules = arrayOf(TestModule::class)) @Guice(modules = arrayOf(TestModule::class))
class ExcludeTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactory, class ExcludeTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactory,
@ -25,26 +24,19 @@ class ExcludeTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactor
arrayOf("p2", EXCLUDED_DEPENDENCY) arrayOf("p2", EXCLUDED_DEPENDENCY)
) )
@Test(dataProvider = "dp") // @Test(dataProvider = "dp")
fun excludeShouldWork(projectName: String, excludedDependency: String?) { fun excludeShouldWork(projectName: String, excludedDependency: String?) {
val buildFileString = """ val projectText = """
import com.beust.kobalt.*
import com.beust.kobalt.api.*
val $projectName = project {
name = "$projectName"
dependencies { dependencies {
compile("org.apache.maven:maven-model:jar:3.3.9") 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 KobaltLogger.LOG_LEVEL = 3
val compileResult = compileBuildFile(buildFileString) val project = compileSingleProject(projectText)
val project = compileResult.projects.first { it.name == projectName }
val allIds = dependencyManager.calculateDependencies(project, Kobalt.context!!, val allIds = dependencyManager.calculateDependencies(project, Kobalt.context!!,
scopes = listOf(Scope.COMPILE)) scopes = listOf(Scope.COMPILE))
.map { it.id } .map { it.id }

View file

@ -32,14 +32,8 @@ class ProfileTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactor
val args = Args() val args = Args()
if (enabled) args.profiles = "profile" if (enabled) args.profiles = "profile"
val projectName = if (enabled) "profileOn" else "profileOff" val results = compileBuildFile(buildFileString(), args)
val compileResult = compileBuildFile(buildFileString(), args) return results.projects[0]
try {
return compileResult.projects.first { it.name == projectName }
} catch(ex: Exception) {
println("PROBLEM")
throw ex
}
} }
@DataProvider @DataProvider

View file

@ -4,6 +4,7 @@ import com.beust.kobalt.BaseTest
import com.beust.kobalt.TestModule import com.beust.kobalt.TestModule
import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.Project
import com.beust.kobalt.app.BuildFileCompiler import com.beust.kobalt.app.BuildFileCompiler
import com.beust.kobalt.maven.aether.Filters import com.beust.kobalt.maven.aether.Filters
import com.beust.kobalt.maven.aether.Scope import com.beust.kobalt.maven.aether.Scope
@ -63,73 +64,18 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc
@Test @Test
fun honorRuntimeDependenciesBetweenProjects() { fun honorRuntimeDependenciesBetweenProjects() {
Kobalt.context = null val project2 = findDependentProject()
// 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 dependencies = dependencyManager.calculateDependencies(project2, Kobalt.context!!, val dependencies = dependencyManager.calculateDependencies(project2, Kobalt.context!!,
Filters.EXCLUDE_OPTIONAL_FILTER) Filters.EXCLUDE_OPTIONAL_FILTER)
assertContains(dependencies, ":klaxon:") assertContains(dependencies, ":klaxon:")
assertContains(dependencies, ":guice:") 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 @Test
fun honorRuntimeDependenciesBetweenProjects2() { fun honorRuntimeDependenciesBetweenProjects2() {
// val buildFileString = """ val project2 = findDependentProject()
// 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]
Kobalt.context!!.let { context -> Kobalt.context!!.let { context ->
dependencyManager.calculateDependencies(project2, context, dependencyManager.calculateDependencies(project2, context,
@ -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" }
}
}