Skip to content

Commit 27128e8

Browse files
#430: Introduce withFakeRoot helper method
1 parent 49480b8 commit 27128e8

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

core/linux/test/TimeZoneNativeTest.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,25 @@ import kotlin.test.assertTrue
1616
class TimeZoneNativeTest {
1717

1818
@Test
19-
fun correctSymlinkTest() {
20-
root = "${RESOURCES}correct-symlink/"
21-
19+
fun correctSymlinkTest() = withFakeRoot("${RESOURCES}correct-symlink/") {
2220
val tz = TimeZone.currentSystemDefault()
2321
assertEquals(TimeZone.of("Europe/Oslo"), tz)
2422
}
2523

2624
@Test
27-
fun correctLocaltimeCopyTest() {
28-
root = "${RESOURCES}correct-localtime-copy/"
29-
25+
fun correctLocaltimeCopyTest() = withFakeRoot("${RESOURCES}correct-localtime-copy/") {
3026
val tz = TimeZone.currentSystemDefault()
3127
assertEquals(TimeZone.of("Europe/Oslo"), tz)
3228
}
3329

3430
@Test
35-
fun fallsBackToUTC() {
36-
root = "${RESOURCES}missing-localtime/"
37-
31+
fun fallsBackToUTC() = withFakeRoot("${RESOURCES}falls-back-to-utc/") {
3832
val tz = TimeZone.currentSystemDefault()
3933
assertEquals(TimeZone.UTC, tz)
4034
}
4135

4236
@Test
43-
fun missingTimezoneTest() {
44-
root = "${RESOURCES}missing-timezone/"
45-
37+
fun missingTimezoneTest() = withFakeRoot("${RESOURCES}missing-timezone/") {
4638
val exception = assertFailsWith<IllegalTimeZoneException> {
4739
TimeZone.currentSystemDefault()
4840
}
@@ -54,9 +46,7 @@ class TimeZoneNativeTest {
5446
}
5547

5648
@Test
57-
fun incorrectTimezoneTest() {
58-
root = "${RESOURCES}incorrect-timezone/"
59-
49+
fun incorrectTimezoneTest() = withFakeRoot("${RESOURCES}incorrect-timezone/") {
6050
val exception = assertFailsWith<IllegalTimeZoneException> {
6151
TimeZone.currentSystemDefault()
6252
}
@@ -68,9 +58,7 @@ class TimeZoneNativeTest {
6858
}
6959

7060
@Test
71-
fun differentTimezonesTest() {
72-
root = "${RESOURCES}different-timezones/"
73-
61+
fun differentTimezonesTest() = withFakeRoot("${RESOURCES}different-timezones/") {
7462
val exception = assertFailsWith<IllegalTimeZoneException> {
7563
TimeZone.currentSystemDefault()
7664
}
@@ -83,5 +71,15 @@ class TimeZoneNativeTest {
8371

8472
companion object {
8573
const val RESOURCES = "./linux/test/time-zone-native-test-resources/"
74+
75+
private fun withFakeRoot(fakeRoot: String, action: () -> Unit) {
76+
val defaultRoot = root
77+
root = fakeRoot
78+
try {
79+
action()
80+
} finally {
81+
root = defaultRoot
82+
}
83+
}
8684
}
8785
}

0 commit comments

Comments
 (0)