Skip to content

Commit

Permalink
Fix various scripted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eed3si9n committed Dec 18, 2023
1 parent 152f668 commit 0c8f8cc
Show file tree
Hide file tree
Showing 41 changed files with 147 additions and 178 deletions.
15 changes: 4 additions & 11 deletions main/src/main/scala/sbt/Defaults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -718,14 +718,7 @@ object Defaults extends BuildCommon {
CrossVersion(scalaV, binVersion)(base).withCrossVersion(Disabled())
},
crossSbtVersions := Vector((pluginCrossBuild / sbtVersion).value),
crossTarget := makeCrossTarget(
target.value,
scalaVersion.value,
scalaBinaryVersion.value,
(pluginCrossBuild / sbtBinaryVersion).value,
sbtPlugin.value,
crossPaths.value
),
crossTarget := target.value,
cleanIvy := IvyActions.cleanCachedResolutionCache(ivyModule.value, streams.value.log),
clean := clean.dependsOnTask(cleanIvy).value,
scalaCompilerBridgeBinaryJar := Def.settingDyn {
Expand Down Expand Up @@ -4697,18 +4690,18 @@ trait BuildExtra extends BuildCommon with DefExtra {
libraryDependencies += compilerPlugin(dependency)

/** Constructs a setting that declares a new artifact `a` that is generated by `taskDef`. */
def addArtifact(a: Artifact, taskDef: TaskKey[VirtualFile]): SettingsDefinition = {
def addArtifact(a: Artifact, taskDef: TaskKey[HashedVirtualFileRef]): SettingsDefinition = {
val pkgd = packagedArtifacts := packagedArtifacts.value.updated(a, taskDef.value)
Seq(artifacts += a, pkgd)
}

/** Constructs a setting that declares a new artifact `artifact` that is generated by `taskDef`. */
def addArtifact(
artifact: Initialize[Artifact],
taskDef: Initialize[Task[VirtualFile]]
taskDef: Initialize[Task[HashedVirtualFileRef]]
): SettingsDefinition = {
val artLocal = SettingKey.local[Artifact]
val taskLocal = TaskKey.local[VirtualFile]
val taskLocal = TaskKey.local[HashedVirtualFileRef]
val art = artifacts := artLocal.value +: artifacts.value
val pkgd = packagedArtifacts := packagedArtifacts.value.updated(artLocal.value, taskLocal.value)
Seq(artLocal := artifact.value, taskLocal := taskDef.value, art, pkgd)
Expand Down
8 changes: 8 additions & 0 deletions notes/2.0.0/migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


## files extension on Classpath

```scala
+ given FileConverter = fileConverter.value
val cp = (Compile / classpath).value.files
```
2 changes: 2 additions & 0 deletions sbt-app/src/main/scala/sbt/Import.scala
Original file line number Diff line number Diff line change
Expand Up @@ -366,5 +366,7 @@ trait Import {
type IvyPaths = sbt.librarymanagement.ivy.IvyPaths
val IvyPaths = sbt.librarymanagement.ivy.IvyPaths

type FileConverter = xsbti.FileConverter
type HashedVirtualFileRef = xsbti.HashedVirtualFileRef
type IncOptions = xsbti.compile.IncOptions
}
9 changes: 7 additions & 2 deletions sbt-app/src/sbt-test/dependency-management/artifact/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ lazy val root = (project in file("."))
Compile / packageBin / artifact := mainArtifact,
libraryDependencies ++= (if (baseDirectory.value / "retrieve").exists then publishedID :: Nil else Nil),
// needed to add a jar with a different type to the managed classpath
Compile / unmanagedClasspath ++= scalaInstance.value.libraryJars.toSeq,
Compile / unmanagedClasspath ++= {
val converter = fileConverter.value
val xs = scalaInstance.value.libraryJars.toSeq
xs.map(x => converter.toVirtualFile(x.toPath()): HashedVirtualFileRef)
},
classpathTypes := Set(tpe),

// custom configuration artifacts
Expand Down Expand Up @@ -66,7 +70,8 @@ def retrieveID = org % "test-retrieve" % "2.0"
def checkTask(classpath: TaskKey[Classpath]) =
Def.task {
val deps = libraryDependencies.value
val cp = (Compile / classpath).value.files
given FileConverter = fileConverter.value
val cp = (Compile / classpath).value.files.map(_.toFile())
val loader = ClasspathUtilities.toLoader(cp, scalaInstance.value.loader)
try { Class.forName("test.Test", false, loader); () }
catch { case _: ClassNotFoundException | _: NoClassDefFoundError => sys.error(s"Dependency not retrieved properly: $deps, $cp") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ val checkScalaLibrary = TaskKey[Unit]("checkScalaLibrary")
checkScalaLibrary := {
val scalaLibsJars = (Compile / managedClasspath)
.value
.map(_.data.getName)
.map(_.data.name)
.filter(_.startsWith("scala-library"))
.sorted
val expectedScalaLibsJars = Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ lazy val root = (project in file(".")).
settings(commonSettings: _*).
settings(
check := {
val acp = (a / Compile / externalDependencyClasspath).value.map {_.data.getName}.sorted
val acp = (a / Compile / externalDependencyClasspath).value.map {_.data.name}.sorted
if (!(acp contains "netty-3.2.0.Final.jar")) {
sys.error("netty-3.2.0.Final not found when it should be included: " + acp.toString)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ lazy val root = (project in file(".")).
(ThisBuild / organization) := "org.example",
(ThisBuild / version) := "1.0",
check := {
val acp = (a / Compile / externalDependencyClasspath).value.map {_.data.getName}.sorted
val bcp = (b / Compile / externalDependencyClasspath).value.map {_.data.getName}.sorted
val ccp = (c / Compile / externalDependencyClasspath).value.map {_.data.getName}.sorted filterNot { _ == "demo_2.10.jar"}
val acp = (a / Compile / externalDependencyClasspath).value.map {_.data.name}.sorted
val bcp = (b / Compile / externalDependencyClasspath).value.map {_.data.name}.sorted
val ccp = (c / Compile / externalDependencyClasspath).value.map {_.data.name}.sorted filterNot { _ == "demo_2.10.jar"}
if (!(acp contains "commons-io-1.4-sources.jar")) {
sys.error("commons-io-1.4-sources not found when it should be included: " + acp.toString)
}
Expand All @@ -90,9 +90,9 @@ lazy val root = (project in file(".")).
"\n - b (plain) " + bcpWithoutSource.toString +
"\n - c (inter-project) " + ccpWithoutSource.toString)

val atestcp = (a / Test / externalDependencyClasspath).value.map {_.data.getName}.sorted filterNot { _ == "commons-io-1.4.jar"}
val btestcp = (b / Test / externalDependencyClasspath).value.map {_.data.getName}.sorted filterNot { _ == "commons-io-1.4.jar"}
val ctestcp = (c / Test / externalDependencyClasspath).value.map {_.data.getName}.sorted filterNot { _ == "demo_2.10.jar"} filterNot { _ == "commons-io-1.4.jar"}
val atestcp = (a / Test / externalDependencyClasspath).value.map {_.data.name}.sorted filterNot { _ == "commons-io-1.4.jar"}
val btestcp = (b / Test / externalDependencyClasspath).value.map {_.data.name}.sorted filterNot { _ == "commons-io-1.4.jar"}
val ctestcp = (c / Test / externalDependencyClasspath).value.map {_.data.name}.sorted filterNot { _ == "demo_2.10.jar"} filterNot { _ == "commons-io-1.4.jar"}
if (ctestcp contains "junit-4.13.1.jar") {
sys.error("junit found when it should be excluded: " + ctestcp.toString)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ val y2 = project.settings(
)

TaskKey[Unit]("check") := {
val x1cp = (x1 / Compile / externalDependencyClasspath).value.map(_.data.getName).sorted
val x1cp = (x1 / Compile / externalDependencyClasspath).value.map(_.data.name).sorted
def x1cpStr = x1cp.mkString("\n* ", "\n* ", "")

// if (!(x1cp contains "slf4j-api-1.6.6.jar"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ lazy val root = (project in file(".")).
version := "1.0",
updateOptions := updateOptions.value.withCachedResolution(true),
check := {
val acp = (a / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val bcp = (b / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
if (acp exists { _.data.getName contains "commons-io" }) {
val acp = (a / Compile / externalDependencyClasspath).value.sortBy {_.data.name}
val bcp = (b / Compile / externalDependencyClasspath).value.sortBy {_.data.name}
if (acp exists { _.data.name contains "commons-io" }) {
sys.error("commons-io found when it should be excluded")
}
if (acp exists { _.data.getName contains "commons-codec" }) {
if (acp exists { _.data.name contains "commons-codec" }) {
sys.error("commons-codec found when it should be excluded")
}
// This is checking to make sure excluded graph is not getting picked up
if (!(bcp exists { _.data.getName contains "commons-io" })) {
if (!(bcp exists { _.data.name contains "commons-io" })) {
sys.error("commons-io NOT found when it should NOT be excluded")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,33 @@ lazy val root = (project in file(".")).
ThisBuild / version := "1.0",
check := {
// sys.error(dependencyCacheDirectory.value.toString)
val acp = (a / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val bcp = (b / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val ccp = (c / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val dcp = (d / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val acp = (a / Compile / externalDependencyClasspath).value.sortBy {_.data.name}
val bcp = (b / Compile / externalDependencyClasspath).value.sortBy {_.data.name}
val ccp = (c / Compile / externalDependencyClasspath).value.sortBy {_.data.name}
val dcp = (d / Compile / externalDependencyClasspath).value.sortBy {_.data.name}

if (!(acp exists {_.data.getName contains "spring-core-3.2.2.RELEASE"})) {
if (!(acp exists {_.data.name contains "spring-core-3.2.2.RELEASE"})) {
sys.error("spring-core-3.2.2 is not found on a")
}
if (!(bcp exists {_.data.getName contains "spring-core-3.2.2.RELEASE"})) {
if (!(bcp exists {_.data.name contains "spring-core-3.2.2.RELEASE"})) {
sys.error("spring-core-3.2.2 is not found on b")
}
if (!(ccp exists {_.data.getName contains "spring-core-3.2.2.RELEASE"})) {
if (!(ccp exists {_.data.name contains "spring-core-3.2.2.RELEASE"})) {
sys.error("spring-core-3.2.2 is not found on c")
}
if (!(dcp exists {_.data.getName contains "spring-core-3.2.2.RELEASE"})) {
if (!(dcp exists {_.data.name contains "spring-core-3.2.2.RELEASE"})) {
sys.error("spring-core-3.2.2 is not found on d\n" + dcp.toString)
}
if (!(acp exists {_.data.getName contains "spring-tx-3.1.2.RELEASE"})) {
if (!(acp exists {_.data.name contains "spring-tx-3.1.2.RELEASE"})) {
sys.error("spring-tx-3.1.2 is not found on a")
}
if (!(bcp exists {_.data.getName contains "spring-tx-3.1.2.RELEASE"})) {
if (!(bcp exists {_.data.name contains "spring-tx-3.1.2.RELEASE"})) {
sys.error("spring-tx-3.1.2 is not found on b")
}
if (!(ccp exists {_.data.getName contains "spring-tx-3.1.2.RELEASE"})) {
if (!(ccp exists {_.data.name contains "spring-tx-3.1.2.RELEASE"})) {
sys.error("spring-tx-3.1.2 is not found on c")
}
if (!(dcp exists {_.data.getName contains "spring-tx-3.1.2.RELEASE"})) {
if (!(dcp exists {_.data.name contains "spring-tx-3.1.2.RELEASE"})) {
sys.error("spring-tx-3.1.2 is not found on d")
}
if (acp == bcp) ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ lazy val root = (project in file(".")).
updateOptions := updateOptions.value.withCachedResolution(true),
check := {
val ur = (a / update).value
val acp = (a / Compile / externalDependencyClasspath).value.map {_.data.getName}
val acp = (a / Compile / externalDependencyClasspath).value.map {_.data.name}
val atestcp0 = (a / Test / fullClasspath).value
val atestcp = (a / Test / externalDependencyClasspath).value.map {_.data.getName}
val atestcp = (a / Test / externalDependencyClasspath).value.map {_.data.name}
// This is checking to make sure interproject dependency works
if (acp exists { _ contains "scalatest" }) {
sys.error("scalatest found when it should NOT be included: " + acp.toString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ lazy val b = project.
lazy val root = (project in file(".")).
settings(
check := {
val acp = (a / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val bcp = (b / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val acp = (a / Compile / externalDependencyClasspath).value.sortBy {_.data.name}
val bcp = (b / Compile / externalDependencyClasspath).value.sortBy {_.data.name}
if (acp == bcp) ()
else sys.error("Different classpaths are found:" +
"\n - a (overrides + cached) " + acp.toString +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ TaskKey[Unit]("check") := {
def isTestJar(n: String): Boolean =
(n contains "scalacheck") ||
(n contains "specs2")
val testLibs = cp map (_.data.getName) filter isTestJar
val testLibs = cp map (_.data.name) filter isTestJar
assert(testLibs.isEmpty, s"Compile Classpath has test libs:\n * ${testLibs.mkString("\n * ")}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ lazy val root = (project in file(".")).
check := {
(a / update).value
(b / update).value
val acp = (a / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val bcp = (b / Compile / externalDependencyClasspath).value.sortBy {_.data.getName}
val acp = (a / Compile / externalDependencyClasspath).value.sortBy {_.data.name}
val bcp = (b / Compile / externalDependencyClasspath).value.sortBy {_.data.name}

if (acp exists { _.data.getName contains "slf4j-api-1.7.5.jar" }) {
if (acp exists { _.data.name contains "slf4j-api-1.7.5.jar" }) {
sys.error("slf4j-api-1.7.5.jar found when it should NOT be included: " + acp.toString)
}
if (bcp exists { _.data.getName contains "dispatch-core_2.11-0.11.1.jar" }) {
if (bcp exists { _.data.name contains "dispatch-core_2.11-0.11.1.jar" }) {
sys.error("dispatch-core_2.11-0.11.1.jar found when it should NOT be included: " + bcp.toString)
}

Expand Down
17 changes: 10 additions & 7 deletions sbt-app/src/sbt-test/dependency-management/exclude-scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ lazy val root = (project in file(".")).
scalaOverride := check("scala.App").value
)

def check(className: String): Def.Initialize[Task[Unit]] = (Compile / fullClasspath) map { cp =>
val existing = cp.files.filter(_.getName contains "scala-library")
println("Full classpath: " + cp.mkString("\n\t", "\n\t", ""))
println("scala-library.jar: " + existing.mkString("\n\t", "\n\t", ""))
val loader = ClasspathUtilities.toLoader(existing)
Class.forName(className, false, loader)
}
def check(className: String): Def.Initialize[Task[Unit]] =
import sbt.TupleSyntax.*
(Compile / fullClasspath, fileConverter.toTaskable) mapN { (cp, c) =>
given FileConverter = c
val existing = cp.files.filter(_.toFile.getName contains "scala-library")
println("Full classpath: " + cp.mkString("\n\t", "\n\t", ""))
println("scala-library.jar: " + existing.mkString("\n\t", "\n\t", ""))
val loader = ClasspathUtilities.toLoader(existing.map(_.toFile()))
Class.forName(className, false, loader)
}

def dependencies(base: File) =
if( ( base / "stm").exists ) ("org.scala-tools" % "scala-stm_2.8.2" % "0.6") :: Nil
Expand Down
2 changes: 1 addition & 1 deletion sbt-app/src/sbt-test/dependency-management/force/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def libraryDeps(base: File) = {
def check(ver: String) =
(Compile / dependencyClasspath) map { jars =>
val log4j = jars map (_.data) collect {
case f if f.getName contains "log4j-" => f.getName
case f if f.name contains "log4j-" => f.name
}
if (log4j.size != 1 || !log4j.head.contains(ver))
sys.error("Did not download the correct jar.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ ThisBuild / useCoursier := false
lazy val commonSettings = Seq(
autoScalaLibrary := false,
scalaModuleInfo := None,
(Compile / unmanagedJars) ++= (scalaInstance map (_.allJars.toSeq)).value,
(Compile / unmanagedJars) ++= {
val converter = fileConverter.value
val xs = scalaInstance.value.allJars.toSeq
xs.map(_.toPath).map(x => converter.toVirtualFile(x): HashedVirtualFileRef)
},
(packageSrc / publishArtifact) := false,
(packageDoc / publishArtifact) := false,
publishMavenStyle := false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ lazy val expectedInter =
</dependency>

def checkTask(expectedDep: xml.Elem) = TaskKey[Unit]("checkPom") := {
val file = makePom.value
val pom = xml.XML.loadFile(file)
val vf = makePom.value
val converter = fileConverter.value
val pom = xml.XML.loadFile(converter.toPath(vf).toFile)
val actual = pom \\ "dependencies"
val expected = <d>
{expectedDep}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import scala.xml._

lazy val root = (project in file(".")) settings (
readPom := (makePom map XML.loadFile).value,
readPom := {
val vf = makePom.value
val converter = fileConverter.value
XML.loadFile(converter.toPath(vf).toFile)
},
TaskKey[Unit]("checkPom") := checkPom.value,
TaskKey[Unit]("checkExtra") := checkExtra.value,
TaskKey[Unit]("checkVersionPlusMapping") := checkVersionPlusMapping.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ moduleName := "asdf"
crossPaths := false

TaskKey[Unit]("checkName") := Def task {
val path = (Compile / packageBin).value.getAbsolutePath
val converter = fileConverter.value
val vf = (Compile / packageBin).value
val path = converter.toPath(vf).toAbsolutePath.toString
val module = moduleName.value
val n = name.value
assert(path contains module, s"Path $path did not contain module name $module")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
libraryDependencies ++= Seq("natives-windows", "natives-linux", "natives-osx") map ( c =>
"org.lwjgl.lwjgl" % "lwjgl-platform" % "2.8.2" classifier c
"org.lwjgl.lwjgl" % "lwjgl-platform" % "2.8.2" classifier c
)

autoScalaLibrary := false

TaskKey[Unit]("check") := ((Compile / dependencyClasspath) map { cp =>
assert(cp.size == 3, "Expected 3 jars, got: " + cp.files.mkString("(", ", ", ")"))
assert(cp.size == 3, "Expected 3 jars, got: " + cp.mkString("(", ", ", ")"))
}).value

TaskKey[Unit]("checkPom") := {
val file = makePom.value
val pom = xml.XML.loadFile(file)
val actual = pom \\ "dependencies"
def depSection(classifier: String) =
<dependency>
<groupId>org.lwjgl.lwjgl</groupId>
<artifactId>lwjgl-platform</artifactId>
<version>2.8.2</version>
<classifier>{classifier}</classifier>
</dependency>
val sections = <d>
val vf = makePom.value
val converter = fileConverter.value
val pom = xml.XML.loadFile(converter.toPath(vf).toFile)
val actual = pom \\ "dependencies"
def depSection(classifier: String) =
<dependency>
<groupId>org.lwjgl.lwjgl</groupId>
<artifactId>lwjgl-platform</artifactId>
<version>2.8.2</version>
<classifier>{classifier}</classifier>
</dependency>
val sections = <d>
{depSection("natives-windows") ++ depSection("natives-linux") ++ depSection("natives-osx")}
</d>
def dropTopElem(s:String): String = s.split("""\n""").drop(1).dropRight(1).mkString("\n")
val pp = new xml.PrettyPrinter(Int.MaxValue, 0)
val expectedString = dropTopElem(pp.formatNodes(sections))
val actualString = dropTopElem(pp.formatNodes(actual))
assert(expectedString == actualString, "Expected dependencies section:\n" + expectedString + "\n\nActual:\n" + actualString)
def dropTopElem(s:String): String = s.split("""\n""").drop(1).dropRight(1).mkString("\n")
val pp = new xml.PrettyPrinter(Int.MaxValue, 0)
val expectedString = dropTopElem(pp.formatNodes(sections))
val actualString = dropTopElem(pp.formatNodes(actual))
assert(expectedString == actualString, "Expected dependencies section:\n" + expectedString + "\n\nActual:\n" + actualString)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ autoScalaLibrary := false
TaskKey[Unit]("check") := {
val cp = (Compile / externalDependencyClasspath).value
val tcp = (Test / externalDependencyClasspath).value
assert(cp.size == 2, "Expected 2 jars on compile classpath, got: " + cp.files.mkString("(", ", ", ")"))
assert(cp.size == 2, "Expected 2 jars on compile classpath, got: " + cp.mkString("(", ", ", ")"))
// this should really be 1 because of intransitive(), but Ivy doesn't handle this.
// So, this test can only check that the assertion reported in #582 isn't triggered.
assert(tcp.size == 2, "Expected 2 jar on test classpath, got: " + tcp.files.mkString("(", ", ", ")"))
assert(tcp.size == 2, "Expected 2 jar on test classpath, got: " + tcp.mkString("(", ", ", ")"))
}
Loading

0 comments on commit 0c8f8cc

Please sign in to comment.