Collapse duplicate toolchain search in test setup (#4442)

This commit is contained in:
Zanie Blue 2024-06-22 10:43:03 -04:00 committed by GitHub
parent 8f2d8b3f4f
commit c7149e96ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -709,47 +709,21 @@ pub fn python_toolchains_for_versions(
let cache = Cache::from_path(temp_dir.child("cache").to_path_buf()).init()?; let cache = Cache::from_path(temp_dir.child("cache").to_path_buf()).init()?;
let selected_pythons = python_versions let selected_pythons = python_versions
.iter() .iter()
.flat_map(|python_version| { .map(|python_version| {
let inner = InstalledToolchains::from_settings() if let Ok(toolchain) = Toolchain::find(
.map(|toolchains| { &ToolchainRequest::parse(python_version),
toolchains EnvironmentPreference::OnlySystem,
.find_version( ToolchainPreference::PreferInstalledManaged,
&PythonVersion::from_str(python_version) &cache,
.expect("Tests should use a valid Python version"), ) {
) toolchain
.expect("Tests are run on a supported platform") .into_interpreter()
.map(|toolchain| { .sys_executable()
toolchain .parent()
.executable() .expect("Python executable should always be in a directory")
.parent() .to_path_buf()
.expect("Executables must exist in a directory")
.to_path_buf()
})
.collect::<Vec<_>>()
})
.unwrap_or_default();
if inner.is_empty() {
// TODO(zanieb): Collapse these two cases now that we support `ToolchainPreference`
// Fallback to a system lookup if we failed to find one in the toolchain directory
if let Ok(toolchain) = Toolchain::find(
&ToolchainRequest::parse(python_version),
// Without required, we could pick the current venv here and the test fails
// because the venv subcommand requires a system interpreter.
EnvironmentPreference::OnlySystem,
ToolchainPreference::PreferInstalledManaged,
&cache,
) {
vec![toolchain
.into_interpreter()
.sys_executable()
.parent()
.expect("Python executable should always be in a directory")
.to_path_buf()]
} else {
panic!("Could not find Python {python_version} for test");
}
} else { } else {
inner panic!("Could not find Python {python_version} for test");
} }
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();