Skip to content

Use structs instead of loose json in native test #23394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions native_locator/src/conda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ pub fn find_and_report(
let conda_binary = find_conda_binary(environment);
match conda_binary {
Some(conda_binary) => {
let params = messaging::EnvManager::new(
let env_manager = messaging::EnvManager::new(
conda_binary.clone(),
get_conda_version(&conda_binary),
EnvManagerType::Conda,
);
dispatcher.report_environment_manager(params);
dispatcher.report_environment_manager(env_manager.clone());

let envs = get_distinct_conda_envs(&conda_binary, environment);
for env in envs {
Expand All @@ -401,7 +401,7 @@ pub fn find_and_report(
get_conda_python_version(&env.path),
Some(env.path.clone()),
Some(env.path.clone()),
None,
Some(env_manager.clone()),
if env.named {
Some(vec![
conda_binary.to_string_lossy().to_string(),
Expand Down
14 changes: 13 additions & 1 deletion native_locator/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,26 @@ fn compare_json(expected: &Value, actual: &Value) -> bool {
}

if expected.is_object() {
if expected.as_object().is_none() && actual.as_object().is_none() {
return true;
}

if expected.as_object().is_none() && actual.as_object().is_some() {
return false;
}
if expected.as_object().is_some() && actual.as_object().is_none() {
return false;
}

let expected = expected.as_object().unwrap();
let actual = actual.as_object().unwrap();

for (key, value) in expected.iter() {
if !actual.contains_key(key) {
return false;
}

println!("\nCompare Key {:?}", key);
println!("\nCompare Key {:?}", actual.get(key).is_none());
if !compare_json(value, actual.get(key).unwrap()) {
return false;
}
Expand Down
21 changes: 17 additions & 4 deletions native_locator/tests/common_python_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn find_python_in_path_this() {
assert_messages, create_test_dispatcher, create_test_environment, join_test_paths,
test_file_path,
};
use python_finder::common_python;
use python_finder::{common_python, messaging::PythonEnvironment};
use serde_json::json;
use std::collections::HashMap;

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

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

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

assert_eq!(dispatcher.messages.len(), 1);
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()});
assert_messages(&[expected_json], &dispatcher);
let env = PythonEnvironment {
env_manager: None,
project_path: None,
name: None,
python_executable_path: Some(unix_python_exe.clone()),
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()),
sys_prefix_path: Some(unix_python.clone()),
};
assert_messages(&[json!(env)], &dispatcher);
}
63 changes: 55 additions & 8 deletions native_locator/tests/conda_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ fn find_conda_exe_and_empty_envs() {
assert_messages, create_test_dispatcher, create_test_environment, join_test_paths,
test_file_path,
};
use python_finder::conda;
use python_finder::{
conda,
messaging::{EnvManager, EnvManagerType},
};
use serde_json::json;
use std::{collections::HashMap, path::PathBuf};
let conda_dir = test_file_path(&["tests/unix/conda_without_envs"]);
Expand All @@ -47,8 +50,12 @@ fn find_conda_exe_and_empty_envs() {
conda::find_and_report(&mut dispatcher, &known);

let conda_exe = join_test_paths(&[conda_dir.clone().to_str().unwrap(), "conda"]);
let expected_json = json!({"executablePath":conda_exe.clone(),"version":null, "tool": "conda"});
assert_messages(&[expected_json], &dispatcher)
let expected_conda_manager = EnvManager {
executable_path: conda_exe.clone(),
version: None,
tool: EnvManagerType::Conda,
};
assert_messages(&[json!(expected_conda_manager)], &dispatcher)
}
#[test]
#[cfg(unix)]
Expand All @@ -58,6 +65,7 @@ fn finds_two_conda_envs_from_txt() {
test_file_path,
};
use python_finder::conda;
use python_finder::messaging::{EnvManager, EnvManagerType, PythonEnvironment};
use serde_json::json;
use std::collections::HashMap;
use std::fs;
Expand Down Expand Up @@ -90,12 +98,51 @@ fn finds_two_conda_envs_from_txt() {
let conda_1_exe = join_test_paths(&[conda_1.clone().to_str().unwrap(), "python"]);
let conda_2_exe = join_test_paths(&[conda_2.clone().to_str().unwrap(), "python"]);

let expected_conda_env =
json!({ "executablePath": conda_exe.clone(), "version": null, "tool": "conda"});
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"]});
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"]});
let expected_conda_manager = EnvManager {
executable_path: conda_exe.clone(),
version: None,
tool: EnvManagerType::Conda,
};
let expected_conda_1 = PythonEnvironment {
name: Some("one".to_string()),
project_path: None,
python_executable_path: Some(conda_1_exe.clone()),
category: python_finder::messaging::PythonEnvironmentCategory::Conda,
version: Some("10.0.1".to_string()),
env_path: Some(conda_1.clone()),
sys_prefix_path: Some(conda_1.clone()),
env_manager: Some(expected_conda_manager.clone()),
python_run_command: Some(vec![
conda_exe.clone().to_str().unwrap().to_string(),
"run".to_string(),
"-n".to_string(),
"one".to_string(),
"python".to_string(),
]),
};
let expected_conda_2 = PythonEnvironment {
name: Some("two".to_string()),
project_path: None,
python_executable_path: Some(conda_2_exe.clone()),
category: python_finder::messaging::PythonEnvironmentCategory::Conda,
version: None,
env_path: Some(conda_2.clone()),
sys_prefix_path: Some(conda_2.clone()),
env_manager: Some(expected_conda_manager.clone()),
python_run_command: Some(vec![
conda_exe.clone().to_str().unwrap().to_string(),
"run".to_string(),
"-n".to_string(),
"two".to_string(),
"python".to_string(),
]),
};
assert_messages(
&[expected_conda_1, expected_conda_env, expected_conda_2],
&[
json!(expected_conda_1),
json!(expected_conda_manager),
json!(expected_conda_2),
],
&dispatcher,
)
}
159 changes: 145 additions & 14 deletions native_locator/tests/pyenv_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ fn find_pyenv_envs() {
assert_messages, create_test_dispatcher, create_test_environment, join_test_paths,
test_file_path,
};
use python_finder::pyenv;
use python_finder::{
messaging::{EnvManager, EnvManagerType, PythonEnvironment},
pyenv,
};
use serde_json::json;
use std::{collections::HashMap, path::PathBuf};

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

assert_eq!(dispatcher.messages.len(), 6);
let expected_manager =
json!({ "executablePath": pyenv_exe.clone(), "version": null, "tool": "pyenv" });
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});
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});
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});
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});
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});
let expected_manager = EnvManager {
executable_path: pyenv_exe.clone(),
version: None,
tool: EnvManagerType::Pyenv,
};
let expected_3_9_9 = json!(PythonEnvironment {
project_path: None,
name: None,
python_executable_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.9.9/bin/python"
])),
python_run_command: Some(vec![join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.9.9/bin/python"
])
.to_str()
.unwrap()
.to_string()]),
category: python_finder::messaging::PythonEnvironmentCategory::Pyenv,
version: Some("3.9.9".to_string()),
env_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.9.9"
])),
sys_prefix_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.9.9"
])),
env_manager: Some(expected_manager.clone())
});
let expected_virtual_env = PythonEnvironment {
project_path: None,
name: Some("my-virtual-env".to_string()),
python_executable_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/my-virtual-env/bin/python",
])),
python_run_command: Some(vec![join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/my-virtual-env/bin/python",
])
.to_str()
.unwrap()
.to_string()]),
category: python_finder::messaging::PythonEnvironmentCategory::PyenvVirtualEnv,
version: Some("3.10.13".to_string()),
env_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/my-virtual-env",
])),
sys_prefix_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/my-virtual-env",
])),
env_manager: Some(expected_manager.clone()),
};
let expected_3_12_1 = PythonEnvironment {
project_path: None,
name: None,
python_executable_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.12.1/bin/python",
])),
python_run_command: Some(vec![join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.12.1/bin/python",
])
.to_str()
.unwrap()
.to_string()]),
category: python_finder::messaging::PythonEnvironmentCategory::Pyenv,
version: Some("3.12.1".to_string()),
env_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.12.1",
])),
sys_prefix_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.12.1",
])),
env_manager: Some(expected_manager.clone()),
};
let expected_3_13_dev = PythonEnvironment {
project_path: None,
name: None,
python_executable_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.13-dev/bin/python",
])),
python_run_command: Some(vec![join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.13-dev/bin/python",
])
.to_str()
.unwrap()
.to_string()]),
category: python_finder::messaging::PythonEnvironmentCategory::Pyenv,
version: Some("3.13-dev".to_string()),
env_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.13-dev",
])),
sys_prefix_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.13-dev",
])),
env_manager: Some(expected_manager.clone()),
};
let expected_3_12_1a3 = PythonEnvironment {
project_path: None,
name: None,
python_executable_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.12.1a3/bin/python",
])),
python_run_command: Some(vec![join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.12.1a3/bin/python",
])
.to_str()
.unwrap()
.to_string()]),
category: python_finder::messaging::PythonEnvironmentCategory::Pyenv,
version: Some("3.12.1a3".to_string()),
env_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.12.1a3",
])),
sys_prefix_path: Some(join_test_paths(&[
home.to_str().unwrap(),
".pyenv/versions/3.12.1a3",
])),
env_manager: Some(expected_manager.clone()),
};
assert_messages(
&[
expected_manager,
expected_3_9_9,
expected_virtual_env,
expected_3_12_1,
expected_3_13_dev,
expected_3_12_1a3,
json!(expected_manager),
json!(expected_3_9_9),
json!(expected_virtual_env),
json!(expected_3_12_1),
json!(expected_3_13_dev),
json!(expected_3_12_1a3),
],
&dispatcher,
)
Expand Down
Loading