|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package aws.smithy.kotlin.runtime.testing |
| 6 | + |
| 7 | +import java.util.* |
| 8 | + |
| 9 | +@PublishedApi |
| 10 | +internal val editableEnvVars: MutableMap<String, String> by lazy { |
| 11 | + val systemEnv = System.getenv() |
| 12 | + val classOfMap = systemEnv::class.java |
| 13 | + |
| 14 | + @Suppress("UNCHECKED_CAST") |
| 15 | + classOfMap |
| 16 | + .getDeclaredField("m") |
| 17 | + .apply { isAccessible = true } |
| 18 | + .get(systemEnv) as MutableMap<String, String> |
| 19 | +} |
| 20 | + |
| 21 | +@PublishedApi |
| 22 | +internal fun <K, V> MutableMap<K, V>.setAll(newEntries: Map<K, V>) { |
| 23 | + clear() |
| 24 | + putAll(newEntries) |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Runs the given [block] with modified environment variables. The variables given in [newVars] are merged onto the |
| 29 | + * existing environment variables, adding values that did not exist previously and updating values which did exist. |
| 30 | + * After the block completes, environment variables will be restored to their previous values. This method may be nested |
| 31 | + * multiple times to apply/restore hierarchical overrides. |
| 32 | + * |
| 33 | + * **Caution**: This method is not thread-safe as environment variables are global for a JVM instance. |
| 34 | + * |
| 35 | + * # How it works |
| 36 | + * |
| 37 | + * Normally environment variables cannot be modified within a JVM instance so this function makes use of private |
| 38 | + * reflection to gain access to the underlying mutable map used by [System.getenv]. At runtime, this requires |
| 39 | + * `--add-opens=java.base/java.util=ALL-UNNAMED` to be passed to the `java` executable on JDK 9+. For instance: |
| 40 | + * |
| 41 | + * ```sh |
| 42 | + * java \ |
| 43 | + * -jar my-testing-jar \ |
| 44 | + * -classpath testing-jvm.X.Y.Z.jar,... \ |
| 45 | + * --add-opens=java.base/java.util=ALL-UNNAMED |
| 46 | + * ``` |
| 47 | + * |
| 48 | + * In **smithy-kotlin** and **aws-sdk-kotlin** this is handled in the root scripts by adding a JVM arg to all JVM test |
| 49 | + * JARs (whether they rely on this module or not). |
| 50 | + * |
| 51 | + * @param newVars The new environment variables to merge onto the existing environment variables |
| 52 | + * @param block The block to execute with updated environment variables |
| 53 | + */ |
| 54 | +public suspend inline fun <T> withEnvVars( |
| 55 | + newVars: Map<String, String>, |
| 56 | + crossinline block: suspend () -> T, |
| 57 | +): T { |
| 58 | + val originalVars = System.getenv() |
| 59 | + editableEnvVars.setAll(originalVars + newVars) |
| 60 | + return try { |
| 61 | + block() |
| 62 | + } finally { |
| 63 | + editableEnvVars.setAll(originalVars) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Runs the given [block] with modified JVM system properties. The properties given in [newProps] are merged onto the |
| 69 | + * existing system properties, adding values that did not exist previously and updating values which did exist. After |
| 70 | + * the block completes, system properties will be restored to their previous values. This method may be nested multiple |
| 71 | + * times to apply/restore hierarchical overrides. |
| 72 | + * |
| 73 | + * **Caution**: This method is not thread-safe as system properties are global for a JVM instance. |
| 74 | + * |
| 75 | + * @param newProps The new system properties to merge onto the existing system properties. |
| 76 | + * @param block The block to execute with updated system properties |
| 77 | + */ |
| 78 | +public suspend inline fun <T> withSystemProperties( |
| 79 | + newProps: Map<String, String>, |
| 80 | + crossinline block: suspend () -> T, |
| 81 | +): T { |
| 82 | + val originalProps = System.getProperties() |
| 83 | + val replacementProps = Properties().apply { |
| 84 | + putAll(originalProps) |
| 85 | + putAll(newProps) |
| 86 | + } |
| 87 | + System.setProperties(replacementProps) |
| 88 | + return try { |
| 89 | + block() |
| 90 | + } finally { |
| 91 | + System.setProperties(originalProps) |
| 92 | + } |
| 93 | +} |
0 commit comments