Pycharm: Only use Py3 Conda in JUnit5 tests.

No need to test against Py2 in 2024, and conda is a different beast and should be tested separatelly, so we use tags to filter "wrong" pythons. See `api.kt`

GitOrigin-RevId: 2f47f5aca3c8cd9d12684e425796aedaee5d239f
This commit is contained in:
Ilya.Kazakevich
2024-07-23 21:47:40 +02:00
committed by intellij-monorepo-bot
parent 12b33d2717
commit 06d28837eb
4 changed files with 40 additions and 17 deletions

View File

@@ -228,7 +228,7 @@ public abstract class PyEnvTestCase {
}
public static List<String> getDefaultPythonRoots() {
return ContainerUtil.map(SETTINGS.getPythons(), File::getAbsolutePath);
return ContainerUtil.map(SETTINGS.getPythons$intellij_python_community_tests(), File::getAbsolutePath);
}
/**

View File

@@ -6,7 +6,6 @@ import com.intellij.util.SystemProperties
import org.jetbrains.annotations.ApiStatus
import org.jetbrains.annotations.TestOnly
import java.io.File
import java.nio.file.Path
//TODO: Use Konfig instead?
@@ -29,7 +28,8 @@ internal data class PyEnvTestSettings(
/**
* Paths to all existing python SDKs
*/
val pythons: List<File> = foldersWithPythons
@TestOnly
internal val pythons: List<File> = foldersWithPythons
.filter(File::exists)
.flatMap { it.listFiles()?.toList() ?: emptyList() }
.filter { !it.name.startsWith('.') }
@@ -126,10 +126,3 @@ enum class PyTestEnvVars(private val getVarName: (PyTestEnvVars) -> String = { i
fun isSet() = getValue() != null
}
/**
* Returns first python installed with gradle script to the dir with env variable (see [PyEnvTestSettings])
*/
@TestOnly
fun getPython(): Result<Path> = PyEnvTestSettings.fromEnvVariables().pythons.firstOrNull()?.toPath()?.let { Result.success(it) }
?: Result.failure(Throwable("No python found. See ${PyEnvTestSettings::class} class for more info"))

View File

@@ -35,16 +35,17 @@ class PySDKRule(private val targetConfigProducer: (() -> TargetEnvironmentConfig
companion object {
/**
* Creates sdk (possible on [targetConfig])
* Creates sdk (possible on [targetConfig]).
* If [detectSystemSdk] tries to use system python first, then uses one created by [PyEnvTestSettings]
*/
suspend fun createSdk(targetConfig: TargetEnvironmentConfiguration?): Sdk = withContext(Dispatchers.IO) {
suspend fun createSdk(targetConfig: TargetEnvironmentConfiguration?, detectSystemSdk: Boolean): Sdk = withContext(Dispatchers.IO) {
val (pythonPath, additionalData) = if (targetConfig == null) {
// Local
val flavor = if (SystemInfo.isWindows) WinPythonSdkFlavor() else UnixPythonSdkFlavor.getInstance()
val pythonPath = flavor.suggestLocalHomePaths(null, null).firstOrNull()
?: PyEnvTestSettings.fromEnvVariables().pythons.firstOrNull()?.toPath()?.let { PythonSdkUtil.getPythonExecutable(it.toString()); }
Assume.assumeNotNull("No python found on local installation", pythonPath)
Pair(pythonPath!!.toString(), PythonSdkAdditionalData(PyFlavorAndData(PyFlavorData.Empty, flavor)))
val pythonPath = if (detectSystemSdk) flavor.suggestLocalHomePaths(null, null).firstOrNull()
else null
?: PythonSdkUtil.getPythonExecutable(getCPython3().getOrThrow().toString())
Pair(pythonPath.toString(), PythonSdkAdditionalData(PyFlavorAndData(PyFlavorData.Empty, flavor)))
}
else {
// Target
@@ -77,6 +78,6 @@ class PySDKRule(private val targetConfigProducer: (() -> TargetEnvironmentConfig
private set
override fun before() {
sdk = runBlocking { createSdk(targetConfigProducer?.let { it() }) }
sdk = runBlocking { createSdk(targetConfigProducer?.let { it() }, detectSystemSdk = true) }
}
}

View File

@@ -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.env.python
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
import com.jetbrains.env.PyEnvTestCase
import com.jetbrains.env.PyEnvTestSettings
import org.jetbrains.annotations.TestOnly
import java.nio.file.Path
/**
* Returns first CPython 3.x installed with gradle script to the dir with env variable (see [PyEnvTestSettings])
*/
@TestOnly
@RequiresBackgroundThread
fun getCPython3(): Result<Path> =
PyEnvTestSettings
.fromEnvVariables()
.pythons
.map { it.toPath() }
.firstOrNull { isCPython3(it) }
?.let { Result.success(it) }
?: Result.failure(Throwable("No python found. See ${PyEnvTestSettings::class} class for more info"))
@RequiresBackgroundThread
private fun isCPython3(env: Path): Boolean =
PyEnvTestCase.loadEnvTags(env.toString()).let {
"conda" !in it && "python3" in it
}