mirror of https://github.com/sbt/sbt.git
[2.x] fix: skip writing sbt.version in scripted test directories (#8673)
**Problem** 1. Write a scripted test that runs sbt in that test. 2. sbt writes `sbt.version` under `project/build.properties`, which acts as a test filter. 3. The test setup is implicitly altered. **Solution** Before writing `build.properties`, sbt checks whether the base directory is inside a scripted test directory (path contains `sbt-test`). If so, it skips writing so the test setup is not altered.
This commit is contained in:
parent
afc9a90374
commit
a76074a602
|
|
@ -1115,26 +1115,37 @@ object BuiltinCommands {
|
|||
private val sbtVersionRegex = """sbt\.version\s*=.*""".r
|
||||
private def isSbtVersionLine(s: String) = sbtVersionRegex.pattern.matcher(s).matches()
|
||||
|
||||
private def writeSbtVersionUnconditionally(state: State) = {
|
||||
/** True if baseDir appears to be inside a scripted test directory (sbt#4536). */
|
||||
private def isScriptedTestDirectory(baseDir: File): Boolean = {
|
||||
val path = baseDir.getAbsolutePath
|
||||
path.contains(java.io.File.separator + "sbt-test" + java.io.File.separator) ||
|
||||
path.contains("/sbt-test/")
|
||||
}
|
||||
|
||||
private def writeSbtVersionUnconditionally(state: State): Unit = {
|
||||
val baseDir = state.baseDir
|
||||
val sbtVersion = BuiltinCommands.sbtVersion(state)
|
||||
val projectDir = baseDir / "project"
|
||||
val buildProps = projectDir / "build.properties"
|
||||
if (isScriptedTestDirectory(baseDir)) {
|
||||
// sbt#4536: don't alter scripted test setup
|
||||
} else {
|
||||
val sbtVersion = BuiltinCommands.sbtVersion(state)
|
||||
val projectDir = baseDir / "project"
|
||||
val buildProps = projectDir / "build.properties"
|
||||
|
||||
val buildPropsLines = if (buildProps.canRead) IO.readLines(buildProps) else Nil
|
||||
val buildPropsLines = if (buildProps.canRead) IO.readLines(buildProps) else Nil
|
||||
|
||||
val sbtVersionAbsent = buildPropsLines forall (!isSbtVersionLine(_))
|
||||
val sbtVersionAbsent = buildPropsLines forall (!isSbtVersionLine(_))
|
||||
|
||||
if (sbtVersionAbsent) {
|
||||
val warnMsg = s"No sbt.version set in project/build.properties, base directory: $baseDir"
|
||||
try {
|
||||
if (isSbtBuild(baseDir)) {
|
||||
val line = s"sbt.version=$sbtVersion"
|
||||
IO.writeLines(buildProps, line :: buildPropsLines)
|
||||
state.log.info(s"Updated file $buildProps: set sbt.version to $sbtVersion")
|
||||
} else state.log.warn(warnMsg)
|
||||
} catch {
|
||||
case _: IOException => state.log.warn(warnMsg)
|
||||
if (sbtVersionAbsent) {
|
||||
val warnMsg = s"No sbt.version set in project/build.properties, base directory: $baseDir"
|
||||
try {
|
||||
if (isSbtBuild(baseDir)) {
|
||||
val line = s"sbt.version=$sbtVersion"
|
||||
IO.writeLines(buildProps, line :: buildPropsLines)
|
||||
state.log.info(s"Updated file $buildProps: set sbt.version to $sbtVersion")
|
||||
} else state.log.warn(warnMsg)
|
||||
} catch {
|
||||
case _: IOException => state.log.warn(warnMsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue