-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathSystemProps.common.kt
76 lines (68 loc) · 2.81 KB
/
SystemProps.common.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
74
75
76
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:JvmName("SystemPropsKt")
@file:JvmMultifileClass
package kotlinx.coroutines.internal
import kotlin.jvm.*
/**
* Gets the system property indicated by the specified [property name][propertyName],
* or returns [defaultValue] if there is no property with that key.
*
* **Note: this function should be used in JVM tests only, other platforms use the default value.**
*/
internal fun systemProp(
propertyName: String,
defaultValue: Boolean
): Boolean = systemProp(propertyName)?.toBoolean() ?: defaultValue
/**
* Gets the system property indicated by the specified [property name][propertyName],
* or returns [defaultValue] if there is no property with that key. It also checks that the result
* is between [minValue] and [maxValue] (inclusively), throws [IllegalStateException] if it is not.
*
* **Note: this function should be used in JVM tests only, other platforms use the default value.**
*/
internal fun systemProp(
propertyName: String,
defaultValue: Int,
minValue: Int = 1,
maxValue: Int = Int.MAX_VALUE
): Int = systemProp(propertyName, defaultValue.toLong(), minValue.toLong(), maxValue.toLong()).toInt()
/**
* Gets the system property indicated by the specified [property name][propertyName],
* or returns [defaultValue] if there is no property with that key. It also checks that the result
* is between [minValue] and [maxValue] (inclusively), throws [IllegalStateException] if it is not.
*
* **Note: this function should be used in JVM tests only, other platforms use the default value.**
*/
internal fun systemProp(
propertyName: String,
defaultValue: Long,
minValue: Long = 1,
maxValue: Long = Long.MAX_VALUE
): Long {
val value = systemProp(propertyName) ?: return defaultValue
val parsed = value.toLongOrNull()
?: error("System property '$propertyName' has unrecognized value '$value'")
if (parsed !in minValue..maxValue) {
error("System property '$propertyName' should be in range $minValue..$maxValue, but is '$parsed'")
}
return parsed
}
/**
* Gets the system property indicated by the specified [property name][propertyName],
* or returns [defaultValue] if there is no property with that key.
*
* **Note: this function should be used in JVM tests only, other platforms use the default value.**
*/
internal fun systemProp(
propertyName: String,
defaultValue: String
): String = systemProp(propertyName) ?: defaultValue
/**
* Gets the system property indicated by the specified [property name][propertyName],
* or returns `null` if there is no property with that key.
*
* **Note: this function should be used in JVM tests only, other platforms use the default value.**
*/
internal expect fun systemProp(propertyName: String): String?