Skip to content

Commit

Permalink
Make it possible to set a custom Settings for a specific test.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Dec 20, 2023
1 parent 9f217c1 commit 0b8140d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,28 @@ interface SelfieSettingsAPI {
"src/test/scala",
"src/test/resources")
internal fun initialize(): SelfieSettingsAPI {
val settings = System.getProperty("selfie.settings")
if (settings != null && settings.trim().isNotEmpty()) {
try {
return instantiate(Class.forName(settings))
} catch (e: ClassNotFoundException) {
throw Error(
"The system property selfie.settings was set to $settings, but that class could not be found.",
e)
}
}
try {
val clazz = Class.forName("com.diffplug.selfie.SelfieSettings")
return clazz.getDeclaredConstructor().newInstance() as SelfieSettingsAPI
return instantiate(Class.forName("SelfieSettings"))
} catch (e: ClassNotFoundException) {
return SelfieSettingsNoOp()
}
}
private fun instantiate(clazz: Class<*>): SelfieSettingsAPI {
try {
return clazz.getDeclaredConstructor().newInstance() as SelfieSettingsAPI
} catch (e: InstantiationException) {
throw AssertionError(
"Unable to instantiate com.diffplug.SelfieSettings, is it abstract?", e)
"Unable to instantiate ${clazz.name}, is it abstract? Does it require arguments?", e)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.xml.sax.InputSource

open class Harness(subproject: String) {
val subprojectFolder: Path
var settings = ""

init {
var rootFolder = FileSystem.SYSTEM.canonicalize("".toPath())
Expand Down Expand Up @@ -267,17 +268,17 @@ open class Harness(subproject: String) {
return error
}
fun gradleWriteSS() {
gradlew("underTest", "-Pselfie=write")?.let {
gradlew("underTest", "-Pselfie=write", "-Pselfie.settings=${settings}")?.let {
throw AssertionError("Expected write snapshots to succeed, but it failed", it)
}
}
fun gradleReadSS() {
gradlew("underTest", "-Pselfie=read")?.let {
gradlew("underTest", "-Pselfie=read", "-Pselfie.settings=${settings}")?.let {
throw AssertionError("Expected read snapshots to succeed, but it failed", it)
}
}
fun gradleReadSSFail(): AssertionFailedError {
val failure = gradlew("underTest", "-Pselfie=read")
val failure = gradlew("underTest", "-Pselfie=read", "-Pselfie.settings=${settings}")
if (failure == null) {
throw AssertionError("Expected read snapshots to fail, but it succeeded.")
} else {
Expand Down
2 changes: 2 additions & 0 deletions undertest-junit5/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tasks.register('underTest', Test) {
outputs.upToDateWhen { false }
// defaults to 'write'
systemProperty 'selfie', findProperty('selfie')
systemProperty 'selfie.settings', findProperty('selfie.settings')
}
tasks.register('underTestRead', Test) {
useJUnitPlatform()
Expand All @@ -44,4 +45,5 @@ tasks.register('underTestRead', Test) {
outputs.upToDateWhen { false }
// read-only
systemProperty 'selfie', 'read'
systemProperty 'selfie.settings', findProperty('selfie.settings')
}

0 comments on commit 0b8140d

Please sign in to comment.