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

Add an exclude() test.

… but disabled, until I can figure out why it interferes with the profile test.
This commit is contained in:
Cedric Beust 2017-02-22 20:45:11 -08:00
parent b54c4c63c8
commit 3290769453
4 changed files with 90 additions and 20 deletions

View file

@ -10,24 +10,25 @@ import org.testng.annotations.BeforeClass
import java.io.File
import java.nio.file.Paths
open class BaseTest {
open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) {
@BeforeClass
fun bc() {
Kobalt.init(TestModule())
}
fun compileBuildFile(buildFileText: String, args: Args, compilerFactory: BuildFileCompiler.IFactory)
: BuildFileCompiler.FindProjectResult {
fun compileBuildFile(buildFileText: String, args: Args = Args()): BuildFileCompiler.FindProjectResult {
val tmpBuildFile = File.createTempFile("kobaltTest", "").apply {
deleteOnExit()
writeText(buildFileText)
}
val thisBuildFile = BuildFile(Paths.get(tmpBuildFile.absolutePath), "Build.kt")
args.buildFile = tmpBuildFile.absolutePath
args.apply {
buildFile = tmpBuildFile.absolutePath
}
val jvmCompilerPlugin = Kobalt.findPlugin("JvmCompiler") as JvmCompilerPlugin
val pluginInfo = PluginInfo(KobaltPluginXml(), null, null).apply {
projectContributors.add(jvmCompilerPlugin)
}
return compilerFactory.create(listOf(thisBuildFile), pluginInfo).compileBuildFiles(args)
return compilerFactory!!.create(listOf(thisBuildFile), pluginInfo).compileBuildFiles(args)
}
}

View file

@ -0,0 +1,59 @@
package com.beust.kobalt.internal
import com.beust.kobalt.BaseTest
import com.beust.kobalt.TestModule
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.app.BuildFileCompiler
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.aether.Scope
import com.beust.kobalt.misc.KobaltLogger
import com.google.inject.Inject
import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.DataProvider
import org.testng.annotations.Guice
@Guice(modules = arrayOf(TestModule::class))
class ExcludeTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactory,
val dependencyManager: DependencyManager) : BaseTest(compilerFactory) {
val EXCLUDED_DEPENDENCY = "org.codehaus.plexus:plexus-utils:jar:3.0.22"
@DataProvider
fun dp() = arrayOf<Array<String?>>(
arrayOf("p1", null),
arrayOf("p2", EXCLUDED_DEPENDENCY)
)
// @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"
dependencies {
compile("org.apache.maven:maven-model:jar:3.3.9")
""" +
(if (excludedDependency != null) """exclude("$excludedDependency")""" else "") +
"""
}
}
"""
KobaltLogger.LOG_LEVEL = 3
val compileResult = compileBuildFile(buildFileString)
val project = compileResult.projects.first { it.name == projectName }
val allIds = dependencyManager.calculateDependencies(project, Kobalt.context!!,
scopes = listOf(Scope.COMPILE))
.map { it.id }
if (excludedDependency != null) {
assertThat(allIds).doesNotContain(excludedDependency)
} else {
assertThat(allIds).contains(EXCLUDED_DEPENDENCY)
}
}
}

View file

@ -11,24 +11,35 @@ import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.DataProvider
import org.testng.annotations.Guice
import org.testng.annotations.Test
import java.util.*
@Guice(modules = arrayOf(TestModule::class))
class ProfileTest @Inject constructor(val compilerFactory: BuildFileCompiler.IFactory) : BaseTest() {
class ProfileTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactory) : BaseTest(compilerFactory) {
private fun runTestWithProfile(enabled: Boolean) : Project {
val buildFileString = """
import com.beust.kobalt.*
import com.beust.kobalt.api.*
val profile = false
val p = project {
name = if (profile) "profileOn" else "profileOff"
}
"""
val projectVal = "p" + Math.abs(Random().nextInt())
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"
}
"""
}
val args = Args()
if (enabled) args.profiles = "profile"
val compileResult = compileBuildFile(buildFileString, args, compilerFactory)
return compileResult.projects[0]
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
}
}
@DataProvider

View file

@ -1,6 +1,5 @@
package com.beust.kobalt.maven
import com.beust.kobalt.Args
import com.beust.kobalt.BaseTest
import com.beust.kobalt.TestModule
import com.beust.kobalt.api.IClasspathDependency
@ -16,7 +15,7 @@ import org.testng.annotations.Test
@Guice(modules = arrayOf(TestModule::class))
class DependencyManagerTest @Inject constructor(val dependencyManager: DependencyManager,
val compilerFactory: BuildFileCompiler.IFactory) : BaseTest() {
compilerFactory: BuildFileCompiler.IFactory) : BaseTest(compilerFactory) {
private fun assertContains(dependencies: List<IClasspathDependency>, vararg ids: String) {
ids.forEach { id ->
@ -81,7 +80,7 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc
// }
// """
val compileResult = compileBuildFile(sharedBuildFile, Args(), compilerFactory)
val compileResult = compileBuildFile(sharedBuildFile)
val project2 = compileResult.projects[1]
val dependencies = dependencyManager.calculateDependencies(project2, Kobalt.context!!,
Filters.EXCLUDE_OPTIONAL_FILTER)
@ -129,7 +128,7 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc
// }
// """
val compileResult = compileBuildFile(sharedBuildFile, Args(), compilerFactory)
val compileResult = compileBuildFile(sharedBuildFile)
val project2 = compileResult.projects[1]
Kobalt.context!!.let { context ->