Skip to content

Commit 5831123

Browse files
committed
Use structs instead of loose json in native test (#23394)
1 parent 0479bc9 commit 5831123

File tree

5 files changed

+233
-30
lines changed

5 files changed

+233
-30
lines changed

native_locator/src/conda.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ pub fn find_and_report(
384384
let conda_binary = find_conda_binary(environment);
385385
match conda_binary {
386386
Some(conda_binary) => {
387-
let params = messaging::EnvManager::new(
387+
let env_manager = messaging::EnvManager::new(
388388
conda_binary.clone(),
389389
get_conda_version(&conda_binary),
390390
EnvManagerType::Conda,
391391
);
392-
dispatcher.report_environment_manager(params);
392+
dispatcher.report_environment_manager(env_manager.clone());
393393

394394
let envs = get_distinct_conda_envs(&conda_binary, environment);
395395
for env in envs {
@@ -401,7 +401,7 @@ pub fn find_and_report(
401401
get_conda_python_version(&env.path),
402402
Some(env.path.clone()),
403403
Some(env.path.clone()),
404-
None,
404+
Some(env_manager.clone()),
405405
if env.named {
406406
Some(vec![
407407
conda_binary.to_string_lossy().to_string(),

native_locator/tests/common.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,26 @@ fn compare_json(expected: &Value, actual: &Value) -> bool {
9292
}
9393

9494
if expected.is_object() {
95+
if expected.as_object().is_none() && actual.as_object().is_none() {
96+
return true;
97+
}
98+
99+
if expected.as_object().is_none() && actual.as_object().is_some() {
100+
return false;
101+
}
102+
if expected.as_object().is_some() && actual.as_object().is_none() {
103+
return false;
104+
}
105+
95106
let expected = expected.as_object().unwrap();
96107
let actual = actual.as_object().unwrap();
97108

98109
for (key, value) in expected.iter() {
99110
if !actual.contains_key(key) {
100111
return false;
101112
}
102-
113+
println!("\nCompare Key {:?}", key);
114+
println!("\nCompare Key {:?}", actual.get(key).is_none());
103115
if !compare_json(value, actual.get(key).unwrap()) {
104116
return false;
105117
}

native_locator/tests/common_python_test.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn find_python_in_path_this() {
1010
assert_messages, create_test_dispatcher, create_test_environment, join_test_paths,
1111
test_file_path,
1212
};
13-
use python_finder::common_python;
13+
use python_finder::{common_python, messaging::PythonEnvironment};
1414
use serde_json::json;
1515
use std::collections::HashMap;
1616

@@ -19,14 +19,27 @@ fn find_python_in_path_this() {
1919

2020
let mut dispatcher = create_test_dispatcher();
2121
let known = create_test_environment(
22-
HashMap::from([("PATH".to_string(), unix_python.clone().to_str().unwrap().to_string())]),
22+
HashMap::from([(
23+
"PATH".to_string(),
24+
unix_python.clone().to_str().unwrap().to_string(),
25+
)]),
2326
Some(unix_python.clone()),
2427
Vec::new(),
2528
);
2629

2730
common_python::find_and_report(&mut dispatcher, &known);
2831

2932
assert_eq!(dispatcher.messages.len(), 1);
30-
let expected_json = json!({"envManager":null,"projectPath": null, "name":null,"pythonExecutablePath":unix_python_exe.clone(),"category":"system","version":null,"pythonRunCommand":[unix_python_exe.clone()],"envPath":unix_python.clone(),"sysPrefixPath":unix_python.clone()});
31-
assert_messages(&[expected_json], &dispatcher);
33+
let env = PythonEnvironment {
34+
env_manager: None,
35+
project_path: None,
36+
name: None,
37+
python_executable_path: Some(unix_python_exe.clone()),
38+
category: python_finder::messaging::PythonEnvironmentCategory::System,
39+
version: None,
40+
python_run_command: Some(vec![unix_python_exe.clone().to_str().unwrap().to_string()]),
41+
env_path: Some(unix_python.clone()),
42+
sys_prefix_path: Some(unix_python.clone()),
43+
};
44+
assert_messages(&[json!(env)], &dispatcher);
3245
}

native_locator/tests/conda_test.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ fn find_conda_exe_and_empty_envs() {
2929
assert_messages, create_test_dispatcher, create_test_environment, join_test_paths,
3030
test_file_path,
3131
};
32-
use python_finder::conda;
32+
use python_finder::{
33+
conda,
34+
messaging::{EnvManager, EnvManagerType},
35+
};
3336
use serde_json::json;
3437
use std::{collections::HashMap, path::PathBuf};
3538
let conda_dir = test_file_path(&["tests/unix/conda_without_envs"]);
@@ -47,8 +50,12 @@ fn find_conda_exe_and_empty_envs() {
4750
conda::find_and_report(&mut dispatcher, &known);
4851

4952
let conda_exe = join_test_paths(&[conda_dir.clone().to_str().unwrap(), "conda"]);
50-
let expected_json = json!({"executablePath":conda_exe.clone(),"version":null, "tool": "conda"});
51-
assert_messages(&[expected_json], &dispatcher)
53+
let expected_conda_manager = EnvManager {
54+
executable_path: conda_exe.clone(),
55+
version: None,
56+
tool: EnvManagerType::Conda,
57+
};
58+
assert_messages(&[json!(expected_conda_manager)], &dispatcher)
5259
}
5360
#[test]
5461
#[cfg(unix)]
@@ -58,6 +65,7 @@ fn finds_two_conda_envs_from_txt() {
5865
test_file_path,
5966
};
6067
use python_finder::conda;
68+
use python_finder::messaging::{EnvManager, EnvManagerType, PythonEnvironment};
6169
use serde_json::json;
6270
use std::collections::HashMap;
6371
use std::fs;
@@ -90,12 +98,51 @@ fn finds_two_conda_envs_from_txt() {
9098
let conda_1_exe = join_test_paths(&[conda_1.clone().to_str().unwrap(), "python"]);
9199
let conda_2_exe = join_test_paths(&[conda_2.clone().to_str().unwrap(), "python"]);
92100

93-
let expected_conda_env =
94-
json!({ "executablePath": conda_exe.clone(), "version": null, "tool": "conda"});
95-
let expected_conda_1 = json!({ "name": "one","projectPath": null, "pythonExecutablePath": conda_1_exe.clone(), "category": "conda", "version": "10.0.1", "envPath": conda_1.clone(), "sysPrefixPath": conda_1.clone(), "envManager": null, "pythonRunCommand": [conda_exe.clone(), "run", "-n", "one", "python"]});
96-
let expected_conda_2 = json!({ "name": "two", "projectPath": null, "pythonExecutablePath": conda_2_exe.clone(), "category": "conda", "version": null, "envPath": conda_2.clone(), "sysPrefixPath": conda_2.clone(), "envManager": null,"pythonRunCommand": [conda_exe.clone(),"run","-n","two","python"]});
101+
let expected_conda_manager = EnvManager {
102+
executable_path: conda_exe.clone(),
103+
version: None,
104+
tool: EnvManagerType::Conda,
105+
};
106+
let expected_conda_1 = PythonEnvironment {
107+
name: Some("one".to_string()),
108+
project_path: None,
109+
python_executable_path: Some(conda_1_exe.clone()),
110+
category: python_finder::messaging::PythonEnvironmentCategory::Conda,
111+
version: Some("10.0.1".to_string()),
112+
env_path: Some(conda_1.clone()),
113+
sys_prefix_path: Some(conda_1.clone()),
114+
env_manager: Some(expected_conda_manager.clone()),
115+
python_run_command: Some(vec![
116+
conda_exe.clone().to_str().unwrap().to_string(),
117+
"run".to_string(),
118+
"-n".to_string(),
119+
"one".to_string(),
120+
"python".to_string(),
121+
]),
122+
};
123+
let expected_conda_2 = PythonEnvironment {
124+
name: Some("two".to_string()),
125+
project_path: None,
126+
python_executable_path: Some(conda_2_exe.clone()),
127+
category: python_finder::messaging::PythonEnvironmentCategory::Conda,
128+
version: None,
129+
env_path: Some(conda_2.clone()),
130+
sys_prefix_path: Some(conda_2.clone()),
131+
env_manager: Some(expected_conda_manager.clone()),
132+
python_run_command: Some(vec![
133+
conda_exe.clone().to_str().unwrap().to_string(),
134+
"run".to_string(),
135+
"-n".to_string(),
136+
"two".to_string(),
137+
"python".to_string(),
138+
]),
139+
};
97140
assert_messages(
98-
&[expected_conda_1, expected_conda_env, expected_conda_2],
141+
&[
142+
json!(expected_conda_1),
143+
json!(expected_conda_manager),
144+
json!(expected_conda_2),
145+
],
99146
&dispatcher,
100147
)
101148
}

native_locator/tests/pyenv_test.rs

Lines changed: 145 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ fn find_pyenv_envs() {
5757
assert_messages, create_test_dispatcher, create_test_environment, join_test_paths,
5858
test_file_path,
5959
};
60-
use python_finder::pyenv;
60+
use python_finder::{
61+
messaging::{EnvManager, EnvManagerType, PythonEnvironment},
62+
pyenv,
63+
};
6164
use serde_json::json;
6265
use std::{collections::HashMap, path::PathBuf};
6366

@@ -74,21 +77,149 @@ fn find_pyenv_envs() {
7477
pyenv::find_and_report(&mut dispatcher, &known);
7578

7679
assert_eq!(dispatcher.messages.len(), 6);
77-
let expected_manager =
78-
json!({ "executablePath": pyenv_exe.clone(), "version": null, "tool": "pyenv" });
79-
let expected_3_9_9 = json!({"projectPath": null, "name": null,"pythonExecutablePath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.9.9/bin/python"]), "pythonRunCommand": [join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.9.9/bin/python"])], "category": "pyenv","version": "3.9.9","envPath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.9.9"]),"sysPrefixPath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.9.9"]), "envManager": expected_manager});
80-
let expected_virtual_env = json!({"projectPath": null, "name": "my-virtual-env", "version": "3.10.13", "category": "pyenvVirtualEnv", "envPath": join_test_paths(&[home.to_str().unwrap(),".pyenv/versions/my-virtual-env"]), "pythonExecutablePath": join_test_paths(&[home.to_str().unwrap(),".pyenv/versions/my-virtual-env/bin/python"]), "sysPrefixPath": join_test_paths(&[home.to_str().unwrap(),".pyenv/versions/my-virtual-env"]), "pythonRunCommand": [join_test_paths(&[home.to_str().unwrap(),".pyenv/versions/my-virtual-env/bin/python"])], "envManager": expected_manager});
81-
let expected_3_12_1 = json!({"projectPath": null, "name": null,"pythonExecutablePath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.12.1/bin/python"]), "pythonRunCommand": [join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.12.1/bin/python"])], "category": "pyenv","version": "3.12.1","envPath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.12.1"]),"sysPrefixPath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.12.1"]), "envManager": expected_manager});
82-
let expected_3_13_dev = json!({"projectPath": null, "name": null,"pythonExecutablePath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.13-dev/bin/python"]), "pythonRunCommand": [join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.13-dev/bin/python"])], "category": "pyenv","version": "3.13-dev","envPath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.13-dev"]),"sysPrefixPath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.13-dev"]), "envManager": expected_manager});
83-
let expected_3_12_1a3 = json!({"projectPath": null, "name": null,"pythonExecutablePath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.12.1a3/bin/python"]), "pythonRunCommand": [join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.12.1a3/bin/python"])], "category": "pyenv","version": "3.12.1a3","envPath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.12.1a3"]),"sysPrefixPath": join_test_paths(&[home.to_str().unwrap(), ".pyenv/versions/3.12.1a3"]), "envManager": expected_manager});
80+
let expected_manager = EnvManager {
81+
executable_path: pyenv_exe.clone(),
82+
version: None,
83+
tool: EnvManagerType::Pyenv,
84+
};
85+
let expected_3_9_9 = json!(PythonEnvironment {
86+
project_path: None,
87+
name: None,
88+
python_executable_path: Some(join_test_paths(&[
89+
home.to_str().unwrap(),
90+
".pyenv/versions/3.9.9/bin/python"
91+
])),
92+
python_run_command: Some(vec![join_test_paths(&[
93+
home.to_str().unwrap(),
94+
".pyenv/versions/3.9.9/bin/python"
95+
])
96+
.to_str()
97+
.unwrap()
98+
.to_string()]),
99+
category: python_finder::messaging::PythonEnvironmentCategory::Pyenv,
100+
version: Some("3.9.9".to_string()),
101+
env_path: Some(join_test_paths(&[
102+
home.to_str().unwrap(),
103+
".pyenv/versions/3.9.9"
104+
])),
105+
sys_prefix_path: Some(join_test_paths(&[
106+
home.to_str().unwrap(),
107+
".pyenv/versions/3.9.9"
108+
])),
109+
env_manager: Some(expected_manager.clone())
110+
});
111+
let expected_virtual_env = PythonEnvironment {
112+
project_path: None,
113+
name: Some("my-virtual-env".to_string()),
114+
python_executable_path: Some(join_test_paths(&[
115+
home.to_str().unwrap(),
116+
".pyenv/versions/my-virtual-env/bin/python",
117+
])),
118+
python_run_command: Some(vec![join_test_paths(&[
119+
home.to_str().unwrap(),
120+
".pyenv/versions/my-virtual-env/bin/python",
121+
])
122+
.to_str()
123+
.unwrap()
124+
.to_string()]),
125+
category: python_finder::messaging::PythonEnvironmentCategory::PyenvVirtualEnv,
126+
version: Some("3.10.13".to_string()),
127+
env_path: Some(join_test_paths(&[
128+
home.to_str().unwrap(),
129+
".pyenv/versions/my-virtual-env",
130+
])),
131+
sys_prefix_path: Some(join_test_paths(&[
132+
home.to_str().unwrap(),
133+
".pyenv/versions/my-virtual-env",
134+
])),
135+
env_manager: Some(expected_manager.clone()),
136+
};
137+
let expected_3_12_1 = PythonEnvironment {
138+
project_path: None,
139+
name: None,
140+
python_executable_path: Some(join_test_paths(&[
141+
home.to_str().unwrap(),
142+
".pyenv/versions/3.12.1/bin/python",
143+
])),
144+
python_run_command: Some(vec![join_test_paths(&[
145+
home.to_str().unwrap(),
146+
".pyenv/versions/3.12.1/bin/python",
147+
])
148+
.to_str()
149+
.unwrap()
150+
.to_string()]),
151+
category: python_finder::messaging::PythonEnvironmentCategory::Pyenv,
152+
version: Some("3.12.1".to_string()),
153+
env_path: Some(join_test_paths(&[
154+
home.to_str().unwrap(),
155+
".pyenv/versions/3.12.1",
156+
])),
157+
sys_prefix_path: Some(join_test_paths(&[
158+
home.to_str().unwrap(),
159+
".pyenv/versions/3.12.1",
160+
])),
161+
env_manager: Some(expected_manager.clone()),
162+
};
163+
let expected_3_13_dev = PythonEnvironment {
164+
project_path: None,
165+
name: None,
166+
python_executable_path: Some(join_test_paths(&[
167+
home.to_str().unwrap(),
168+
".pyenv/versions/3.13-dev/bin/python",
169+
])),
170+
python_run_command: Some(vec![join_test_paths(&[
171+
home.to_str().unwrap(),
172+
".pyenv/versions/3.13-dev/bin/python",
173+
])
174+
.to_str()
175+
.unwrap()
176+
.to_string()]),
177+
category: python_finder::messaging::PythonEnvironmentCategory::Pyenv,
178+
version: Some("3.13-dev".to_string()),
179+
env_path: Some(join_test_paths(&[
180+
home.to_str().unwrap(),
181+
".pyenv/versions/3.13-dev",
182+
])),
183+
sys_prefix_path: Some(join_test_paths(&[
184+
home.to_str().unwrap(),
185+
".pyenv/versions/3.13-dev",
186+
])),
187+
env_manager: Some(expected_manager.clone()),
188+
};
189+
let expected_3_12_1a3 = PythonEnvironment {
190+
project_path: None,
191+
name: None,
192+
python_executable_path: Some(join_test_paths(&[
193+
home.to_str().unwrap(),
194+
".pyenv/versions/3.12.1a3/bin/python",
195+
])),
196+
python_run_command: Some(vec![join_test_paths(&[
197+
home.to_str().unwrap(),
198+
".pyenv/versions/3.12.1a3/bin/python",
199+
])
200+
.to_str()
201+
.unwrap()
202+
.to_string()]),
203+
category: python_finder::messaging::PythonEnvironmentCategory::Pyenv,
204+
version: Some("3.12.1a3".to_string()),
205+
env_path: Some(join_test_paths(&[
206+
home.to_str().unwrap(),
207+
".pyenv/versions/3.12.1a3",
208+
])),
209+
sys_prefix_path: Some(join_test_paths(&[
210+
home.to_str().unwrap(),
211+
".pyenv/versions/3.12.1a3",
212+
])),
213+
env_manager: Some(expected_manager.clone()),
214+
};
84215
assert_messages(
85216
&[
86-
expected_manager,
87-
expected_3_9_9,
88-
expected_virtual_env,
89-
expected_3_12_1,
90-
expected_3_13_dev,
91-
expected_3_12_1a3,
217+
json!(expected_manager),
218+
json!(expected_3_9_9),
219+
json!(expected_virtual_env),
220+
json!(expected_3_12_1),
221+
json!(expected_3_13_dev),
222+
json!(expected_3_12_1a3),
92223
],
93224
&dispatcher,
94225
)

0 commit comments

Comments
 (0)