diff --git a/json/tests/test/com/intellij/json/JsonIconsAstLoadingTest.kt b/json/tests/test/com/intellij/json/JsonIconsAstLoadingTest.kt new file mode 100644 index 000000000000..405e1a31a784 --- /dev/null +++ b/json/tests/test/com/intellij/json/JsonIconsAstLoadingTest.kt @@ -0,0 +1,37 @@ +// 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.json + +import com.intellij.openapi.util.registry.Registry +import com.intellij.testFramework.fixtures.BasePlatformTestCase +import com.intellij.util.AstLoadingFilter +import com.intellij.util.PsiIconUtil +import com.intellij.util.ThrowableRunnable +import java.util.function.Supplier + +class JsonIconsAstLoadingTest : BasePlatformTestCase() { + + override fun setUp() { + super.setUp() + + Registry.get("ast.loading.filter").setValue(true, testRootDisposable); + } + + /** + * If this test fails for your [com.intellij.ide.IconProvider] you MUST avoid loading PSI by either: + * - indexing and accessing index instead from [com.intellij.ide.IconProvider]; + * - cached computation on AST, e.g., using [com.intellij.util.gist.GistAstMarker]. + */ + fun testNoAstLoadedFromIconProviders() { + val file = myFixture.addFileToProject("package.json", """ + { + "name": "my-awesome-package", + "version": "1.0.0", + "author": "Your Name " + } + """.trimIndent()) + + AstLoadingFilter.disallowTreeLoading(ThrowableRunnable { + PsiIconUtil.getIconFromProviders(file, 0) + }, Supplier { "IconProvider must not access PSI of files directly! Use either indexes or GistManager to cache computation" }) + } +} \ No newline at end of file diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/YamlIconsAstLoadingTest.kt b/plugins/yaml/testSrc/org/jetbrains/yaml/YamlIconsAstLoadingTest.kt new file mode 100644 index 000000000000..a8d04bec6255 --- /dev/null +++ b/plugins/yaml/testSrc/org/jetbrains/yaml/YamlIconsAstLoadingTest.kt @@ -0,0 +1,34 @@ +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.yaml + +import com.intellij.openapi.util.registry.Registry +import com.intellij.testFramework.fixtures.BasePlatformTestCase +import com.intellij.util.AstLoadingFilter +import com.intellij.util.PsiIconUtil +import com.intellij.util.ThrowableRunnable +import java.util.function.Supplier + +class YamlIconsAstLoadingTest : BasePlatformTestCase() { + + override fun setUp() { + super.setUp() + + Registry.get("ast.loading.filter").setValue(true, testRootDisposable); + } + + /** + * If this test fails for your [com.intellij.ide.IconProvider] you MUST avoid loading PSI by either: + * - indexing and accessing index instead from [com.intellij.ide.IconProvider]; + * - cached computation on AST, e.g., using [com.intellij.util.gist.GistAstMarker]. + */ + fun testNoAstLoadedFromIconProviders() { + val file = myFixture.addFileToProject("deployment.yaml", """ + apiVersion: apps/v1 + kind: Pod + """.trimIndent()) + + AstLoadingFilter.disallowTreeLoading(ThrowableRunnable { + PsiIconUtil.getIconFromProviders(file, 0) + }, Supplier { "IconProvider must not access PSI of files directly! Use either indexes or GistManager to cache computation" }) + } +} \ No newline at end of file