Skip to content

Commit 66a7a1f

Browse files
committed
run full test if classpath changes
1 parent 19f230d commit 66a7a1f

File tree

12 files changed

+66
-30
lines changed

12 files changed

+66
-30
lines changed

.gitignore

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
*.class
2-
*.log
3-
4-
.idea
5-
target
6-
project/project
7-
project/target
8-
.cache
9-
.classpath
10-
.project
11-
.settings
12-
bin
1+
*.class
2+
*.log
3+
4+
.idea
5+
target
6+
project/project
7+
project/target
8+
.cache
9+
.classpath
10+
.project
11+
.settings
12+
bin
13+
.bsp

src/main/scala/io/github/olegych/sbt/CachedCiPlugin.scala

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,50 @@
11
package io.github.olegych.sbt
22

33
import java.time.Instant
4-
5-
import sbt.Keys._
4+
import sbt.Keys.*
65
import sbt.Tags.Tag
7-
import sbt._
6+
import sbt.*
87
import sbt.plugins.JvmPlugin
98

10-
import scala.concurrent.duration._
9+
import java.io.FileNotFoundException
10+
import scala.concurrent.duration.*
1111

1212
object CachedCiPlugin extends AutoPlugin {
1313
override def trigger = allRequirements
14+
1415
override def requires = JvmPlugin
1516

1617
object autoImport {
1718
lazy val cachedCiTestFull = taskKey[Unit]("Full test.")
19+
lazy val cachedCiTestFullToken = taskKey[String]("Run full tests if this changed.")
1820
lazy val cachedCiTestFullPeriod = settingKey[FiniteDuration]("Period between full tests.")
1921
lazy val cachedCiTestQuick = taskKey[Unit]("Quick test.")
2022
lazy val cachedCiTest = taskKey[Unit]("Runs clean and full test if last full test was more than cachedCiTestFullPeriod ago, otherwise runs quick test.")
2123
}
2224

2325
import autoImport._
2426

25-
private case class Token(path: File) {
27+
private case class Token(path: File, value: String) {
2628
val lastModified = Instant.ofEpochMilli(path.lastModified())
27-
def valid(period: FiniteDuration) = path.exists() && lastModified.isAfter(Instant.now.minusMillis(period.toMillis))
29+
val lastValue = try IO.read(path) catch {
30+
case e: FileNotFoundException => ""
31+
}
32+
val valueChanged = path.exists() && lastValue != value
33+
34+
def valid(period: FiniteDuration) = !valueChanged && lastModified.isAfter(Instant.now.minusMillis(period.toMillis))
35+
2836
def refresh() = {
2937
path.getParentFile.mkdirs()
3038
path.delete()
31-
path.createNewFile()
39+
IO.write(path, value)
3240
}
3341
}
42+
3443
val CachedCiTest = Tag("CachedCiTest")
3544
override lazy val projectSettings = Seq(
3645
cachedCiTestFull := (Test / test).value,
3746
cachedCiTestQuick := (Test / testQuick).toTask("").value,
47+
cachedCiTestFullToken := (Runtime / fullClasspath).value.mkString,
3848
cachedCiTestFullPeriod := 24.hours,
3949
concurrentRestrictions += Tags.exclusive(CachedCiTest),
4050
cachedCiTest := Def.task {
@@ -47,12 +57,13 @@ object CachedCiPlugin extends AutoPlugin {
4757
runTask(thisProjectRef.value / t, s)
4858
}
4959

50-
val testFullToken = Token((if (crossPaths.value) crossTarget.value else target.value) / ".lastCachedCiTestFull")
51-
s.log.info(s"Last ${thisProjectRef.value.project} / ${cachedCiTest.key.label} was at ${testFullToken.lastModified}")
60+
val cachedCiTestFullTokenValue = cachedCiTestFullToken.value
61+
val testFullToken = Token((if (crossPaths.value) crossTarget.value else target.value) / ".lastCachedCiTestFull", cachedCiTestFullTokenValue)
62+
s.log.info(s"Last ${thisProjectRef.value.project} / ${cachedCiTest.key.label} was at ${testFullToken.lastModified}, token value changed: ${testFullToken.valueChanged}")
5263
if (testFullToken.valid(cachedCiTestFullPeriod.value)) {
5364
run(cachedCiTestQuick)
5465
} else {
55-
val cleanToken = Token(target.value / ".lastCachedCiTestClean")
66+
val cleanToken = Token(target.value / ".lastCachedCiTestClean", "")
5667
if (!cleanToken.valid(cachedCiTestFullPeriod.value)) {
5768
run(clean)
5869
cleanToken.refresh()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.9.3
1+
sbt.version=1.11.5

src/sbt-test/sbt-cached-ci/complex/test.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ lazy val b = project.settings(
44
cachedCiTestQuick := cachedCiTestQuick.dependsOn(a / cachedCiTestQuick).value,
55
cachedCiTestFull := cachedCiTestFull.dependsOn(a / cachedCiTestFull).value,
66
)
7-
ThisBuild / scalaVersion := "2.13.1"
8-
ThisBuild / crossScalaVersions := Seq("2.12.10", scalaVersion.value)
7+
ThisBuild / scalaVersion := "2.13.16"
8+
ThisBuild / crossScalaVersions := Seq("2.12.20", scalaVersion.value)
99
ThisBuild / libraryDependencies += "org.specs2" %% "specs2-junit" % "4.8.3" % Test
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.9.3
1+
sbt.version=1.11.5
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
lazy val root = project.in(file("."))
2-
.settings(scalaVersion := "2.13.1", crossScalaVersions := List(scalaVersion.value))
2+
.settings(scalaVersion := "2.13.16", crossScalaVersions := List(scalaVersion.value))
33
.aggregate(a).dependsOn(a)
44
lazy val a = project
5-
.settings(scalaVersion := "2.13.1", crossScalaVersions := List("2.12.11", scalaVersion.value))
5+
.settings(scalaVersion := "2.13.16", crossScalaVersions := List("2.12.20", scalaVersion.value))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.11.5
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
val pluginVersion = System.getProperty("plugin.version")
3+
if(pluginVersion == null)
4+
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
5+
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
6+
else addSbtPlugin("io.github.olegych" % """sbt-cached-ci""" % pluginVersion)
7+
}
8+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
> cachedCiTest
2+
$ exists target/scala-2.13
3+
> set cachedCiTestFull := Def.task {sys.error("failing test")}.value
4+
> cachedCiTest
5+
> set addDep := true
6+
-> cachedCiTest
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val addDep = settingKey[Boolean]("")
2+
lazy val root = project.in(file(".")).settings(
3+
scalaVersion := "2.13.16",
4+
addDep := false,
5+
libraryDependencies ++= (if (addDep.value) Seq("com.softwaremill.macwire" %% "util" % "2.6.6") else Nil),
6+
)

0 commit comments

Comments
 (0)