-
Notifications
You must be signed in to change notification settings - Fork 982
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a command to find a toolchain on the system. Right now, it displays the path to the first matching toolchain. We'll probably have more rich output in the future (after implementing `toolchain show`). The eventual plan (separate from here) is to port all of the toolchain discovery tests to use this command. I'll add a few tests for this command here anyway.
- Loading branch information
Showing
9 changed files
with
258 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use anyhow::Result; | ||
use std::fmt::Write; | ||
|
||
use uv_cache::Cache; | ||
use uv_configuration::PreviewMode; | ||
use uv_fs::Simplified; | ||
use uv_toolchain::{SystemPython, Toolchain, ToolchainRequest}; | ||
use uv_warnings::warn_user; | ||
|
||
use crate::commands::ExitStatus; | ||
use crate::printer::Printer; | ||
|
||
/// Find a toolchain. | ||
#[allow(clippy::too_many_arguments)] | ||
pub(crate) async fn find( | ||
request: Option<String>, | ||
preview: PreviewMode, | ||
cache: &Cache, | ||
printer: Printer, | ||
) -> Result<ExitStatus> { | ||
if preview.is_disabled() { | ||
warn_user!("`uv toolchain find` is experimental and may change without warning."); | ||
} | ||
|
||
let request = match request { | ||
Some(request) => ToolchainRequest::parse(&request), | ||
None => ToolchainRequest::Any, | ||
}; | ||
let toolchain = Toolchain::find_requested( | ||
&request, | ||
SystemPython::Required, | ||
PreviewMode::Enabled, | ||
cache, | ||
)?; | ||
|
||
writeln!( | ||
printer.stdout(), | ||
"{}", | ||
toolchain.interpreter().sys_executable().user_display() | ||
)?; | ||
|
||
Ok(ExitStatus::Success) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub(crate) mod find; | ||
pub(crate) mod install; | ||
pub(crate) mod list; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
#![cfg(all(feature = "python", feature = "pypi"))] | ||
|
||
use common::{python_path_with_versions, uv_snapshot, TestContext}; | ||
|
||
mod common; | ||
|
||
#[test] | ||
fn toolchain_find() { | ||
let context: TestContext = TestContext::new("3.12"); | ||
|
||
// No interpreters on the path | ||
uv_snapshot!(context.filters(), context.toolchain_find(), @r###" | ||
success: false | ||
exit_code: 2 | ||
----- stdout ----- | ||
----- stderr ----- | ||
error: No Python interpreters found in provided path, active virtual environment, or search path | ||
"###); | ||
|
||
let python_path = python_path_with_versions(&context.temp_dir, &["3.11", "3.12"]) | ||
.expect("Failed to create Python test path"); | ||
|
||
// Create some filters for the test interpreters, otherwise they'll be a path on the dev's machine | ||
// TODO(zanieb): Standardize this when writing more tests | ||
let python_path_filters = std::env::split_paths(&python_path) | ||
.zip(["3.11", "3.12"]) | ||
.flat_map(|(path, version)| { | ||
TestContext::path_patterns(path) | ||
.into_iter() | ||
.map(move |pattern| { | ||
( | ||
format!("{pattern}python.*"), | ||
format!("[PYTHON-PATH-{version}]"), | ||
) | ||
}) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let filters = python_path_filters | ||
.iter() | ||
.map(|(pattern, replacement)| (pattern.as_str(), replacement.as_str())) | ||
.chain(context.filters()) | ||
.collect::<Vec<_>>(); | ||
|
||
// We find the first interpreter on the path | ||
uv_snapshot!(filters, context.toolchain_find() | ||
.env("UV_TEST_PYTHON_PATH", &python_path), @r###" | ||
success: true | ||
exit_code: 0 | ||
----- stdout ----- | ||
[PYTHON-PATH-3.11] | ||
----- stderr ----- | ||
"###); | ||
|
||
// Request Python 3.12 | ||
uv_snapshot!(filters, context.toolchain_find() | ||
.arg("3.12") | ||
.env("UV_TEST_PYTHON_PATH", &python_path), @r###" | ||
success: true | ||
exit_code: 0 | ||
----- stdout ----- | ||
[PYTHON-PATH-3.12] | ||
----- stderr ----- | ||
"###); | ||
|
||
// Request Python 3.11 | ||
uv_snapshot!(filters, context.toolchain_find() | ||
.arg("3.11") | ||
.env("UV_TEST_PYTHON_PATH", &python_path), @r###" | ||
success: true | ||
exit_code: 0 | ||
----- stdout ----- | ||
[PYTHON-PATH-3.11] | ||
----- stderr ----- | ||
"###); | ||
|
||
// Request CPython | ||
uv_snapshot!(filters, context.toolchain_find() | ||
.arg("cpython") | ||
.env("UV_TEST_PYTHON_PATH", &python_path), @r###" | ||
success: true | ||
exit_code: 0 | ||
----- stdout ----- | ||
[PYTHON-PATH-3.11] | ||
----- stderr ----- | ||
"###); | ||
|
||
// Request CPython 3.12 | ||
uv_snapshot!(filters, context.toolchain_find() | ||
.arg("[email protected]") | ||
.env("UV_TEST_PYTHON_PATH", &python_path), @r###" | ||
success: true | ||
exit_code: 0 | ||
----- stdout ----- | ||
[PYTHON-PATH-3.12] | ||
----- stderr ----- | ||
"###); | ||
|
||
// Request PyPy | ||
uv_snapshot!(filters, context.toolchain_find() | ||
.arg("pypy") | ||
.env("UV_TEST_PYTHON_PATH", &python_path), @r###" | ||
success: false | ||
exit_code: 2 | ||
----- stdout ----- | ||
----- stderr ----- | ||
error: No interpreter found for PyPy in provided path, active virtual environment, or search path | ||
"###); | ||
|
||
// Swap the order (but don't change the filters to preserve our indices) | ||
let python_path = python_path_with_versions(&context.temp_dir, &["3.12", "3.11"]) | ||
.expect("Failed to create Python test path"); | ||
|
||
uv_snapshot!(filters, context.toolchain_find() | ||
.env("UV_TEST_PYTHON_PATH", &python_path), @r###" | ||
success: true | ||
exit_code: 0 | ||
----- stdout ----- | ||
[PYTHON-PATH-3.12] | ||
----- stderr ----- | ||
"###); | ||
|
||
// Request Python 3.11 | ||
uv_snapshot!(filters, context.toolchain_find() | ||
.arg("3.11") | ||
.env("UV_TEST_PYTHON_PATH", &python_path), @r###" | ||
success: true | ||
exit_code: 0 | ||
----- stdout ----- | ||
[PYTHON-PATH-3.11] | ||
----- stderr ----- | ||
"###); | ||
} |