Skip to content

Commit

Permalink
Add conda prefix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed May 22, 2024
1 parent dad5128 commit c83ba47
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crates/uv-interpreter/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ impl PythonEnvironment {
}

/// Create a [`PythonEnvironment`] for an existing virtual environment.
///
/// Allows Conda environments (via `CONDA_PREFIX`) though they are not technically virtual environments.
pub fn from_virtualenv(cache: &Cache) -> Result<Self, Error> {
let sources = SourceSelector::VirtualEnv;
let request = InterpreterRequest::Version(VersionRequest::Default);
let found = find_interpreter(&request, SystemPython::Disallowed, &sources, cache)??;

debug_assert!(
found.interpreter().is_virtualenv(),
found.interpreter().is_virtualenv()
|| matches!(found.source(), InterpreterSource::CondaPrefix),
"Not a virtualenv (source: {}, prefix: {})",
found.source(),
found.interpreter().base_prefix().display()
Expand Down
91 changes: 91 additions & 0 deletions crates/uv-interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ mod tests {
Ok(venv.to_path_buf())
}

/// Create a mock conda prefix in the given directory.
///
/// These are like virtual environments but they look like system interpreters because `prefix` and `base_prefix` are equal.
///
/// Returns the path to the environment.
fn mock_conda_prefix(tempdir: &TempDir, version: &'static str) -> Result<PathBuf> {
let env = tempdir.child("conda");
let executable = virtualenv_python_executable(&env);
fs_err::create_dir_all(
executable
.parent()
.expect("A Python executable path should always have a parent"),
)?;
create_mock_interpreter(
&executable,
&PythonVersion::from_str(version).expect("A valid Python version is used for tests"),
ImplementationName::default(),
true,
)?;
env.child("pyvenv.cfg").touch()?;
Ok(env.to_path_buf())
}

#[test]
fn find_default_interpreter_empty_path() -> Result<()> {
let cache = Cache::temp()?;
Expand Down Expand Up @@ -1308,6 +1331,74 @@ mod tests {
Ok(())
}

#[test]
fn find_environment_from_conda_prefix() -> Result<()> {
let tempdir = TempDir::new()?;
let cache = Cache::temp()?;
let conda_prefix = mock_conda_prefix(&tempdir, "3.12.0")?;

with_vars(
[
("UV_TEST_PYTHON_PATH", None),
("UV_BOOTSTRAP_DIR", None),
(
"PATH",
Some(simple_mock_interpreters(&tempdir, &["3.10.1", "3.11.2"])?),
),
("CONDA_PREFIX", Some(conda_prefix.into())),
("PWD", Some(tempdir.path().into())),
],
|| {
let environment =
// Note this environment is not treated as a system interpreter
PythonEnvironment::find(None, SystemPython::Disallowed, &cache)
.expect("An environment is found");
assert_eq!(
environment.interpreter().python_full_version().to_string(),
"3.12.0",
"We should allow the conda environment"
);
},
);

Ok(())
}

#[test]
fn find_environment_from_conda_prefix_and_virtualenv() -> Result<()> {
let tempdir = TempDir::new()?;
let cache = Cache::temp()?;
let generic = mock_venv(&tempdir, "3.12.0")?;
let conda = mock_conda_prefix(&tempdir, "3.12.1")?;

with_vars(
[
("UV_TEST_PYTHON_PATH", None),
("UV_BOOTSTRAP_DIR", None),
(
"PATH",
Some(simple_mock_interpreters(&tempdir, &["3.10.2", "3.11.3"])?),
),
("CONDA_PREFIX", Some(conda.into())),
("VIRTUAL_ENV", Some(generic.into())),
("PWD", Some(tempdir.path().into())),
],
|| {
let environment =
// Note this environment is not treated as a system interpreter
PythonEnvironment::find(None, SystemPython::Disallowed, &cache)
.expect("An environment is found");
assert_eq!(
environment.interpreter().python_full_version().to_string(),
"3.12.0",
"We should prefer the active environment"
);
},
);

Ok(())
}

#[test]
fn find_environment_from_discovered_environment() -> Result<()> {
let tempdir = TempDir::new()?;
Expand Down

0 comments on commit c83ba47

Please sign in to comment.