Skip to content

Commit 6a25657

Browse files
committed
Search for conda envs in known locations (#23428)
1 parent fea0149 commit 6a25657

File tree

65 files changed

+603
-316
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+603
-316
lines changed

native_locator/src/conda.rs

+373-199
Large diffs are not rendered by default.

native_locator/src/known.rs

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use std::{env, path::PathBuf};
44

55
pub trait Environment {
66
fn get_user_home(&self) -> Option<PathBuf>;
7+
/**
8+
* Only used in tests, this is the root `/`.
9+
*/
10+
fn get_root(&self) -> Option<PathBuf>;
711
fn get_env_var(&self, key: String) -> Option<String>;
812
fn get_know_global_search_locations(&self) -> Vec<PathBuf>;
913
}
@@ -15,6 +19,9 @@ impl Environment for EnvironmentApi {
1519
fn get_user_home(&self) -> Option<PathBuf> {
1620
get_user_home()
1721
}
22+
fn get_root(&self) -> Option<PathBuf> {
23+
None
24+
}
1825
fn get_env_var(&self, key: String) -> Option<String> {
1926
get_env_var(key)
2027
}
@@ -28,6 +35,9 @@ impl Environment for EnvironmentApi {
2835
fn get_user_home(&self) -> Option<PathBuf> {
2936
get_user_home()
3037
}
38+
fn get_root(&self) -> Option<PathBuf> {
39+
None
40+
}
3141
fn get_env_var(&self, key: String) -> Option<String> {
3242
get_env_var(key)
3343
}

native_locator/src/locator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
utils::PythonEnv,
77
};
88

9-
#[derive(Debug)]
9+
#[derive(Debug, Clone)]
1010
pub struct LocatorResult {
1111
pub managers: Vec<EnvManager>,
1212
pub environments: Vec<PythonEnvironment>,

native_locator/src/messaging.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum EnvManagerType {
2525
Pyenv,
2626
}
2727

28-
#[derive(Serialize, Deserialize)]
28+
#[derive(Serialize, Deserialize, Clone)]
2929
#[serde(rename_all = "camelCase")]
3030
#[derive(Debug)]
3131
pub struct EnvManager {
@@ -44,16 +44,6 @@ impl EnvManager {
4444
}
4545
}
4646

47-
impl Clone for EnvManager {
48-
fn clone(&self) -> Self {
49-
Self {
50-
executable_path: self.executable_path.clone(),
51-
version: self.version.clone(),
52-
tool: self.tool.clone(),
53-
}
54-
}
55-
}
56-
5747
#[derive(Serialize, Deserialize)]
5848
#[serde(rename_all = "camelCase")]
5949
#[derive(Debug)]

native_locator/src/utils.rs

-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@ impl PythonEnv {
2424
version,
2525
}
2626
}
27-
pub fn from(executable: PathBuf) -> Self {
28-
Self {
29-
executable,
30-
path: None,
31-
version: None,
32-
}
33-
}
3427
}
3528

3629
#[derive(Debug)]

native_locator/tests/common.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use python_finder::{
5-
known::Environment,
6-
locator::LocatorResult,
7-
messaging::{EnvManager, PythonEnvironment},
8-
};
4+
use python_finder::known::Environment;
95
use serde_json::Value;
106
use std::{collections::HashMap, path::PathBuf};
117

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

1712
paths.iter().for_each(|p| root.push(p));
@@ -32,18 +27,23 @@ pub trait TestMessages {
3227
pub struct TestEnvironment {
3328
vars: HashMap<String, String>,
3429
home: Option<PathBuf>,
30+
root: Option<PathBuf>,
3531
globals_locations: Vec<PathBuf>,
3632
}
3733
#[allow(dead_code)]
3834
pub fn create_test_environment(
3935
vars: HashMap<String, String>,
4036
home: Option<PathBuf>,
4137
globals_locations: Vec<PathBuf>,
38+
root: Option<PathBuf>,
4239
) -> TestEnvironment {
4340
impl Environment for TestEnvironment {
4441
fn get_env_var(&self, key: String) -> Option<String> {
4542
self.vars.get(&key).cloned()
4643
}
44+
fn get_root(&self) -> Option<PathBuf> {
45+
self.root.clone()
46+
}
4747
fn get_user_home(&self) -> Option<PathBuf> {
4848
self.home.clone()
4949
}
@@ -54,6 +54,7 @@ pub fn create_test_environment(
5454
TestEnvironment {
5555
vars,
5656
home,
57+
root,
5758
globals_locations,
5859
}
5960
}
@@ -142,19 +143,3 @@ pub fn assert_messages(expected_json: &[Value], actual_json: &[Value]) {
142143
}
143144
}
144145
}
145-
146-
#[allow(dead_code)]
147-
pub fn get_environments_from_result(result: &Option<LocatorResult>) -> Vec<PythonEnvironment> {
148-
match result {
149-
Some(result) => result.environments.clone(),
150-
None => vec![],
151-
}
152-
}
153-
154-
#[allow(dead_code)]
155-
pub fn get_managers_from_result(result: &Option<LocatorResult>) -> Vec<EnvManager> {
156-
match result {
157-
Some(result) => result.managers.clone(),
158-
None => vec![],
159-
}
160-
}

native_locator/tests/common_python_test.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,29 @@ mod common;
77
#[cfg(unix)]
88
fn find_python_in_path_this() {
99
use crate::common::{
10-
assert_messages, create_test_environment, get_environments_from_result, join_test_paths,
11-
test_file_path,
10+
assert_messages, create_test_environment, join_test_paths, test_file_path,
1211
};
1312
use python_finder::{common_python, locator::Locator, messaging::PythonEnvironment};
1413
use serde_json::json;
1514
use std::collections::HashMap;
1615

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

2019
let known = create_test_environment(
2120
HashMap::from([(
2221
"PATH".to_string(),
23-
unix_python.clone().to_str().unwrap().to_string(),
22+
user_home.clone().to_string_lossy().to_string(),
2423
)]),
25-
Some(unix_python.clone()),
24+
Some(user_home.clone()),
2625
Vec::new(),
26+
None,
2727
);
2828

2929
let mut locator = common_python::PythonOnPath::with(&known);
30-
let result = locator.find();
30+
let result = locator.find().unwrap();
3131

32-
let environments = get_environments_from_result(&result);
33-
assert_eq!(environments.len(), 1);
32+
assert_eq!(result.environments.len(), 1);
3433

3534
let env = PythonEnvironment {
3635
display_name: None,
@@ -41,10 +40,14 @@ fn find_python_in_path_this() {
4140
category: python_finder::messaging::PythonEnvironmentCategory::System,
4241
version: None,
4342
python_run_command: Some(vec![unix_python_exe.clone().to_str().unwrap().to_string()]),
44-
env_path: Some(unix_python.clone()),
43+
env_path: Some(user_home.clone()),
4544
};
4645
assert_messages(
4746
&[json!(env)],
48-
&environments.iter().map(|e| json!(e)).collect::<Vec<_>>(),
47+
&result
48+
.environments
49+
.iter()
50+
.map(|e| json!(e))
51+
.collect::<Vec<_>>(),
4952
);
5053
}

0 commit comments

Comments
 (0)