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

Attempt to fix the tests for TeamCity.

This commit is contained in:
Cedric Beust 2017-03-15 14:14:27 -07:00
parent bd861a9198
commit c16aa0aa67
3 changed files with 19 additions and 8 deletions

View file

@ -9,6 +9,7 @@ import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.internal.build.BuildFile import com.beust.kobalt.internal.build.BuildFile
import com.beust.kobalt.misc.KobaltLogger import com.beust.kobalt.misc.KobaltLogger
import org.testng.annotations.BeforeClass import org.testng.annotations.BeforeClass
import java.io.File
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Paths import java.nio.file.Paths
import java.util.* import java.util.*
@ -26,17 +27,20 @@ open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) {
*/ */
fun compileSingleProject(projectText: String, args: Args = Args()) : Project { fun compileSingleProject(projectText: String, args: Args = Args()) : Project {
val projectName = "p" + Math.abs(Random().nextInt()) val projectName = "p" + Math.abs(Random().nextInt())
val projectDirectory = Files.createTempDirectory("kobaltTest").toFile().path
val buildFileText= """ val buildFileText= """
import com.beust.kobalt.* import com.beust.kobalt.*
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
val $projectName = project { val $projectName = project {
name = "$projectName" name = "$projectName"
directory = "$projectDirectory"
$projectText $projectText
} }
""" """.trim()
args.noIncremental = true args.noIncremental = true
val projectResults = compileBuildFile(buildFileText, args) val projectResults = compileBuildFile(projectDirectory, buildFileText, args)
val result = projectResults.projects.firstOrNull { it.name == projectName } val result = projectResults.projects.firstOrNull { it.name == projectName }
if (result == null) { if (result == null) {
throw IllegalArgumentException("Couldn't find project named $projectName in " throw IllegalArgumentException("Couldn't find project named $projectName in "
@ -51,10 +55,11 @@ open class BaseTest(val compilerFactory: BuildFileCompiler.IFactory? = null) {
* should preferably use random names for the projects defined in their build file to avoid * should preferably use random names for the projects defined in their build file to avoid
* interfering with other tests. * interfering with other tests.
*/ */
fun compileBuildFile(buildFileText: String, args: Args = Args()): BuildFileCompiler.FindProjectResult { fun compileBuildFile(projectDirectory: String, buildFileText: String, args: Args = Args())
: BuildFileCompiler .FindProjectResult {
KobaltLogger.LOG_LEVEL = 3 KobaltLogger.LOG_LEVEL = 3
val tmpBaseDir = Files.createTempDirectory("kobaltTest") val path = Paths.get(projectDirectory)
val tmpBuildFile = Files.createTempFile(tmpBaseDir, "kobaltTest", "").toFile().apply { val tmpBuildFile = File(path.toFile(), "Build.kt").apply {
deleteOnExit() deleteOnExit()
writeText(buildFileText) writeText(buildFileText)
} }

View file

@ -11,6 +11,7 @@ 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 import org.testng.annotations.Test
import java.nio.file.Files
import java.util.* import java.util.*
@Guice(modules = arrayOf(TestModule::class)) @Guice(modules = arrayOf(TestModule::class))
@ -18,6 +19,7 @@ class ProfileTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactor
private fun runTestWithProfile(enabled: Boolean) : Project { private fun runTestWithProfile(enabled: Boolean) : Project {
val projectVal = "p" + Math.abs(Random().nextInt()) val projectVal = "p" + Math.abs(Random().nextInt())
val projectDirectory = Files.createTempDirectory("kobaltTest").toFile().path
fun buildFileString(): String { fun buildFileString(): String {
return """ return """
@ -26,13 +28,14 @@ class ProfileTest @Inject constructor(compilerFactory: BuildFileCompiler.IFactor
val profile = false val profile = false
val $projectVal = project { val $projectVal = project {
name = if (profile) "profileOn" else "profileOff" name = if (profile) "profileOn" else "profileOff"
directory = "$projectDirectory"
} }
""" """.trim()
} }
val args = Args() val args = Args()
if (enabled) args.profiles = "profile" if (enabled) args.profiles = "profile"
val results = compileBuildFile(buildFileString(), args) val results = compileBuildFile(projectDirectory, buildFileString(), args)
return results.projects[0] return results.projects[0]
} }

View file

@ -13,6 +13,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.eclipse.aether.util.filter.AndDependencyFilter import org.eclipse.aether.util.filter.AndDependencyFilter
import org.testng.annotations.Guice import org.testng.annotations.Guice
import org.testng.annotations.Test import org.testng.annotations.Test
import java.nio.file.Files
@Guice(modules = arrayOf(TestModule::class)) @Guice(modules = arrayOf(TestModule::class))
class DependencyManagerTest @Inject constructor(val dependencyManager: DependencyManager, class DependencyManagerTest @Inject constructor(val dependencyManager: DependencyManager,
@ -106,11 +107,13 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc
} }
private fun findDependentProject(): Project { private fun findDependentProject(): Project {
val projectDirectory = Files.createTempDirectory("kobaltTest").toFile().path
val sharedBuildFile = """ val sharedBuildFile = """
import com.beust.kobalt.* import com.beust.kobalt.*
val lib2 = project { val lib2 = project {
name = "lib2" name = "lib2"
directory = "$projectDirectory"
dependencies { dependencies {
// pick dependencies that don't have dependencies themselves, to avoid interferences // pick dependencies that don't have dependencies themselves, to avoid interferences
compile("com.beust:klaxon:0.27", compile("com.beust:klaxon:0.27",
@ -125,7 +128,7 @@ class DependencyManagerTest @Inject constructor(val dependencyManager: Dependenc
} }
""" """
Kobalt.context = null Kobalt.context = null
return compileBuildFile(sharedBuildFile).projects.first { it.name == "transitive2" } return compileBuildFile(projectDirectory, sharedBuildFile).projects.first { it.name == "transitive2" }
} }
} }