diff --git a/src/test/kotlin/com/beust/kobalt/BaseTest.kt b/src/test/kotlin/com/beust/kobalt/BaseTest.kt index b71e9566..abe35e0c 100644 --- a/src/test/kotlin/com/beust/kobalt/BaseTest.kt +++ b/src/test/kotlin/com/beust/kobalt/BaseTest.kt @@ -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) } } \ No newline at end of file diff --git a/src/test/kotlin/com/beust/kobalt/internal/ExcludeTest.kt b/src/test/kotlin/com/beust/kobalt/internal/ExcludeTest.kt new file mode 100644 index 00000000..0bce1fc5 --- /dev/null +++ b/src/test/kotlin/com/beust/kobalt/internal/ExcludeTest.kt @@ -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>( + 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) + } + + } +} + + diff --git a/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt b/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt index 1ebbd54e..6a81e322 100644 --- a/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt +++ b/src/test/kotlin/com/beust/kobalt/internal/ProfileTest.kt @@ -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 diff --git a/src/test/kotlin/com/beust/kobalt/maven/DependencyManagerTest.kt b/src/test/kotlin/com/beust/kobalt/maven/DependencyManagerTest.kt index 00b263ee..a3b31855 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/DependencyManagerTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/DependencyManagerTest.kt @@ -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, 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 ->