Skip to content
This repository has been archived by the owner on May 22, 2022. It is now read-only.

properly handle test dependencies #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/scala/com/github/tkawachi/sbtlock/SbtLock.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ object SbtLock {
private[sbtlock] val DEFAULT_LOCK_FILE_NAME = "lock.sbt"
private[sbtlock] val DEPS_HASH_PREFIX = "// LIBRARY_DEPENDENCIES_HASH "

case class Artifact(organization: String, name: String) {
def sbtString(revision: String) =
Seq(organization, name, revision).map("\"" + _ + "\"").mkString(" % ")
}

def doLock(
allModules: Seq[ModuleID],
depsHash: String,
Expand All @@ -20,16 +15,21 @@ object SbtLock {

val moduleLines = allModules
.filter(m => m.organization != "org.scala-lang")
.map(mod =>
s""""${mod.organization}" % "${mod.name}" % "${mod.extraAttributes.getOrElse("version", mod.revision)}"""")
.map { mod =>
val attributes = List(
mod.organization,
mod.name,
mod.extraAttributes.getOrElse("version", mod.revision)) ++ mod.configurations.filter(_ != Default.name)
attributes.map(x => "\"" + x + "\"").mkString(" % ")
}
.distinct
.sorted

val dependencyOverrides =
s"""// DON'T EDIT THIS FILE.
|// This file is auto generated by ${BuildInfo.name} ${BuildInfo.version}.
|// ${BuildInfo._url}
|${Compat.syntax("Compile", "dependencyOverrides")} ++= {
|dependencyOverrides ++= {
| if (!(${Compat.syntax("ThisBuild", "sbtLockHashIsUpToDate")}).value && sbtLockIgnoreOverridesOnStaleHash.value) {
| ${Compat.dependencyOverridesType}.empty
| } else {
Expand Down
17 changes: 15 additions & 2 deletions src/main/scala/com/github/tkawachi/sbtlock/SbtLockPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ object SbtLockPlugin extends AutoPlugin {

override lazy val projectSettings = Seq(
SbtLockKeys.collectLockModuleIDs := {
val classpath: Seq[Attributed[File]] =
val compileClasspath: Seq[Attributed[File]] =
Classpaths.managedJars(Compile, classpathTypes.value, update.value)
val testClasspath: Seq[Attributed[File]] =
Classpaths.managedJars(Test, classpathTypes.value, update.value)
val logger = streams.value.log

classpath.flatMap { entry =>
def filesToModules(classpath: Seq[Attributed[File]]): Seq[ModuleID] = classpath.flatMap { entry =>
for {
art: Artifact <- entry.get(artifact.key)
mod: ModuleID <- entry.get(moduleID.key)
Expand All @@ -45,6 +47,17 @@ object SbtLockPlugin extends AutoPlugin {
mod
}
}

val compileModules = filesToModules(compileClasspath)
//only add a module with test configuration if they exist in compileModules but with a different version
val testModules = filesToModules(testClasspath).filter {
testModule =>
compileModules.exists(compileModule =>
compileModule.organization == testModule.organization &&
compileModule.name == testModule.name &&
compileModule.revision != testModule.revision)
}.map(_.withConfigurations(Some(Test.name)))
compileModules ++ testModules
},
lock := {
val lockFile = new File(baseDirectory.value, sbtLockLockFile.value)
Expand Down
40 changes: 40 additions & 0 deletions src/sbt-test/sbt-lock/compileVsTest/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// https://github.com/playframework/playframework/blob/2.2.3/framework/project/Dependencies.scala#L101
libraryDependencies += "com.typesafe.play" %% "play" % "2.2.3"
libraryDependencies += "com.fasterxml.jackson.core" % "jackson-core" % "2.2.3" % "test"

resolvers += Resolver.typesafeRepo("releases")

scalaVersion := "2.10.4"

def check(module: Seq[String], dependencies: Set[ModuleID]): Boolean = {
val (org, name, v, c) = module match {
case Seq(org, name, v) => (org, name, v, None)
case Seq(org, name, v, c) => (org, name, v, Some(c))
}
dependencies.exists { m =>
m.organization == org && m.name == name && m.revision == v && m.configurations == c
}
}

InputKey[Unit]("checkExistsDependency") := {
val module = complete.Parsers.spaceDelimited("").parsed
val exists = check(module, (dependencyOverrides in Compile).value.toSet)
assert(
exists,
s"${module.mkString(":")} should exist in dependencyOverrides: ${(dependencyOverrides in Compile).value}")
}

InputKey[Unit]("checkAbsentDependency") := {
val module = complete.Parsers.spaceDelimited("").parsed
val exists = check(module, (dependencyOverrides in Compile).value.toSet)
assert(
!exists,
s"${module.mkString(":")} should not exist in dependencyOverrides: ${(dependencyOverrides in Compile).value}")
}

InputKey[Unit]("addDependency") := {
val module = complete.Parsers.spaceDelimited("").parsed
val Seq(org, name, v) = module
val line = "libraryDependencies += \"" + org + "\" %% \"" + name + "\" % \"" + v + "\""
IO.append(new File(Keys.baseDirectory.value, "build.sbt"), "\n\n" + line)
}
7 changes: 7 additions & 0 deletions src/sbt-test/sbt-lock/compileVsTest/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("com.github.tkawachi" % "sbt-lock" % pluginVersion)
}
7 changes: 7 additions & 0 deletions src/sbt-test/sbt-lock/compileVsTest/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
> lock
> reload
$ exec cat lock.sbt
> checkExistsDependency "com.fasterxml.jackson.core" "jackson-core" "2.2.2"
> checkExistsDependency "com.fasterxml.jackson.core" "jackson-core" "2.2.3" "test"
> checkAbsentDependency "com.fasterxml.jackson.core" "jackson-core" "2.2.2" "test"