23
23
24
24
package org.ossreviewtoolkit.utils.test
25
25
26
+ import io.kotest.extensions.system.OverrideMode
27
+ import io.kotest.extensions.system.OverrideMode.SetOrError
28
+
26
29
import java.lang.reflect.Field
27
30
31
+ /* *
32
+ * Modifies System Environment with chosen key and value
33
+ *
34
+ * This is a helper function for code that uses Environment Variables. It changes the specific [key] from [System.getenv]
35
+ * with the specified [value], only during the execution of [block].
36
+ *
37
+ * To do this, this function uses a trick that makes the System Environment editable, and changes [key]. Any previous
38
+ * environment (anything not overridden) will also be in the environment. If the chosen key is in the environment,
39
+ * it will be changed according to [mode]. If the chosen key is not in the environment, it will be included.
40
+ *
41
+ * After the execution of [block], the environment is set to what it was before.
42
+ *
43
+ * **ATTENTION**: This code is susceptible to race conditions. If you attempt to change the environment while it was
44
+ * already changed, the result is inconsistent, as the System Environment Map is a single map.
45
+ */
46
+ inline fun <T > withEnvironment (key : String , value : String? , mode : OverrideMode = SetOrError , block : () -> T ): T {
47
+ return withEnvironment(key to value, mode, block)
48
+ }
49
+
50
+ /* *
51
+ * Modifies System Environment with chosen key and value
52
+ *
53
+ * This is a helper function for code that uses Environment Variables. It changes the specific key from [System.getenv]
54
+ * with the specified value, only during the execution of [block].
55
+ *
56
+ * To do this, this function uses a trick that makes the System Environment editable, and changes key. Any previous
57
+ * environment (anything not overridden) will also be in the environment. If the chosen key is in the environment,
58
+ * it will be changed according to [mode]. If the chosen key is not in the environment, it will be included.
59
+ *
60
+ * After the execution of [block], the environment is set to what it was before.
61
+ *
62
+ * **ATTENTION**: This code is susceptible to race conditions. If you attempt to change the environment while it was
63
+ * already changed, the result is inconsistent, as the System Environment Map is a single map.
64
+ */
65
+ inline fun <T > withEnvironment (environment : Pair <String , String ?>, mode : OverrideMode = SetOrError , block : () -> T ): T {
66
+ return withEnvironment(mapOf (environment), mode, block)
67
+ }
68
+
28
69
/* *
29
70
* Modifies System Environment with chosen keys and values
30
71
*
@@ -33,22 +74,22 @@ import java.lang.reflect.Field
33
74
*
34
75
* To do this, this function uses a trick that makes the System Environment editable, and changes key. Any previous
35
76
* environment (anything not overridden) will also be in the environment. If the chosen key is in the environment,
36
- * it will be overridden . If the chosen key is not in the environment, it will be included.
77
+ * it will be changed according to [mode] . If the chosen key is not in the environment, it will be included.
37
78
*
38
79
* After the execution of [block], the environment is set to what it was before.
39
80
*
40
81
* **ATTENTION**: This code is susceptible to race conditions. If you attempt to change the environment while it was
41
82
* already changed, the result is inconsistent, as the System Environment Map is a single map.
42
83
*/
43
- inline fun <T > withEnvironment (environment : Map <String , String ?>, block : () -> T ): T {
84
+ inline fun <T > withEnvironment (environment : Map <String , String ?>, mode : OverrideMode = SetOrError , block : () -> T ): T {
44
85
val isWindows = " windows" in System .getProperty(" os.name" ).orEmpty().lowercase()
45
86
val originalEnvironment = if (isWindows) {
46
87
System .getenv().toSortedMap(String .CASE_INSENSITIVE_ORDER )
47
88
} else {
48
89
System .getenv().toMap()
49
90
}
50
91
51
- setEnvironmentMap(setOrOverride (originalEnvironment, environment))
92
+ setEnvironmentMap(mode. override (originalEnvironment, environment))
52
93
53
94
try {
54
95
return block()
@@ -103,6 +144,3 @@ private fun MutableMap<String, String>.putReplacingNulls(map: Map<String, String
103
144
if (value == null ) remove(key) else put(key, value)
104
145
}
105
146
}
106
-
107
- fun setOrOverride (originalValues : Map <String , String >, newValues : Map <String , String ?>) =
108
- originalValues.toMutableMap().apply { putReplacingNulls(newValues) }
0 commit comments