-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement selective promote for file snapshots
- Loading branch information
Showing
10 changed files
with
202 additions
and
44 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
modules/core/src/main/scala-2/com/siriusxm/snapshot4s/AssertFileSnapshotMacro.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2024 SiriusXM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package snapshot4s | ||
|
||
import scala.reflect.macros.blackbox | ||
|
||
private[snapshot4s] trait AssertFileSnapshotMacro[R] { | ||
|
||
/** Assert that a found value is equal to a previously snapshotted value. | ||
* | ||
* If the assertion fails, the file can be recreated with the found contents using `snapshot4sPromote`. | ||
* @see https://siriusxm.github.io/snapshot4s/file-snapshots | ||
* | ||
* @param found The found value. | ||
* @param snapshotPath Path to file containing the previously snapshotted value. This is relative to the resources/snapshot directory. | ||
* @param eq Compares the found and snapshot values. | ||
* @param resultLike Constructs a framework-specific result. | ||
*/ | ||
def assertFileSnapshot(found: String, snapshotPath: String)(implicit | ||
config: SnapshotConfig, | ||
snapshotEq: SnapshotEq[String], | ||
resultLike: ResultLike[String, R] | ||
): R = macro AssertFileSnapshotMacro.Macro.impl[R] | ||
} | ||
|
||
private[snapshot4s] object AssertFileSnapshotMacro { | ||
|
||
class Macro(val c: blackbox.Context) { | ||
import c.universe.* | ||
|
||
def impl[E]( | ||
found: Expr[String], | ||
snapshotPath: Expr[String] | ||
)( | ||
config: Expr[SnapshotConfig], | ||
snapshotEq: Expr[SnapshotEq[String]], | ||
resultLike: Expr[ResultLike[String, E]] | ||
): Tree = { | ||
// Scala 2 macro system will place this call in client code so the called method must be public | ||
// `FileSnapshotProxy.createFileSnapshot` is introduced to keep `FileSnapshot` private | ||
q"""_root_.snapshot4s.FileSnapshotProxy.createFileSnapshot($found, $snapshotPath, ${c.enclosingPosition.source.path}, $config, $snapshotEq, $resultLike)""" | ||
} | ||
} | ||
|
||
} | ||
|
||
object FileSnapshotProxy { | ||
|
||
def createFileSnapshot[E]( | ||
found: String, | ||
snapshotPath: String, | ||
sourceFile: String, | ||
config: SnapshotConfig, | ||
snapshotEq: SnapshotEq[String], | ||
resultLike: ResultLike[String, E] | ||
): E = FileSnapshot(found, snapshotPath, sourceFile, config, snapshotEq, resultLike) | ||
} |
61 changes: 61 additions & 0 deletions
61
modules/core/src/main/scala-3/com/siriusxm/snapshot4s/AssertFileSnapshotMacro.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 SiriusXM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package snapshot4s | ||
|
||
private[snapshot4s] trait AssertFileSnapshotMacro[R]: | ||
|
||
/** Assert that a found value is equal to a previously snapshotted value. | ||
* | ||
* If the assertion fails, the file can be recreated with the found contents using `snapshot4sPromote`. | ||
* @see https://siriusxm.github.io/snapshot4s/file-snapshots | ||
* | ||
* @param found The found value. | ||
* @param snapshotPath Path to file containing the previously snapshotted value. This is relative to the resources/snapshot directory. | ||
* @param eq Compares the found and snapshot values. | ||
* @param resultLike Constructs a framework-specific result. | ||
*/ | ||
inline def assertFileSnapshot(found: String, snapshotPath: String)(implicit | ||
config: SnapshotConfig, | ||
eq: SnapshotEq[String], | ||
resultLike: ResultLike[String, R] | ||
): R = | ||
${ | ||
AssertFileSnapshotMacro.impl( | ||
'found, | ||
'snapshotPath, | ||
'config, | ||
'eq, | ||
'resultLike | ||
) | ||
} | ||
|
||
import scala.quoted.* | ||
|
||
private[snapshot4s] object AssertFileSnapshotMacro: | ||
|
||
def impl[A, E]( | ||
found: Expr[String], | ||
snapshotPath: Expr[String], | ||
config: Expr[SnapshotConfig], | ||
snapshotEq: Expr[SnapshotEq[String]], | ||
resultLike: Expr[ResultLike[String, E]] | ||
)(using q: Quotes, ta: Type[A], te: Type[E]): Expr[E] = | ||
import q.reflect.* | ||
val sourceFile = Expr(Position.ofMacroExpansion.sourceFile.path) | ||
'{ | ||
FileSnapshot($found, $snapshotPath, $sourceFile, $config, $snapshotEq, $resultLike) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
modules/core/src/main/scala/com/siriusxm/snapshot4s/Locations.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2024 SiriusXM | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package snapshot4s | ||
|
||
private[snapshot4s] object Locations { | ||
|
||
private[snapshot4s] def getFileName(filePath: String): String = | ||
filePath.split("/").last | ||
|
||
private[snapshot4s] def relativeSourceFilePath( | ||
sourceFile: String, | ||
config: SnapshotConfig | ||
): RelPath = { | ||
val baseDirectory = config.sourceDirectory | ||
val sourceFilePath = Path(sourceFile) | ||
sourceFilePath.relativeTo(baseDirectory).getOrElse { | ||
throw new SnapshotConfigUnsupportedError(config) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters