Skip to content

Commit

Permalink
Search for conda envs in known locations (microsoft#23428)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonJayamanne authored May 15, 2024
1 parent 156f22c commit d057346
Show file tree
Hide file tree
Showing 65 changed files with 603 additions and 316 deletions.
572 changes: 373 additions & 199 deletions native_locator/src/conda.rs

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions native_locator/src/known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use std::{env, path::PathBuf};

pub trait Environment {
fn get_user_home(&self) -> Option<PathBuf>;
/**
* Only used in tests, this is the root `/`.
*/
fn get_root(&self) -> Option<PathBuf>;
fn get_env_var(&self, key: String) -> Option<String>;
fn get_know_global_search_locations(&self) -> Vec<PathBuf>;
}
Expand All @@ -15,6 +19,9 @@ impl Environment for EnvironmentApi {
fn get_user_home(&self) -> Option<PathBuf> {
get_user_home()
}
fn get_root(&self) -> Option<PathBuf> {
None
}
fn get_env_var(&self, key: String) -> Option<String> {
get_env_var(key)
}
Expand All @@ -28,6 +35,9 @@ impl Environment for EnvironmentApi {
fn get_user_home(&self) -> Option<PathBuf> {
get_user_home()
}
fn get_root(&self) -> Option<PathBuf> {
None
}
fn get_env_var(&self, key: String) -> Option<String> {
get_env_var(key)
}
Expand Down
2 changes: 1 addition & 1 deletion native_locator/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
utils::PythonEnv,
};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct LocatorResult {
pub managers: Vec<EnvManager>,
pub environments: Vec<PythonEnvironment>,
Expand Down
12 changes: 1 addition & 11 deletions native_locator/src/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum EnvManagerType {
Pyenv,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[derive(Debug)]
pub struct EnvManager {
Expand All @@ -44,16 +44,6 @@ impl EnvManager {
}
}

impl Clone for EnvManager {
fn clone(&self) -> Self {
Self {
executable_path: self.executable_path.clone(),
version: self.version.clone(),
tool: self.tool.clone(),
}
}
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug)]
Expand Down
7 changes: 0 additions & 7 deletions native_locator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ impl PythonEnv {
version,
}
}
pub fn from(executable: PathBuf) -> Self {
Self {
executable,
path: None,
version: None,
}
}
}

#[derive(Debug)]
Expand Down
29 changes: 7 additions & 22 deletions native_locator/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use python_finder::{
known::Environment,
locator::LocatorResult,
messaging::{EnvManager, PythonEnvironment},
};
use python_finder::known::Environment;
use serde_json::Value;
use std::{collections::HashMap, path::PathBuf};

#[allow(dead_code)]
pub fn test_file_path(paths: &[&str]) -> PathBuf {
// let parts: Vec<String> = paths.iter().map(|p| p.to_string()).collect();
let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

paths.iter().for_each(|p| root.push(p));
Expand All @@ -32,18 +27,23 @@ pub trait TestMessages {
pub struct TestEnvironment {
vars: HashMap<String, String>,
home: Option<PathBuf>,
root: Option<PathBuf>,
globals_locations: Vec<PathBuf>,
}
#[allow(dead_code)]
pub fn create_test_environment(
vars: HashMap<String, String>,
home: Option<PathBuf>,
globals_locations: Vec<PathBuf>,
root: Option<PathBuf>,
) -> TestEnvironment {
impl Environment for TestEnvironment {
fn get_env_var(&self, key: String) -> Option<String> {
self.vars.get(&key).cloned()
}
fn get_root(&self) -> Option<PathBuf> {
self.root.clone()
}
fn get_user_home(&self) -> Option<PathBuf> {
self.home.clone()
}
Expand All @@ -54,6 +54,7 @@ pub fn create_test_environment(
TestEnvironment {
vars,
home,
root,
globals_locations,
}
}
Expand Down Expand Up @@ -142,19 +143,3 @@ pub fn assert_messages(expected_json: &[Value], actual_json: &[Value]) {
}
}
}

#[allow(dead_code)]
pub fn get_environments_from_result(result: &Option<LocatorResult>) -> Vec<PythonEnvironment> {
match result {
Some(result) => result.environments.clone(),
None => vec![],
}
}

#[allow(dead_code)]
pub fn get_managers_from_result(result: &Option<LocatorResult>) -> Vec<EnvManager> {
match result {
Some(result) => result.managers.clone(),
None => vec![],
}
}
25 changes: 14 additions & 11 deletions native_locator/tests/common_python_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,29 @@ mod common;
#[cfg(unix)]
fn find_python_in_path_this() {
use crate::common::{
assert_messages, create_test_environment, get_environments_from_result, join_test_paths,
test_file_path,
assert_messages, create_test_environment, join_test_paths, test_file_path,
};
use python_finder::{common_python, locator::Locator, messaging::PythonEnvironment};
use serde_json::json;
use std::collections::HashMap;

let unix_python = test_file_path(&["tests/unix/known"]);
let unix_python_exe = join_test_paths(&[unix_python.clone().to_str().unwrap(), "python"]);
let user_home = test_file_path(&["tests/unix/known/user_home"]);
let unix_python_exe = join_test_paths(&[user_home.clone().to_str().unwrap(), "python"]);

let known = create_test_environment(
HashMap::from([(
"PATH".to_string(),
unix_python.clone().to_str().unwrap().to_string(),
user_home.clone().to_string_lossy().to_string(),
)]),
Some(unix_python.clone()),
Some(user_home.clone()),
Vec::new(),
None,
);

let mut locator = common_python::PythonOnPath::with(&known);
let result = locator.find();
let result = locator.find().unwrap();

let environments = get_environments_from_result(&result);
assert_eq!(environments.len(), 1);
assert_eq!(result.environments.len(), 1);

let env = PythonEnvironment {
display_name: None,
Expand All @@ -41,10 +40,14 @@ fn find_python_in_path_this() {
category: python_finder::messaging::PythonEnvironmentCategory::System,
version: None,
python_run_command: Some(vec![unix_python_exe.clone().to_str().unwrap().to_string()]),
env_path: Some(unix_python.clone()),
env_path: Some(user_home.clone()),
};
assert_messages(
&[json!(env)],
&environments.iter().map(|e| json!(e)).collect::<Vec<_>>(),
&result
.environments
.iter()
.map(|e| json!(e))
.collect::<Vec<_>>(),
);
}
Loading

0 comments on commit d057346

Please sign in to comment.