-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
SentryFileWriterTest.kt
73 lines (62 loc) · 2.2 KB
/
SentryFileWriterTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package io.sentry.instrumentation.file
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.sentry.IHub
import io.sentry.SentryOptions
import io.sentry.SentryTracer
import io.sentry.SpanStatus.OK
import io.sentry.TransactionContext
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals
class SentryFileWriterTest {
class Fixture {
val hub = mock<IHub>()
lateinit var sentryTracer: SentryTracer
internal fun getSut(
tmpFile: File,
activeTransaction: Boolean = true,
append: Boolean = false
): SentryFileWriter {
whenever(hub.options).thenReturn(SentryOptions())
sentryTracer = SentryTracer(TransactionContext("name", "op"), hub)
if (activeTransaction) {
whenever(hub.span).thenReturn(sentryTracer)
}
return SentryFileWriter(tmpFile, append, hub)
}
}
@get:Rule
val tmpDir = TemporaryFolder()
private val fixture = Fixture()
private val tmpFile: File by lazy { tmpDir.newFile("test.txt") }
@Test
fun `captures a span`() {
val writer = fixture.getSut(tmpFile)
writer.write("TEXT")
writer.close()
assertEquals(fixture.sentryTracer.children.size, 1)
val fileIOSpan = fixture.sentryTracer.children.first()
assertEquals(fileIOSpan.spanContext.operation, "file.write")
assertEquals(fileIOSpan.spanContext.description, "test.txt (4 B)")
assertEquals(fileIOSpan.data["file.size"], 4L)
assertEquals(fileIOSpan.throwable, null)
assertEquals(fileIOSpan.isFinished, true)
assertEquals(fileIOSpan.status, OK)
}
@Test
fun `append works`() {
val writer1 = fixture.getSut(tmpFile, append = true)
writer1.use {
it.append("test")
}
// second writer should not overwrite the file contents, but append to the existing content
val writer2 = fixture.getSut(tmpFile, append = true)
writer2.use {
it.append("test2")
}
assertEquals("testtest2", tmpFile.readText())
}
}