selfie icon indicating copy to clipboard operation
selfie copied to clipboard

Remove requirement to not have "/" in test names? (Junit 5)

Open veeti opened this issue 11 months ago • 1 comments

Fantastic tool, thank you! I only hit a little snag: in my project I already have many Kotest/Junit5 units named after HTTP endpoints such as GET /example, and I'm hitting this error:

Caused by: java.lang.IllegalStateException: Test name cannot contain '/', was ...
	at com.diffplug.selfie.junit5.SnapshotFileProgress.startTest(SnapshotSystemJUnit5.kt:188)

This happens for every test even though they do not invoke Selfie for snapshots. Could this requirement be loosened somehow? For example:

  • Escape the resulting name of snapshot files to remove the slash
  • Do not run this check in tests that do not make use of Selfie

Thank you.

veeti avatar Jan 24 '25 06:01 veeti

Workarounds:

  • in your test names, replace / with some other filename-safe character like % or even division slash (if you're crazy).
  • have two test suites (Gradle makes this pretty easy, not sure about Maven), and only add Selfie to one of them.

I would love to merge a PR which relaxes this requirement, I think you nailed the two paths forward:

Do not run this check in tests that do not make use of Selfie

I think this will be hard. An intentional design decision in Selfie was "add the dependency and it just works". A downside of this is that the Selfie test runner can't know ahead of time whether any given test is going to use Selfie or not. Delaying the check to a lazy "only if used" is possible, but looks tricky to me. If it adds a ton of complexity to the code, then I don't want to merge it.

Escape the resulting name of snapshot files to remove the slash

This would be great! Selfie already has a rigorous escape system so that snapshot names and contents can be literally anything without causing any problems:

https://github.com/diffplug/selfie/blob/a5073e772957c4aa03b625012bc45ec06e9f155a/jvm/selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/SnapshotFile.kt#L382-L388

I think the right thing is to have a filenameEsc = PerCharacterEscaper.specifiedEscape("@@<(>):c\"q/f\b|p?q*s"). This would create an escaper that does the following transformation:

  • @ -> @@
  • < -> @(
  • " -> @q
  • / -> @f (forward slash)
  • etc

It should be pretty straightforward from there, but there might be some gotchas I haven't thought of.

nedtwigg avatar Jan 24 '25 17:01 nedtwigg