[platform test framework] Change the registry value type to String

Merge-request: IJ-MR-152137
Merged-by: Shumaf Lovpache <soarex16@gmail.com>

GitOrigin-RevId: 9826a6f8794a042e7f8e9067629ae381b14cbae3
This commit is contained in:
Shumaf Lovpache
2025-01-14 18:52:20 +00:00
committed by intellij-monorepo-bot
parent 955c17be31
commit fbb8c1be9b
3 changed files with 63 additions and 34 deletions
@@ -0,0 +1,16 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.testFramework.junit5
import com.intellij.testFramework.junit5.impl.RegistryKeyExtension
import org.jetbrains.annotations.TestOnly
import org.junit.jupiter.api.extension.ExtendWith
/**
* Sets the [value] for the Registry [key], runs, and reverts to previous value.
* @see com.intellij.openapi.util.registry.RegistryValue.setValue
*/
@TestOnly
@Repeatable
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@ExtendWith(RegistryKeyExtension::class)
annotation class RegistryKey(val key: String, val value: String)
@@ -1,34 +0,0 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.testFramework.junit5
import com.intellij.openapi.util.registry.Registry
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext
/**
* Sets the [value] for the Registry [key], runs, and reverts to previous value.
*
* Boolean keys are supported currently.
* TODO change the [value] type to [String] when needed.
*
* @see com.intellij.openapi.util.registry.RegistryValue.setValue
*/
class RegistryKeyExtension(
key: String,
private val value: Boolean,
) : BeforeEachCallback,
AfterEachCallback {
private val registryValue = Registry.get(key)
private var previous: Boolean = false
override fun beforeEach(context: ExtensionContext?) {
previous = registryValue.asBoolean()
registryValue.setValue(value)
}
override fun afterEach(context: ExtensionContext?) {
registryValue.setValue(previous)
}
}
@@ -0,0 +1,47 @@
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.testFramework.junit5.impl
import com.intellij.openapi.util.registry.Registry
import com.intellij.testFramework.junit5.RegistryKey
import org.jetbrains.annotations.TestOnly
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.InvocationInterceptor
import org.junit.platform.commons.support.AnnotationSupport
@TestOnly
internal class RegistryKeyExtension : AbstractInvocationInterceptor() {
override fun <T> intercept(invocation: InvocationInterceptor.Invocation<T>, context: ExtensionContext): T {
val annotations = AnnotationSupport.findRepeatableAnnotations(context.testClass, RegistryKey::class.java) +
AnnotationSupport.findRepeatableAnnotations(context.element, RegistryKey::class.java)
if (annotations.isEmpty()) {
return invocation.proceed()
}
val valuesBefore = annotations.map { annotation ->
Pair(annotation, annotation.setValue())
}
try {
return invocation.proceed()
}
finally {
for ((annotation, previousValue) in valuesBefore.asReversed()) {
annotation.resetValue(previousValue)
}
}
}
private fun RegistryKey.setValue(): String? = Registry.Companion.get(key).run {
val previousValue = Registry.Companion.stringValue(key)
setValue(value)
previousValue
}
private fun RegistryKey.resetValue(previousValue: String?): Unit = Registry.Companion.get(key).run {
if (previousValue == null) {
resetToDefault()
}
else {
setValue(previousValue)
}
}
}