Skip to content

Commit

Permalink
Allow promoting selected suites based on filter
Browse files Browse the repository at this point in the history
  • Loading branch information
majk-p committed Sep 16, 2024
1 parent 7fb07ef commit 2052eb2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package snapshot4s

import sbt.*
import sbt.Keys.*
import sbt.complete.DefaultParsers.*
import sbt.util.Logger

object Snapshot4sPlugin extends AutoPlugin {
Expand All @@ -29,7 +30,7 @@ object Snapshot4sPlugin extends AutoPlugin {
settingKey[File]("The directory in which snapshot4s results are stored prior to promotion.")
val snapshot4sSourceGenerator =
taskKey[Seq[File]]("Generate source files for snapshot4s testing.")
val snapshot4sPromote = taskKey[Unit]("Update failing snapshot4s snapshot files.")
val snapshot4sPromote = inputKey[Unit]("Update failing snapshot4s snapshot files.")
}

import autoImport.*
Expand Down Expand Up @@ -65,17 +66,29 @@ object generated {
},
snapshot4sPromote := {
val log = streams.value.log
val arguments =
spaceDelimited("<tests filter>")
.examples("*MySuite*", "*MySuite.scala")
.parsed

val filter = makeFilter(arguments)
applyResourcePatches(log)(
snapshot4sDirectory.value / "resource-patch",
snapshot4sResourceDirectory.value
snapshot4sResourceDirectory.value,
filter
)
applyInlinePatches(log)(
snapshot4sDirectory.value / "inline-patch",
sourceBaseDirectory((Test / sourceDirectories).value)
sourceBaseDirectory((Test / sourceDirectories).value),
filter
)
}
)

private def makeFilter(arguments: Seq[String]): NameFilter =
if (arguments.isEmpty) AllPassFilter
else arguments.map(GlobFilter(_)).reduce(_ | _)

private def sourceBaseDirectory(sourceDirectories: Seq[File]): File = {
def sharedParent(dirA: File, dirB: File): File = {
if (dirB.getAbsolutePath().startsWith(dirA.getAbsolutePath())) dirA
Expand All @@ -85,9 +98,12 @@ object generated {
sourceDirectories.reduce(sharedParent)
}

private def applyResourcePatches(log: Logger)(resourcePatchDir: File, resourceDir: File) = {
val patches = (resourcePatchDir ** (-DirectoryFilter)).get
patches.foreach { patchFile =>
private def applyResourcePatches(
log: Logger
)(resourcePatchDir: File, resourceDir: File, filter: NameFilter) = {
val patches = (resourcePatchDir ** (-DirectoryFilter)).get
val filteredPatches = patches.filter(file => filter.accept(file.getParent))
filteredPatches.foreach { patchFile =>
val patchContents = IO.read(patchFile)
val relativeSourceFile = IO.relativize(resourcePatchDir, patchFile).get
val sourceFile = resourceDir / relativeSourceFile
Expand All @@ -97,10 +113,12 @@ object generated {
}
}

private def applyInlinePatches(log: Logger)(inlinePatchDir: File, sourceDir: File) = {
val allChangeFiles = (inlinePatchDir ** (-DirectoryFilter)).get

allChangeFiles.groupBy(_.getParent).foreach { case (parentDir, changeFiles) =>
private def applyInlinePatches(
log: Logger
)(inlinePatchDir: File, sourceDir: File, filter: NameFilter) = {
val patchDirectories = (inlinePatchDir ** (-DirectoryFilter)).get
val dirsByParent = patchDirectories.groupBy(_.getParent).filterKeys(filter.accept)
dirsByParent.foreach { case (parentDir, changeFiles) =>
val relativeSourceFile = IO.relativize(inlinePatchDir, new File(parentDir)).get
val sourceFile = sourceDir / relativeSourceFile
val sourceContents = IO.read(sourceFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import snapshot4s.BuildInfo.snapshot4sVersion

lazy val root = (project in file("."))
.settings(
scalaVersion := "3.3.1",
scalacOptions += "-Xsource:3",
crossScalaVersions := Seq("3.3.1", "2.12.20", "2.13.14"),
libraryDependencies += "com.siriusxm" %% "snapshot4s-weaver" % snapshot4sVersion % Test
)
.enablePlugins(Snapshot4sPlugin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sys.props.get("plugin.version") match {
case Some(x) =>
addSbtPlugin("com.siriusxm" % "sbt-snapshot4s" % x)
case _ => sys.error("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
old-contents
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
old-contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package simple

import weaver.*
import snapshot4s.weaver.SnapshotExpectations
import snapshot4s.generated.snapshotConfig

object FilterTest extends SimpleIOSuite with SnapshotExpectations {

test("inline") {
assertInlineSnapshot(1, 2)
}

test("new inline snapshot") {
assertInlineSnapshot(1, ???)
}

test("file that doesn't exist") {
assertFileSnapshot("contents", "nonexistent-file")
}

test("existing file") {
assertFileSnapshot("contents", "existing-file")
}

test("file in nested directory") {
assertFileSnapshot("contents", "nested-directory/nested-file")
}

}
20 changes: 20 additions & 0 deletions modules/plugin/src/sbt-test/sbt-snapshot4s/promote-filter/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated code should compile
> ++ 2.12 compile
> ++ 2.13 compile
> ++ 3 compile
# # Tests should fail as snapshots are out of date
-> ++ 2.12 test
-> ++ 2.13 test
-> ++ 3 test
# Not update anything with invalid filter
> snapshot4sPromote *IDontExist*
# Tests should fail as snapshots are out of date
-> ++ 2.12 test
-> ++ 2.13 test
-> ++ 3 test
# # Update snapshots with correct filter
> snapshot4sPromote *FilterTest*
# # Tests should succeed with updated snapshots
> ++ 2.12 test
> ++ 2.13 test
> ++ 3 test

0 comments on commit 2052eb2

Please sign in to comment.