@@ -30,28 +30,28 @@ import org.junit.platform.launcher.TestPlan
30
30
internal object Router {
31
31
private class ClassMethod (val clazz : ClassProgress , val method : String )
32
32
private val threadCtx = ThreadLocal <ClassMethod ?>()
33
- fun readOrWriteOrKeep (snapshot : Snapshot ? , subOrKeepAll : String? ): Snapshot ? {
34
- val classMethod =
35
- threadCtx.get()
36
- ? : throw AssertionError (
37
- " Selfie `toMatchDisk` must be called only on the original thread." )
38
- return if (subOrKeepAll == null ) {
39
- assert (snapshot == null )
40
- classMethod.clazz.keep(classMethod.method, null )
41
- null
33
+ private fun classAndMethod () =
34
+ threadCtx.get()
35
+ ? : throw AssertionError (
36
+ " Selfie `toMatchDisk` must be called only on the original thread." )
37
+ private fun suffix (sub : String ) = if (sub == " " ) " " else " /$sub "
38
+ fun readWriteThroughPipeline (actual : Snapshot , sub : String ): ExpectedActual {
39
+ val cm = classAndMethod()
40
+ val suffix = suffix(sub)
41
+ val callStack = recordCall()
42
+ return if (RW .isWrite) {
43
+ cm.clazz.write(cm.method, suffix, actual, callStack)
44
+ ExpectedActual (actual, actual)
42
45
} else {
43
- val suffix = if (subOrKeepAll == " " ) " " else " /$subOrKeepAll "
44
- if (snapshot == null ) {
45
- classMethod.clazz.keep(classMethod.method, suffix)
46
- null
47
- } else {
48
- if (RW .isWrite) {
49
- classMethod.clazz.write(classMethod.method, suffix, snapshot)
50
- snapshot
51
- } else {
52
- classMethod.clazz.read(classMethod.method, suffix)
53
- }
54
- }
46
+ ExpectedActual (cm.clazz.read(cm.method, suffix), actual)
47
+ }
48
+ }
49
+ fun keep (subOrKeepAll : String? ) {
50
+ val cm = classAndMethod()
51
+ if (subOrKeepAll == null ) {
52
+ cm.clazz.keep(cm.method, null )
53
+ } else {
54
+ cm.clazz.keep(cm.method, suffix(subOrKeepAll))
55
55
}
56
56
}
57
57
internal fun start (clazz : ClassProgress , method : String ) {
@@ -71,18 +71,10 @@ internal object Router {
71
71
}
72
72
threadCtx.set(null )
73
73
}
74
- fun fileLocationFor (className : String ): Path {
75
- if (layout == null ) {
76
- layout = SnapshotFileLayout .initialize(className)
77
- }
78
- return layout!! .snapshotPathForClass(className)
79
- }
80
-
81
- var layout: SnapshotFileLayout ? = null
82
74
}
83
75
84
76
/* * Tracks the progress of test runs within a single class, so that snapshots can be pruned. */
85
- internal class ClassProgress (val className : String ) {
77
+ internal class ClassProgress (val parent : Progress , val className : String ) {
86
78
companion object {
87
79
val TERMINATED =
88
80
ArrayMap .empty<String , MethodSnapshotGC >().plus(" ~ f!n1shed ~ " , MethodSnapshotGC ())
@@ -113,7 +105,7 @@ internal class ClassProgress(val className: String) {
113
105
MethodSnapshotGC .findStaleSnapshotsWithin(className, file!! .snapshots, methods)
114
106
if (staleSnapshotIndices.isNotEmpty() || file!! .wasSetAtTestTime) {
115
107
file!! .removeAllIndices(staleSnapshotIndices)
116
- val snapshotPath = Router .fileLocationFor (className)
108
+ val snapshotPath = parent.layout.snapshotPathForClass (className)
117
109
if (file!! .snapshots.isEmpty()) {
118
110
deleteFileAndParentDirIfEmpty(snapshotPath)
119
111
} else {
@@ -127,7 +119,7 @@ internal class ClassProgress(val className: String) {
127
119
// we never read or wrote to the file
128
120
val isStale = MethodSnapshotGC .isUnusedSnapshotFileStale(className, methods, success)
129
121
if (isStale) {
130
- val snapshotFile = Router .fileLocationFor (className)
122
+ val snapshotFile = parent.layout.snapshotPathForClass (className)
131
123
deleteFileAndParentDirIfEmpty(snapshotFile)
132
124
}
133
125
}
@@ -145,17 +137,14 @@ internal class ClassProgress(val className: String) {
145
137
methods[method]!! .keepSuffix(suffixOrAll)
146
138
}
147
139
}
148
- @Synchronized fun write (method : String , suffix : String , snapshot : Snapshot ) {
140
+ @Synchronized fun write (method : String , suffix : String , snapshot : Snapshot , callStack : CallStack ) {
149
141
assertNotTerminated()
150
142
val key = " $method$suffix "
151
- diskWriteTracker!! .record(key, snapshot, recordCall() )
143
+ diskWriteTracker!! .record(key, snapshot, callStack, parent.layout )
152
144
methods[method]!! .keepSuffix(suffix)
153
145
read().setAtTestTime(key, snapshot)
154
146
}
155
- @Synchronized fun read (
156
- method : String ,
157
- suffix : String ,
158
- ): Snapshot ? {
147
+ @Synchronized fun read (method : String , suffix : String ): Snapshot ? {
159
148
assertNotTerminated()
160
149
val snapshot = read().snapshots[" $method$suffix " ]
161
150
if (snapshot != null ) {
@@ -165,13 +154,13 @@ internal class ClassProgress(val className: String) {
165
154
}
166
155
private fun read (): SnapshotFile {
167
156
if (file == null ) {
168
- val snapshotPath = Router .fileLocationFor (className)
157
+ val snapshotPath = parent.layout.snapshotPathForClass (className)
169
158
file =
170
159
if (Files .exists(snapshotPath) && Files .isRegularFile(snapshotPath)) {
171
160
val content = Files .readAllBytes(snapshotPath)
172
161
SnapshotFile .parse(SnapshotValueReader .of(content))
173
162
} else {
174
- SnapshotFile .createEmptyWithUnixNewlines(Router .layout!! .unixNewlines)
163
+ SnapshotFile .createEmptyWithUnixNewlines(parent .layout.unixNewlines)
175
164
}
176
165
}
177
166
return file!!
@@ -183,14 +172,17 @@ internal class ClassProgress(val className: String) {
183
172
* - pruning unused snapshot files
184
173
*/
185
174
internal class Progress {
175
+ val settings = SelfieSettingsAPI .initialize()
176
+ val layout = SnapshotFileLayout .initialize(settings)
177
+
186
178
private var progressPerClass = ArrayMap .empty<String , ClassProgress >()
187
179
private fun forClass (className : String ) = synchronized(this ) { progressPerClass[className]!! }
188
180
189
181
// TestExecutionListener
190
182
fun start (className : String , method : String? ) {
191
183
if (method == null ) {
192
184
synchronized(this ) {
193
- progressPerClass = progressPerClass.plus(className, ClassProgress (className))
185
+ progressPerClass = progressPerClass.plus(className, ClassProgress (this , className))
194
186
}
195
187
} else {
196
188
forClass(className).startMethod(method)
@@ -214,10 +206,9 @@ internal class Progress {
214
206
}
215
207
}
216
208
fun finishedAllTests () {
217
- Router .layout?.let { layout ->
218
- for (stale in findStaleSnapshotFiles(layout)) {
219
- deleteFileAndParentDirIfEmpty(layout.snapshotPathForClass(stale))
220
- }
209
+ for (stale in findStaleSnapshotFiles(layout)) {
210
+ val path = layout.snapshotPathForClass(stale)
211
+ deleteFileAndParentDirIfEmpty(path)
221
212
}
222
213
}
223
214
}
0 commit comments