|
| 1 | +#![cfg(all(feature = "python", feature = "pypi"))] |
| 2 | + |
| 3 | +use common::{python_path_with_versions, uv_snapshot, TestContext}; |
| 4 | + |
| 5 | +mod common; |
| 6 | + |
| 7 | +#[test] |
| 8 | +fn toolchain_find() { |
| 9 | + let context: TestContext = TestContext::new("3.12"); |
| 10 | + |
| 11 | + // No interpreters on the path |
| 12 | + uv_snapshot!(context.filters(), context.toolchain_find(), @r###" |
| 13 | + success: false |
| 14 | + exit_code: 2 |
| 15 | + ----- stdout ----- |
| 16 | +
|
| 17 | + ----- stderr ----- |
| 18 | + error: No Python interpreters found in provided path, active virtual environment, or search path |
| 19 | + "###); |
| 20 | + |
| 21 | + let python_path = python_path_with_versions(&context.temp_dir, &["3.11", "3.12"]) |
| 22 | + .expect("Failed to create Python test path"); |
| 23 | + |
| 24 | + // Create some filters for the test interpreters, otherwise they'll be a path on the dev's machine |
| 25 | + // TODO(zanieb): Standardize this when writing more tests |
| 26 | + let python_path_filters = std::env::split_paths(&python_path) |
| 27 | + .zip(["3.11", "3.12"]) |
| 28 | + .flat_map(|(path, version)| { |
| 29 | + TestContext::path_patterns(path) |
| 30 | + .into_iter() |
| 31 | + .map(move |pattern| { |
| 32 | + ( |
| 33 | + format!("{pattern}python.*"), |
| 34 | + format!("[PYTHON-PATH-{version}]"), |
| 35 | + ) |
| 36 | + }) |
| 37 | + }) |
| 38 | + .collect::<Vec<_>>(); |
| 39 | + |
| 40 | + let filters = python_path_filters |
| 41 | + .iter() |
| 42 | + .map(|(pattern, replacement)| (pattern.as_str(), replacement.as_str())) |
| 43 | + .chain(context.filters()) |
| 44 | + .collect::<Vec<_>>(); |
| 45 | + |
| 46 | + // We find the first interpreter on the path |
| 47 | + uv_snapshot!(filters, context.toolchain_find() |
| 48 | + .env("UV_TEST_PYTHON_PATH", &python_path), @r###" |
| 49 | + success: true |
| 50 | + exit_code: 0 |
| 51 | + ----- stdout ----- |
| 52 | + [PYTHON-PATH-3.11] |
| 53 | +
|
| 54 | + ----- stderr ----- |
| 55 | + "###); |
| 56 | + |
| 57 | + // Request Python 3.12 |
| 58 | + uv_snapshot!(filters, context.toolchain_find() |
| 59 | + .arg("3.12") |
| 60 | + .env("UV_TEST_PYTHON_PATH", &python_path), @r###" |
| 61 | + success: true |
| 62 | + exit_code: 0 |
| 63 | + ----- stdout ----- |
| 64 | + [PYTHON-PATH-3.12] |
| 65 | +
|
| 66 | + ----- stderr ----- |
| 67 | + "###); |
| 68 | + |
| 69 | + // Request Python 3.11 |
| 70 | + uv_snapshot!(filters, context.toolchain_find() |
| 71 | + .arg("3.11") |
| 72 | + .env("UV_TEST_PYTHON_PATH", &python_path), @r###" |
| 73 | + success: true |
| 74 | + exit_code: 0 |
| 75 | + ----- stdout ----- |
| 76 | + [PYTHON-PATH-3.11] |
| 77 | +
|
| 78 | + ----- stderr ----- |
| 79 | + "###); |
| 80 | + |
| 81 | + // Request CPython |
| 82 | + uv_snapshot!(filters, context.toolchain_find() |
| 83 | + .arg("cpython") |
| 84 | + .env("UV_TEST_PYTHON_PATH", &python_path), @r###" |
| 85 | + success: true |
| 86 | + exit_code: 0 |
| 87 | + ----- stdout ----- |
| 88 | + [PYTHON-PATH-3.11] |
| 89 | +
|
| 90 | + ----- stderr ----- |
| 91 | + "###); |
| 92 | + |
| 93 | + // Request CPython 3.12 |
| 94 | + uv_snapshot!(filters, context.toolchain_find() |
| 95 | + |
| 96 | + .env("UV_TEST_PYTHON_PATH", &python_path), @r###" |
| 97 | + success: true |
| 98 | + exit_code: 0 |
| 99 | + ----- stdout ----- |
| 100 | + [PYTHON-PATH-3.12] |
| 101 | +
|
| 102 | + ----- stderr ----- |
| 103 | + "###); |
| 104 | + |
| 105 | + // Request PyPy |
| 106 | + uv_snapshot!(filters, context.toolchain_find() |
| 107 | + .arg("pypy") |
| 108 | + .env("UV_TEST_PYTHON_PATH", &python_path), @r###" |
| 109 | + success: false |
| 110 | + exit_code: 2 |
| 111 | + ----- stdout ----- |
| 112 | +
|
| 113 | + ----- stderr ----- |
| 114 | + error: No interpreter found for PyPy in provided path, active virtual environment, or search path |
| 115 | + "###); |
| 116 | + |
| 117 | + // Swap the order (but don't change the filters to preserve our indices) |
| 118 | + let python_path = python_path_with_versions(&context.temp_dir, &["3.12", "3.11"]) |
| 119 | + .expect("Failed to create Python test path"); |
| 120 | + |
| 121 | + uv_snapshot!(filters, context.toolchain_find() |
| 122 | + .env("UV_TEST_PYTHON_PATH", &python_path), @r###" |
| 123 | + success: true |
| 124 | + exit_code: 0 |
| 125 | + ----- stdout ----- |
| 126 | + [PYTHON-PATH-3.12] |
| 127 | +
|
| 128 | + ----- stderr ----- |
| 129 | + "###); |
| 130 | + |
| 131 | + // Request Python 3.11 |
| 132 | + uv_snapshot!(filters, context.toolchain_find() |
| 133 | + .arg("3.11") |
| 134 | + .env("UV_TEST_PYTHON_PATH", &python_path), @r###" |
| 135 | + success: true |
| 136 | + exit_code: 0 |
| 137 | + ----- stdout ----- |
| 138 | + [PYTHON-PATH-3.11] |
| 139 | +
|
| 140 | + ----- stderr ----- |
| 141 | + "###); |
| 142 | +} |
0 commit comments