-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track double writes (prepare for literal snapshots). #15
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1fe22b1
Modify `RW` to support `writeonce`.
nedtwigg c74196d
Add a mechanism for tracking writes to disk values.
nedtwigg 93bae96
Merge branch 'main' into feat/track-writes
nedtwigg 6957dd5
Make a test for `recordCall()`. Note that the test is in `runner-juni…
nedtwigg 4afb743
Write a test and (harnessing) that demonstrates `writeonce` and multi…
nedtwigg ffb23dd
recordCall: implementation
jknack ed3a72a
recordCall: implement restOfStack
jknack 0620e08
recordCall: fix JDK 11 build
jknack e16f237
harness: parse exception from gradle build
jknack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/junit5/WriteTracker.kt
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,79 @@ | ||
/* | ||
* Copyright (C) 2023 DiffPlug | ||
* | ||
* 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 | ||
* | ||
* https://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 com.diffplug.selfie.junit5 | ||
|
||
import com.diffplug.selfie.RW | ||
import com.diffplug.selfie.Snapshot | ||
import java.util.stream.Collectors | ||
|
||
/** Represents the line at which user code called into Selfie. */ | ||
data class CallLocation(val subpath: String, val line: Int) : Comparable<CallLocation> { | ||
override fun compareTo(other: CallLocation): Int { | ||
val subpathCompare = subpath.compareTo(other.subpath) | ||
return if (subpathCompare != 0) subpathCompare else line.compareTo(other.line) | ||
} | ||
override fun toString(): String = "$subpath:$line" | ||
} | ||
/** Represents the callstack above a given CallLocation. */ | ||
class CallStack(val location: CallLocation, val restOfStack: List<CallLocation>) | ||
/** Generates a CallLocation and the CallStack behind it. */ | ||
fun recordCall(): CallStack { | ||
val calls = | ||
StackWalker.getInstance().walk { frames -> | ||
frames | ||
.skip(1) | ||
.map { CallLocation(it.className.replace('.', '/') + ".kt", it.lineNumber) } | ||
.collect(Collectors.toList()) | ||
} | ||
return CallStack(calls.removeAt(0), calls) | ||
} | ||
/** The first write at a given spot. */ | ||
internal class FirstWrite<T>(val snapshot: T, val callStack: CallStack) | ||
|
||
/** For tracking the writes of disk snapshots literals. */ | ||
internal open class WriteTracker<K : Comparable<K>, V> { | ||
val writes = mutableMapOf<K, FirstWrite<V>>() | ||
protected fun recordInternal(key: K, snapshot: V, call: CallStack) { | ||
val existing = writes.putIfAbsent(key, FirstWrite(snapshot, call)) | ||
if (existing != null) { | ||
if (existing.snapshot != snapshot) { | ||
throw org.opentest4j.AssertionFailedError( | ||
"Snapshot was set to multiple values:\nfirst time:${existing.callStack}\n\nthis time:${call}", | ||
existing.snapshot, | ||
snapshot) | ||
} else if (RW.isWriteOnce) { | ||
throw org.opentest4j.AssertionFailedError( | ||
"Snapshot was set to the same value multiple times.", existing.callStack, call) | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal class DiskWriteTracker : WriteTracker<String, Snapshot>() { | ||
fun record(key: String, snapshot: Snapshot, call: CallStack) { | ||
recordInternal(key, snapshot, call) | ||
} | ||
} | ||
|
||
class LiteralValue { | ||
// TODO: String, Int, Long, Boolean, etc | ||
} | ||
|
||
internal class InlineWriteTracker : WriteTracker<CallLocation, LiteralValue>() { | ||
fun record(call: CallStack, snapshot: LiteralValue) { | ||
recordInternal(call.location, snapshot, call) | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/DuplicateWriteTest.kt
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,67 @@ | ||
/* | ||
* Copyright (C) 2023 DiffPlug | ||
* | ||
* 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 | ||
* | ||
* https://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 com.diffplug.selfie.junit5 | ||
|
||
import io.kotest.matchers.shouldBe | ||
import kotlin.test.Test | ||
import org.junit.jupiter.api.MethodOrderer | ||
import org.junit.jupiter.api.Order | ||
import org.junit.jupiter.api.TestMethodOrder | ||
import org.junitpioneer.jupiter.DisableIfTestFails | ||
|
||
/** Simplest test for verifying read/write of a snapshot. */ | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation::class) | ||
@DisableIfTestFails | ||
class DuplicateWriteTest : Harness("undertest-junit5") { | ||
@Test @Order(1) | ||
fun noSelfie() { | ||
ut_snapshot().deleteIfExists() | ||
ut_snapshot().assertDoesNotExist() | ||
} | ||
|
||
@Test @Order(2) | ||
fun cannot_write_multiple_things_to_one_snapshot() { | ||
ut_mirror().linesFrom("fun shouldFail()").toFirst("}").uncomment() | ||
ut_mirror().linesFrom("fun shouldPass()").toFirst("}").commentOut() | ||
gradlew("underTest", "-Pselfie=write")?.printStackTrace() | ||
} | ||
|
||
@Test @Order(3) | ||
fun can_write_one_thing_multiple_times_to_one_snapshot() { | ||
ut_mirror().linesFrom("fun shouldFail()").toFirst("}").commentOut() | ||
ut_mirror().linesFrom("fun shouldPass()").toFirst("}").uncomment() | ||
gradlew("underTest", "-Pselfie=write") shouldBe null | ||
} | ||
|
||
@Test @Order(4) | ||
fun can_read_one_thing_multiple_times_from_one_snapshot() { | ||
ut_mirror().linesFrom("fun shouldFail()").toFirst("}").commentOut() | ||
ut_mirror().linesFrom("fun shouldPass()").toFirst("}").uncomment() | ||
gradlew("underTest", "-Pselfie=read") shouldBe null | ||
} | ||
|
||
@Test @Order(5) | ||
fun writeonce_mode() { | ||
ut_mirror().linesFrom("fun shouldFail()").toFirst("}").commentOut() | ||
ut_mirror().linesFrom("fun shouldPass()").toFirst("}").uncomment() | ||
gradlew("underTest", "-Pselfie=writeonce")?.printStackTrace() | ||
} | ||
|
||
@Test @Order(6) | ||
fun deleteSelfie() { | ||
ut_snapshot().deleteIfExists() | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
selfie-runner-junit5/src/test/kotlin/testpkg/RecordCallTest.kt
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,30 @@ | ||
/* | ||
* Copyright (C) 2023 DiffPlug | ||
* | ||
* 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 | ||
* | ||
* https://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 testpkg | ||
|
||
import com.diffplug.selfie.junit5.recordCall | ||
import io.kotest.matchers.ints.shouldBeGreaterThan | ||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
|
||
class RecordCallTest { | ||
@Test | ||
fun testRecordCall() { | ||
val stack = recordCall() | ||
stack.location.toString() shouldBe "testpkg/RecordCallTest.kt:26" | ||
stack.restOfStack.size shouldBeGreaterThan 0 | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
undertest-junit5/src/test/kotlin/undertest/junit5/UT_DuplicateWriteTest.kt
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,15 @@ | ||
package undertest.junit5 | ||
|
||
import com.diffplug.selfie.expectSelfie | ||
import org.junit.jupiter.api.Test | ||
|
||
class UT_DuplicateWriteTest { | ||
// @Test fun shouldFail() { | ||
// expectSelfie("apples").toMatchDisk() | ||
// expectSelfie("oranges").toMatchDisk() | ||
// } | ||
@Test fun shouldPass() { | ||
expectSelfie("twins").toMatchDisk() | ||
expectSelfie("twins").toMatchDisk() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is expected to fail, and in fact it is failing with:
We should make assertions about the error message (e.g. the current error message is not great).
Same thing is true of the
writeonce_mode
test below. It is supposed to fail, and it is, but we are not making any assertions about what the failure should be, which means it's hard for us to have good failure messages.I'm not sure what the best way to make these assertions is. It's easy to catch an exception in the
UT_
build and make the assertions there. The alternative is to parse the JUnit test result and pull the assertions into the "puppetmaster" build. No opinion which way to go, just that the message should have an assertion against it.Once we are making assertions on the error message somehow, this PR is ready to merge, and you can move on to
selfie/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/Selfie.kt
Lines 71 to 75 in 837c2b4