@@ -17,8 +17,6 @@ package com.diffplug.selfie.junit5
17
17
18
18
import com.diffplug.selfie.RW
19
19
import com.diffplug.selfie.Snapshot
20
- import java.nio.file.Files
21
- import java.nio.file.Paths
22
20
import java.util.stream.Collectors
23
21
import kotlin.io.path.name
24
22
@@ -32,24 +30,26 @@ data class CallLocation(val clazz: String, val method: String, val file: String?
32
30
* If the runtime didn't give us the filename, guess it from the class, and try to find the source
33
31
* file by walking the CWD. If we don't find it, report it as a `.class` file.
34
32
*/
35
- private fun findFileIfAbsent (): String {
33
+ private fun findFileIfAbsent (layout : SnapshotFileLayout ): String {
36
34
if (file != null ) {
37
35
return file
38
36
}
39
- val fileWithoutExtension = clazz.substringAfterLast(' .' ).substringBefore(' $' )
40
- val likelyExtensions = listOf (" kt" , " java" , " scala" , " groovy" , " clj" , " cljc" )
41
- val filenames = likelyExtensions.map { " $fileWithoutExtension .$it " }.toSet()
42
- val firstPath = Files .walk(Paths .get(" " )).use { it.filter { it.name in filenames }.findFirst() }
43
- return if (firstPath.isEmpty) " ${clazz.substringAfterLast(' .' )} .class" else firstPath.get().name
37
+ return layout.sourcecodeForCall(this )?.name ? : " ${clazz.substringAfterLast(' .' )} .class"
44
38
}
45
39
46
40
/* * A `toString` which an IDE will render as a clickable link. */
47
- override fun toString (): String = " $clazz .$method (${findFileIfAbsent()} :$line )"
41
+ fun ideLink (layout : SnapshotFileLayout ): String {
42
+ return " $clazz .$method (${findFileIfAbsent(layout)} :$line )"
43
+ }
48
44
}
49
45
/* * Represents the callstack above a given CallLocation. */
50
46
class CallStack (val location : CallLocation , val restOfStack : List <CallLocation >) {
51
- override fun toString (): String {
52
- return location.toString()
47
+ fun ideLink (layout : SnapshotFileLayout ): String {
48
+ val list = buildList {
49
+ add(location)
50
+ addAll(restOfStack)
51
+ }
52
+ return list.joinToString(" \n " ) { it.ideLink(layout) }
53
53
}
54
54
}
55
55
/* * Generates a CallLocation and the CallStack behind it. */
@@ -69,25 +69,27 @@ internal class FirstWrite<T>(val snapshot: T, val callStack: CallStack)
69
69
/* * For tracking the writes of disk snapshots literals. */
70
70
internal open class WriteTracker <K : Comparable <K >, V > {
71
71
val writes = mutableMapOf<K , FirstWrite <V >>()
72
- protected fun recordInternal (key : K , snapshot : V , call : CallStack ) {
72
+ protected fun recordInternal (key : K , snapshot : V , call : CallStack , layout : SnapshotFileLayout ) {
73
73
val existing = writes.putIfAbsent(key, FirstWrite (snapshot, call))
74
74
if (existing != null ) {
75
75
if (existing.snapshot != snapshot) {
76
76
throw org.opentest4j.AssertionFailedError (
77
- " Snapshot was set to multiple values!\n first time: ${existing.callStack} \n this time: ${call} " ,
77
+ " Snapshot was set to multiple values!\n first time: ${existing.callStack.location.ideLink(layout) } \n this time: ${call.location.ideLink(layout) } " ,
78
78
existing.snapshot,
79
79
snapshot)
80
80
} else if (RW .isWriteOnce) {
81
81
throw org.opentest4j.AssertionFailedError (
82
- " Snapshot was set to the same value multiple times." , existing.callStack, call)
82
+ " Snapshot was set to the same value multiple times." ,
83
+ existing.callStack.ideLink(layout),
84
+ call.ideLink(layout))
83
85
}
84
86
}
85
87
}
86
88
}
87
89
88
90
internal class DiskWriteTracker : WriteTracker <String , Snapshot >() {
89
- fun record (key : String , snapshot : Snapshot , call : CallStack ) {
90
- recordInternal(key, snapshot, call)
91
+ fun record (key : String , snapshot : Snapshot , call : CallStack , layout : SnapshotFileLayout ) {
92
+ recordInternal(key, snapshot, call, layout )
91
93
}
92
94
}
93
95
@@ -96,7 +98,7 @@ class LiteralValue {
96
98
}
97
99
98
100
internal class InlineWriteTracker : WriteTracker <CallLocation , LiteralValue >() {
99
- fun record (call : CallStack , snapshot : LiteralValue ) {
100
- recordInternal(call.location, snapshot, call)
101
+ fun record (call : CallStack , snapshot : LiteralValue , layout : SnapshotFileLayout ) {
102
+ recordInternal(call.location, snapshot, call, layout )
101
103
}
102
104
}
0 commit comments