Python tests: move loadEnvs to the framework module to lose coupling.

Python tests module is for tests only.
Other modules shouldn't depend on it, they should depend on the framework module instead.

GitOrigin-RevId: 51bda8339ab8197f49bab6e9350e0334fe2a3ae8
This commit is contained in:
Ilya.Kazakevich
2024-12-15 15:46:47 +00:00
committed by intellij-monorepo-bot
parent 043a72ae26
commit 30b1a110eb
5 changed files with 43 additions and 27 deletions
@@ -0,0 +1,29 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.jetbrains.python.tools
import com.intellij.openapi.diagnostic.fileLogger
import com.jetbrains.python.sdk.Directory
import java.io.IOException
import java.nio.file.Path
import kotlin.io.path.isDirectory
import kotlin.io.path.readLines
private const val TAGS_FILE = "tags.txt"
/**
* Loads [TAGS_FILE] from [pythonEnv] which should be a path to a python installation (either binary or directory) created by
* `community/python/setup-test-environment`.
*
* Returns set of tags i.e. `django` if interpreter has django.
*/
fun loadEnvTags(pythonEnv: Path): Set<String> {
val envDir: Directory = if (pythonEnv.isDirectory()) pythonEnv else pythonEnv.parent
val tagsFile = envDir.resolve(TAGS_FILE)
try {
return tagsFile.readLines().toSet()
}
catch (e: IOException) {
fileLogger().warn("Can't read $tagsFile", e)
return emptySet()
}
}
+3 -2
View File
@@ -13,7 +13,6 @@ import com.intellij.util.containers.ContainerUtil;
import com.jetbrains.LoggingRule;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.sdk.PySdkUtil;
import com.jetbrains.python.sdk.PythonSdkUtil;
import com.jetbrains.python.sdk.VirtualEnvReader;
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor;
import com.jetbrains.python.tools.sdkTools.PySdkTools;
@@ -25,6 +24,8 @@ import java.io.File;
import java.nio.file.Path;
import java.util.*;
import static com.jetbrains.python.tools.EnvTagsKt.loadEnvTags;
public class PyEnvTaskRunner {
private static final Logger LOG = Logger.getInstance(PyEnvTaskRunner.class);
private final List<String> myRoots;
@@ -177,7 +178,7 @@ public class PyEnvTaskRunner {
EnvInfo(@NotNull String root) {
this.root = root;
tags = PyEnvTestCase.loadEnvTags(root);
tags = loadEnvTags(Path.of(root)).stream().toList();
pythonVersion = ContainerUtil.find(tags, tag -> tag.matches("^python\\d\\.\\d+$"));
}
+6 -22
View File
@@ -18,25 +18,25 @@ import org.junit.rules.TestName;
import org.junit.rules.TestWatcher;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.*;
import static com.intellij.testFramework.assertions.Assertions.assertThat;
import static com.jetbrains.python.tools.EnvTagsKt.loadEnvTags;
/**
* <p>
* All inheritors must be in {@link com.jetbrains.env}.*
* <p>
* See "community/python/setup-test-environment/build.gradle"
* {@link com.jetbrains.env.python.api.EnvTagsKt#loadEnvTags(Path)}
*
* @author traff
*/
public abstract class PyEnvTestCase {
private static final Logger LOG = Logger.getInstance(PyEnvTestCase.class.getName());
private static final String TAGS_FILE = "tags.txt";
@NotNull
protected static final PyEnvTestSettings SETTINGS = PyEnvTestSettings.Companion.fromEnvVariables();
@@ -66,7 +66,8 @@ public abstract class PyEnvTestCase {
@Rule public TestName myTestName = new TestName();
@Rule public final TestWatcher myWatcher = new TestWatcher(){};
@Rule public final TestWatcher myWatcher = new TestWatcher() {
};
static {
LOG.info("Using following config\n" + SETTINGS.reportConfiguration());
@@ -120,7 +121,7 @@ public abstract class PyEnvTestCase {
@BeforeClass
public static void collectTagsForEnvs() {
for (final String pythonRoot : getDefaultPythonRoots()) {
envTags.put(pythonRoot, loadEnvTags(pythonRoot));
envTags.put(pythonRoot, loadEnvTags(Path.of(pythonRoot)).stream().toList());
}
}
@@ -239,23 +240,6 @@ public abstract class PyEnvTestCase {
return getDefaultPythonRoots();
}
public static List<String> loadEnvTags(String env) {
List<String> envTags;
try {
File parent = new File(env);
if (parent.isFile()) {
parent = parent.getParentFile();
}
envTags = com.intellij.openapi.util.io.FileUtil.loadLines(new File(parent, TAGS_FILE));
}
catch (IOException e) {
envTags = new ArrayList<>();
}
return envTags;
}
private final Disposable myDisposable = Disposer.newDisposable();
public Disposable getTestRootDisposable() {
@@ -8,9 +8,11 @@ import com.intellij.testFramework.UsefulTestCase;
import com.jetbrains.env.PyEnvTestCase;
import org.junit.Test;
import java.nio.file.Path;
import java.util.*;
import static com.intellij.testFramework.UsefulTestCase.assertEmpty;
import static com.jetbrains.python.tools.EnvTagsKt.loadEnvTags;
public class PyEnvSufficiencyTest extends PyEnvTestCase {
private static final List<String> BASE_TAGS =
@@ -26,7 +28,7 @@ public class PyEnvSufficiencyTest extends PyEnvTestCase {
return; // not on env agent
}
for (String root : roots) {
tags.addAll(loadEnvTags(root));
tags.addAll(loadEnvTags(Path.of(root)));
}
List<String> missing = new ArrayList<>();
+2 -2
View File
@@ -7,7 +7,6 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
import com.jetbrains.env.PyEnvTestCase
import com.jetbrains.env.PyEnvTestSettings
import com.jetbrains.python.failure
import com.jetbrains.python.packaging.findCondaExecutableRelativeToEnv
@@ -17,6 +16,7 @@ import com.jetbrains.python.sdk.conda.TargetEnvironmentRequestCommandExecutor
import com.jetbrains.python.sdk.flavors.PythonSdkFlavor
import com.jetbrains.python.sdk.flavors.conda.PyCondaEnv
import com.jetbrains.python.sdk.flavors.conda.PyCondaEnvIdentity
import com.jetbrains.python.tools.loadEnvTags
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
@@ -125,7 +125,7 @@ sealed class PythonType<T : Any>(private val tag: @NonNls String) {
@RequiresBackgroundThread
private fun typeMatchesEnv(env: Path, vararg additionalTags: @NonNls String): Boolean {
val envTags = PyEnvTestCase.loadEnvTags(env.toString())
val envTags = loadEnvTags(env)
for (badTag in PythonType::class.sealedSubclasses.filterNot { it.isInstance(this) }.map { it.objectInstance!!.tag }) {
if (badTag in envTags) return false