Skip to content

Commit aeb7d72

Browse files
committed
Address PR comments
1 parent 175fc39 commit aeb7d72

File tree

4 files changed

+40
-58
lines changed

4 files changed

+40
-58
lines changed

sentry-android-core/src/test/java/io/sentry/android/core/anr/AnrProfileManagerTest.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,20 @@ package io.sentry.android.core.anr
22

33
import io.sentry.SentryOptions
44
import java.io.File
5-
import java.nio.file.Files
6-
import kotlin.test.AfterTest
75
import kotlin.test.Test
86
import kotlin.test.assertEquals
97
import kotlin.test.assertNotNull
108
import kotlin.test.assertTrue
9+
import org.junit.Rule
10+
import org.junit.rules.TemporaryFolder
1111
import org.mockito.kotlin.mock
1212

1313
class AnrProfileManagerTest {
14-
private lateinit var tempDir: File
15-
16-
@AfterTest
17-
fun cleanup() {
18-
if (::tempDir.isInitialized && tempDir.exists()) {
19-
tempDir.deleteRecursively()
20-
}
21-
}
14+
@get:Rule val tmpDir = TemporaryFolder()
2215

2316
private fun createOptions(): SentryOptions {
24-
tempDir = Files.createTempDirectory("anr_profile_test").toFile()
2517
val options = SentryOptions()
26-
options.cacheDirPath = tempDir.absolutePath
18+
options.cacheDirPath = tmpDir.newFolder().absolutePath
2719
options.setLogger(mock())
2820
return options
2921
}

sentry-android-core/src/test/java/io/sentry/android/core/anr/AnrProfileRotationHelperTest.kt

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,91 @@
11
package io.sentry.android.core.anr
22

33
import java.io.File
4-
import java.nio.file.Files
5-
import kotlin.test.AfterTest
64
import kotlin.test.BeforeTest
75
import kotlin.test.Test
86
import kotlin.test.assertEquals
97
import kotlin.test.assertFalse
108
import kotlin.test.assertTrue
9+
import org.junit.Rule
10+
import org.junit.rules.TemporaryFolder
1111

1212
class AnrProfileRotationHelperTest {
13-
private lateinit var tempDir: File
13+
@get:Rule val tmpDir = TemporaryFolder()
1414

1515
@BeforeTest
1616
fun setup() {
17-
tempDir = Files.createTempDirectory("anr_profile_rotation_test").toFile()
1817
AnrProfileRotationHelper.rotate()
1918
}
2019

21-
@AfterTest
22-
fun cleanup() {
23-
if (::tempDir.isInitialized && tempDir.exists()) {
24-
tempDir.deleteRecursively()
25-
}
26-
}
27-
2820
@Test
2921
fun `getFileForRecording returns file with correct name`() {
30-
val file = AnrProfileRotationHelper.getFileForRecording(tempDir)
22+
val file = AnrProfileRotationHelper.getFileForRecording(tmpDir.root)
3123

3224
assertEquals("anr_profile", file.name)
33-
assertEquals(tempDir, file.parentFile)
25+
assertEquals(tmpDir.root, file.parentFile)
3426
}
3527

3628
@Test
3729
fun `getLastFile returns last file`() {
38-
val file = AnrProfileRotationHelper.getLastFile(tempDir)
30+
val file = AnrProfileRotationHelper.getLastFile(tmpDir.root)
3931

4032
assertEquals("anr_profile_old", file.name)
41-
assertEquals(tempDir, file.parentFile)
33+
assertEquals(tmpDir.root, file.parentFile)
4234
}
4335

4436
@Test
4537
fun `deleteLastFile returns true when file does not exist`() {
46-
val result = AnrProfileRotationHelper.deleteLastFile(tempDir)
38+
val result = AnrProfileRotationHelper.deleteLastFile(tmpDir.root)
4739

4840
assertTrue(result)
4941
}
5042

5143
@Test
5244
fun `deleteLastFile returns true when file is deleted successfully`() {
53-
val lastFile = File(tempDir, "anr_profile_old")
45+
val lastFile = File(tmpDir.root, "anr_profile_old")
5446
lastFile.writeText("test content")
5547
assertTrue(lastFile.exists())
5648

57-
val result = AnrProfileRotationHelper.deleteLastFile(tempDir)
49+
val result = AnrProfileRotationHelper.deleteLastFile(tmpDir.root)
5850

5951
assertTrue(result)
6052
assertFalse(lastFile.exists())
6153
}
6254

6355
@Test
6456
fun `rotate moves current file to last file`() {
65-
val currentFile = File(tempDir, "anr_profile")
57+
val currentFile = File(tmpDir.root, "anr_profile")
6658
currentFile.writeText("current content")
6759

68-
val lastFile = AnrProfileRotationHelper.getLastFile(tempDir)
60+
val lastFile = AnrProfileRotationHelper.getLastFile(tmpDir.root)
6961

7062
assertTrue(lastFile.exists())
7163
assertEquals("current content", lastFile.readText())
7264
}
7365

7466
@Test
7567
fun `rotate deletes existing last file before moving`() {
76-
val currentFile = File(tempDir, "anr_profile")
77-
val lastFile = File(tempDir, "anr_profile_old")
68+
val currentFile = File(tmpDir.root, "anr_profile")
69+
val lastFile = File(tmpDir.root, "anr_profile_old")
7870

7971
lastFile.writeText("last content")
8072
currentFile.writeText("current content")
8173

8274
assertTrue(lastFile.exists())
8375
assertTrue(currentFile.exists())
8476

85-
val newLastFile = AnrProfileRotationHelper.getLastFile(tempDir)
77+
val newLastFile = AnrProfileRotationHelper.getLastFile(tmpDir.root)
8678

8779
assertTrue(newLastFile.exists())
8880
assertEquals("current content", newLastFile.readText())
8981
}
9082

9183
@Test
9284
fun `rotate does not directly perform file renaming`() {
93-
val currentFile = File(tempDir, "anr_profile")
85+
val currentFile = File(tmpDir.root, "anr_profile")
9486
currentFile.writeText("current")
9587

96-
val lastFile = File(tempDir, "anr_profile_old")
88+
val lastFile = File(tmpDir.root, "anr_profile_old")
9789
lastFile.writeText("last")
9890

9991
AnrProfileRotationHelper.rotate()
@@ -103,18 +95,18 @@ class AnrProfileRotationHelperTest {
10395
assertEquals("last", lastFile.readText())
10496

10597
// but once rotated, the last file should now contain the current file's content
106-
AnrProfileRotationHelper.getFileForRecording(tempDir)
98+
AnrProfileRotationHelper.getFileForRecording(tmpDir.root)
10799
assertEquals("current", lastFile.readText())
108100
}
109101

110102
@Test
111103
fun `getFileForRecording triggers rotation when needed`() {
112-
val currentFile = File(tempDir, "anr_profile")
104+
val currentFile = File(tmpDir.root, "anr_profile")
113105
currentFile.writeText("content before rotation")
114106

115-
AnrProfileRotationHelper.getFileForRecording(tempDir)
107+
AnrProfileRotationHelper.getFileForRecording(tmpDir.root)
116108

117-
val lastFile = File(tempDir, "anr_profile_old")
109+
val lastFile = File(tmpDir.root, "anr_profile_old")
118110
assertTrue(lastFile.exists())
119111
assertEquals("content before rotation", lastFile.readText())
120112
}

sentry-android-core/src/test/java/io/sentry/android/core/anr/AnrProfilingIntegrationTest.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
package io.sentry.android.core.anr
22

33
import android.os.SystemClock
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
45
import io.sentry.ILogger
56
import io.sentry.IScopes
67
import io.sentry.SentryIntegrationPackageStorage
78
import io.sentry.SentryOptions
89
import io.sentry.android.core.AppState
910
import io.sentry.android.core.SentryAndroidOptions
1011
import io.sentry.test.getProperty
11-
import java.io.File
12-
import java.nio.file.Files
1312
import kotlin.test.AfterTest
1413
import kotlin.test.BeforeTest
1514
import kotlin.test.Test
1615
import kotlin.test.assertEquals
1716
import kotlin.test.assertFalse
1817
import kotlin.test.assertNotNull
1918
import kotlin.test.assertTrue
19+
import org.junit.Rule
20+
import org.junit.rules.TemporaryFolder
2021
import org.junit.runner.RunWith
2122
import org.mockito.kotlin.mock
22-
import org.robolectric.RobolectricTestRunner
2323

24-
@RunWith(RobolectricTestRunner::class)
24+
@RunWith(AndroidJUnit4::class)
2525
class AnrProfilingIntegrationTest {
26-
private lateinit var tempDir: File
26+
27+
@get:Rule val tmpDir = TemporaryFolder()
28+
2729
private lateinit var mockScopes: IScopes
2830
private lateinit var mockLogger: ILogger
2931
private lateinit var options: SentryAndroidOptions
3032

3133
@BeforeTest
3234
fun setup() {
33-
tempDir = Files.createTempDirectory("anr_profile_test").toFile()
3435
mockScopes = mock()
3536
mockLogger = mock()
3637
options =
3738
SentryAndroidOptions().apply {
38-
cacheDirPath = tempDir.absolutePath
39+
cacheDirPath = tmpDir.root.absolutePath
3940
setLogger(mockLogger)
4041
isEnableAnrProfiling = true
4142
}
@@ -44,9 +45,6 @@ class AnrProfilingIntegrationTest {
4445

4546
@AfterTest
4647
fun cleanup() {
47-
if (::tempDir.isInitialized && tempDir.exists()) {
48-
tempDir.deleteRecursively()
49-
}
5048
AppState.getInstance().resetInstance()
5149
}
5250

@@ -163,7 +161,7 @@ class AnrProfilingIntegrationTest {
163161

164162
val androidOptions =
165163
SentryAndroidOptions().apply {
166-
cacheDirPath = tempDir.absolutePath
164+
cacheDirPath = tmpDir.root.absolutePath
167165
setLogger(mockLogger)
168166
isEnableAnrProfiling = true
169167
}
@@ -199,7 +197,7 @@ class AnrProfilingIntegrationTest {
199197

200198
val androidOptions =
201199
SentryAndroidOptions().apply {
202-
cacheDirPath = tempDir.absolutePath
200+
cacheDirPath = tmpDir.root.absolutePath
203201
setLogger(mockLogger)
204202
isEnableAnrProfiling = true
205203
}
@@ -220,7 +218,7 @@ class AnrProfilingIntegrationTest {
220218
fun `does not register when options is not SentryAndroidOptions`() {
221219
val plainOptions =
222220
SentryOptions().apply {
223-
cacheDirPath = tempDir.absolutePath
221+
cacheDirPath = tmpDir.root.absolutePath
224222
setLogger(mockLogger)
225223
}
226224

@@ -243,7 +241,7 @@ class AnrProfilingIntegrationTest {
243241
fun `does not register when ANR profiling is disabled`() {
244242
val androidOptions =
245243
SentryAndroidOptions().apply {
246-
cacheDirPath = tempDir.absolutePath
244+
cacheDirPath = tmpDir.root.absolutePath
247245
setLogger(mockLogger)
248246
isEnableAnrProfiling = false
249247
}
@@ -263,7 +261,7 @@ class AnrProfilingIntegrationTest {
263261
fun `registers when ANR profiling is enabled`() {
264262
val androidOptions =
265263
SentryAndroidOptions().apply {
266-
cacheDirPath = tempDir.absolutePath
264+
cacheDirPath = tmpDir.root.absolutePath
267265
setLogger(mockLogger)
268266
isEnableAnrProfiling = true
269267
}

sentry-android-distribution/src/test/java/io/sentry/android/distribution/UpdateResponseParserTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package io.sentry.android.distribution
22

3+
import androidx.test.ext.junit.runners.AndroidJUnit4
34
import io.sentry.SentryOptions
45
import io.sentry.UpdateStatus
56
import org.junit.Assert.assertEquals
67
import org.junit.Assert.assertTrue
78
import org.junit.Before
89
import org.junit.Test
910
import org.junit.runner.RunWith
10-
import org.robolectric.RobolectricTestRunner
1111

12-
@RunWith(RobolectricTestRunner::class)
12+
@RunWith(AndroidJUnit4::class)
1313
class UpdateResponseParserTest {
1414

1515
private lateinit var options: SentryOptions

0 commit comments

Comments
 (0)