diff --git a/crates/pet-conda/src/environment_locations.rs b/crates/pet-conda/src/environment_locations.rs index 744da92c..2a4efffb 100644 --- a/crates/pet-conda/src/environment_locations.rs +++ b/crates/pet-conda/src/environment_locations.rs @@ -253,8 +253,6 @@ pub fn get_known_conda_install_locations( env_vars: &EnvVariables, conda_executable: &Option, ) -> Vec { - use pet_fs::path::norm_case; - let user_profile = env_vars.userprofile.clone().unwrap_or_default(); let program_data = env_vars.programdata.clone().unwrap_or_default(); let all_user_profile = env_vars.allusersprofile.clone().unwrap_or_default(); diff --git a/crates/pet-conda/tests/ci_test.rs b/crates/pet-conda/tests/ci_test.rs index 03cb4d33..a10071f1 100644 --- a/crates/pet-conda/tests/ci_test.rs +++ b/crates/pet-conda/tests/ci_test.rs @@ -387,7 +387,7 @@ fn create_conda_env(mode: &CondaCreateEnvNameOrPath, python_version: Option String { +fn get_version(value: &str) -> String { // Regex to extract just the d.d.d version from the full version string let re = regex::Regex::new(r"\d+\.\d+\.\d+").unwrap(); let captures = re.captures(value).unwrap(); diff --git a/crates/pet-conda/tests/common.rs b/crates/pet-conda/tests/common.rs index 1a282083..fdc752c2 100644 --- a/crates/pet-conda/tests/common.rs +++ b/crates/pet-conda/tests/common.rs @@ -46,6 +46,22 @@ pub struct TestEnvironment { root: Option, globals_locations: Vec, } + +impl Environment for TestEnvironment { + fn get_env_var(&self, key: String) -> Option { + self.vars.get(&key).cloned() + } + fn get_root(&self) -> Option { + self.root.clone() + } + fn get_user_home(&self) -> Option { + self.home.clone() + } + fn get_know_global_search_locations(&self) -> Vec { + self.globals_locations.clone() + } +} + #[allow(dead_code)] pub fn create_test_environment( vars: HashMap, @@ -53,20 +69,6 @@ pub fn create_test_environment( globals_locations: Vec, root: Option, ) -> TestEnvironment { - impl Environment for TestEnvironment { - fn get_env_var(&self, key: String) -> Option { - self.vars.get(&key).cloned() - } - fn get_root(&self) -> Option { - self.root.clone() - } - fn get_user_home(&self) -> Option { - self.home.clone() - } - fn get_know_global_search_locations(&self) -> Vec { - self.globals_locations.clone() - } - } TestEnvironment { vars, home, diff --git a/crates/pet-core/src/python_environment.rs b/crates/pet-core/src/python_environment.rs index 9531a70a..a611ced8 100644 --- a/crates/pet-core/src/python_environment.rs +++ b/crates/pet-core/src/python_environment.rs @@ -416,7 +416,10 @@ pub fn get_environment_key(env: &PythonEnvironment) -> Option { #[cfg(test)] mod tests { - use super::*; + #[cfg(windows)] + use super::{get_shortest_executable, PythonEnvironmentKind}; + #[cfg(windows)] + use std::path::PathBuf; #[test] #[cfg(windows)] diff --git a/crates/pet-poetry/Cargo.toml b/crates/pet-poetry/Cargo.toml index 2a6b4ded..9cf2577f 100644 --- a/crates/pet-poetry/Cargo.toml +++ b/crates/pet-poetry/Cargo.toml @@ -21,3 +21,6 @@ regex = "1.10.4" sha2 = "0.10.6" base64 = "0.22.0" toml = "0.8.14" + +[features] +ci = [] diff --git a/crates/pet-poetry/tests/common.rs b/crates/pet-poetry/tests/common.rs index 4432561b..85676686 100644 --- a/crates/pet-poetry/tests/common.rs +++ b/crates/pet-poetry/tests/common.rs @@ -35,25 +35,27 @@ pub struct TestEnvironment { home: Option, root: Option, } + +impl Environment for TestEnvironment { + fn get_env_var(&self, key: String) -> Option { + self.vars.get(&key).cloned() + } + fn get_root(&self) -> Option { + self.root.clone() + } + fn get_user_home(&self) -> Option { + self.home.clone() + } + fn get_know_global_search_locations(&self) -> Vec { + vec![] + } +} + #[allow(dead_code)] pub fn create_test_environment( vars: HashMap, home: Option, root: Option, ) -> TestEnvironment { - impl Environment for TestEnvironment { - fn get_env_var(&self, key: String) -> Option { - self.vars.get(&key).cloned() - } - fn get_root(&self) -> Option { - self.root.clone() - } - fn get_user_home(&self) -> Option { - self.home.clone() - } - fn get_know_global_search_locations(&self) -> Vec { - vec![] - } - } TestEnvironment { vars, home, root } } diff --git a/crates/pet-poetry/tests/config_test.rs b/crates/pet-poetry/tests/config_test.rs index 92172b6c..ab424c56 100644 --- a/crates/pet-poetry/tests/config_test.rs +++ b/crates/pet-poetry/tests/config_test.rs @@ -83,7 +83,6 @@ fn global_config_with_specific_values() { #[cfg(unix)] #[cfg_attr(any(feature = "ci",), test)] #[allow(dead_code)] - fn local_config_with_specific_values() { use std::path::PathBuf; diff --git a/crates/pet-pyenv/tests/common.rs b/crates/pet-pyenv/tests/common.rs index a6ab9d79..69ed9883 100644 --- a/crates/pet-pyenv/tests/common.rs +++ b/crates/pet-pyenv/tests/common.rs @@ -34,6 +34,22 @@ pub struct TestEnvironment { root: Option, globals_locations: Vec, } + +impl Environment for TestEnvironment { + fn get_env_var(&self, key: String) -> Option { + self.vars.get(&key).cloned() + } + fn get_root(&self) -> Option { + self.root.clone() + } + fn get_user_home(&self) -> Option { + self.home.clone() + } + fn get_know_global_search_locations(&self) -> Vec { + self.globals_locations.clone() + } +} + #[allow(dead_code)] pub fn create_test_environment( vars: HashMap, @@ -41,20 +57,6 @@ pub fn create_test_environment( globals_locations: Vec, root: Option, ) -> TestEnvironment { - impl Environment for TestEnvironment { - fn get_env_var(&self, key: String) -> Option { - self.vars.get(&key).cloned() - } - fn get_root(&self) -> Option { - self.root.clone() - } - fn get_user_home(&self) -> Option { - self.home.clone() - } - fn get_know_global_search_locations(&self) -> Vec { - self.globals_locations.clone() - } - } TestEnvironment { vars, home, diff --git a/crates/pet-pyenv/tests/pyenv_test.rs b/crates/pet-pyenv/tests/pyenv_test.rs index 210d42e3..33b461c7 100644 --- a/crates/pet-pyenv/tests/pyenv_test.rs +++ b/crates/pet-pyenv/tests/pyenv_test.rs @@ -164,7 +164,6 @@ fn find_pyenv_envs() { home.to_str().unwrap(), ".pyenv/versions/3.9.9/bin/python", ])]), - ..Default::default() }; let expected_virtual_env = PythonEnvironment { display_name: None, @@ -186,7 +185,6 @@ fn find_pyenv_envs() { home.to_str().unwrap(), ".pyenv/versions/my-virtual-env/bin/python", ])]), - ..Default::default() }; let expected_3_12_1 = PythonEnvironment { display_name: None, @@ -208,7 +206,6 @@ fn find_pyenv_envs() { home.to_str().unwrap(), ".pyenv/versions/3.12.1/bin/python", ])]), - ..Default::default() }; let expected_3_13_dev = PythonEnvironment { display_name: None, @@ -230,7 +227,6 @@ fn find_pyenv_envs() { home.to_str().unwrap(), ".pyenv/versions/3.13-dev/bin/python", ])]), - ..Default::default() }; let expected_3_12_1a3 = PythonEnvironment { display_name: None, @@ -252,7 +248,6 @@ fn find_pyenv_envs() { home.to_str().unwrap(), ".pyenv/versions/3.12.1a3/bin/python", ])]), - ..Default::default() }; let expected_no_gil = PythonEnvironment { display_name: None, @@ -274,7 +269,6 @@ fn find_pyenv_envs() { home.to_str().unwrap(), ".pyenv/versions/nogil-3.9.10-1/bin/python", ])]), - ..Default::default() }; let expected_pypy = PythonEnvironment { display_name: None, @@ -296,7 +290,6 @@ fn find_pyenv_envs() { home.to_str().unwrap(), ".pyenv/versions/pypy3.9-7.3.15/bin/python", ])]), - ..Default::default() }; let expected_conda_root = PythonEnvironment { @@ -310,7 +303,6 @@ fn find_pyenv_envs() { manager: Some(expected_conda_manager.clone()), arch: Some(Architecture::X64), symlinks: Some(vec![conda_dir.join("bin").join("python")]), - ..Default::default() }; let expected_conda_one = PythonEnvironment { display_name: None, @@ -323,7 +315,6 @@ fn find_pyenv_envs() { manager: Some(expected_conda_manager.clone()), arch: None, symlinks: Some(vec![conda_dir.join("envs").join("one").join("python")]), - ..Default::default() }; let expected_conda_two = PythonEnvironment { display_name: None, @@ -336,7 +327,6 @@ fn find_pyenv_envs() { manager: Some(expected_conda_manager.clone()), symlinks: Some(vec![conda_dir.join("envs").join("two").join("python")]), arch: None, - ..Default::default() }; let mut expected_envs = vec![ @@ -406,7 +396,6 @@ fn resolve_pyenv_environment() { manager: Some(expected_manager.clone()), arch: None, symlinks: Some(vec![executable]), - ..Default::default() }; let expected_virtual_env = PythonEnvironment { display_name: None, @@ -428,7 +417,6 @@ fn resolve_pyenv_environment() { home.to_str().unwrap(), ".pyenv/versions/my-virtual-env/bin/python", ])]), - ..Default::default() }; // Resolve regular Python installs in Pyenv diff --git a/crates/pet-windows-registry/src/environments.rs b/crates/pet-windows-registry/src/environments.rs index 4a2f8c6d..558473a7 100644 --- a/crates/pet-windows-registry/src/environments.rs +++ b/crates/pet-windows-registry/src/environments.rs @@ -19,13 +19,18 @@ use std::{path::PathBuf, sync::Arc}; #[cfg(windows)] use winreg::RegKey; +#[cfg(windows)] +use log::{trace, warn}; +#[cfg(windows)] +use pet_conda::utils::is_conda_env; +#[cfg(windows)] +use pet_fs::path::norm_case; + #[cfg(windows)] pub fn get_registry_pythons( conda_locator: &Arc, reporter: &Option<&dyn Reporter>, ) -> LocatorResult { - use log::{trace, warn}; - let mut environments = vec![]; let mut managers: Vec = vec![]; @@ -88,10 +93,6 @@ fn get_registry_pythons_from_key_for_company( conda_locator: &Arc, reporter: &Option<&dyn Reporter>, ) -> LocatorResult { - use log::{trace, warn}; - use pet_conda::utils::is_conda_env; - use pet_fs::path::norm_case; - let mut environments = vec![]; // let company_display_name: Option = company_key.get_value("DisplayName").ok(); for installed_python in company_key.enum_keys().filter_map(Result::ok) { diff --git a/crates/pet-windows-store/src/environments.rs b/crates/pet-windows-store/src/environments.rs index 2d520e52..faf2b311 100644 --- a/crates/pet-windows-store/src/environments.rs +++ b/crates/pet-windows-store/src/environments.rs @@ -21,6 +21,13 @@ use std::path::PathBuf; #[cfg(windows)] use winreg::RegKey; +#[cfg(windows)] +use crate::environment_locations::get_search_locations; +#[cfg(windows)] +use log::{trace, warn}; +#[cfg(windows)] +use std::collections::HashMap; + #[cfg(windows)] lazy_static! { static ref PYTHON_SOFTWARE_FOUNDATION_FOLDER_VERSION: Regex = Regex::new( @@ -92,10 +99,6 @@ impl PotentialPython { #[cfg(windows)] pub fn list_store_pythons(environment: &EnvVariables) -> Option> { - use crate::environment_locations::get_search_locations; - use log::{trace, warn}; - use std::collections::HashMap; - let mut python_envs: Vec = vec![]; let apps_path = get_search_locations(environment)?; let hkcu = winreg::RegKey::predef(winreg::enums::HKEY_CURRENT_USER); @@ -238,8 +241,6 @@ struct StorePythonInfo { #[cfg(windows)] fn get_package_display_name_and_location(name: &String, hkcu: &RegKey) -> Option { - use log::trace; - if let Some(name) = get_package_full_name_from_registry(name, hkcu) { let key = format!("Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\AppModel\\Repository\\Packages\\{}", name); trace!("Opening registry key {:?}", key); @@ -258,8 +259,6 @@ fn get_package_display_name_and_location(name: &String, hkcu: &RegKey) -> Option #[cfg(windows)] fn get_package_full_name_from_registry(name: &String, hkcu: &RegKey) -> Option { - use log::trace; - let key = format!("Software\\Classes\\Local Settings\\Software\\Microsoft\\Windows\\CurrentVersion\\AppModel\\SystemAppData\\{}\\Schemas", name); trace!("Opening registry key {:?}", key); let package_key = hkcu.open_subkey(key).ok()?; diff --git a/crates/pet-windows-store/src/lib.rs b/crates/pet-windows-store/src/lib.rs index 233974c3..c1cc69e1 100644 --- a/crates/pet-windows-store/src/lib.rs +++ b/crates/pet-windows-store/src/lib.rs @@ -16,6 +16,13 @@ use pet_core::{os_environment::Environment, Locator}; use std::path::Path; use std::sync::{Arc, Mutex}; +#[cfg(windows)] +use pet_core::python_environment::PythonEnvironmentBuilder; +#[cfg(windows)] +use pet_virtualenv::is_virtualenv; +#[cfg(windows)] +use std::path::PathBuf; + pub fn is_windows_app_folder_in_program_files(path: &Path) -> bool { path.to_str().unwrap_or_default().to_string().to_lowercase()[1..] .starts_with(":\\program files\\windowsapps") @@ -61,11 +68,6 @@ impl Locator for WindowsStore { #[cfg(windows)] fn try_from(&self, env: &PythonEnv) -> Option { - use std::path::PathBuf; - - use pet_core::python_environment::PythonEnvironmentBuilder; - use pet_virtualenv::is_virtualenv; - // Assume we create a virtual env from a python install, // Then the exe in the virtual env bin will be a symlink to the homebrew python install. // Hence the first part of the condition will be true, but the second part will be false. diff --git a/crates/pet/tests/ci_poetry.rs b/crates/pet/tests/ci_poetry.rs index 8c14d3b0..277c07a0 100644 --- a/crates/pet/tests/ci_poetry.rs +++ b/crates/pet/tests/ci_poetry.rs @@ -40,8 +40,10 @@ fn verify_ci_poetry_global() { let environment = EnvironmentApi::new(); let conda_locator = Arc::new(Conda::from(&environment)); let poetry_locator = Arc::new(Poetry::from(&environment)); - let mut config = Configuration::default(); - config.workspace_directories = Some(vec![workspace_dir.clone()]); + let config = Configuration { + workspace_directories: Some(vec![workspace_dir.clone()]), + ..Default::default() + }; let locators = create_locators(conda_locator.clone(), poetry_locator.clone(), &environment); for locator in locators.iter() { locator.configure(&config); @@ -110,8 +112,10 @@ fn verify_ci_poetry_project() { let environment = EnvironmentApi::new(); let conda_locator = Arc::new(Conda::from(&environment)); let poetry_locator = Arc::new(Poetry::from(&environment)); - let mut config = Configuration::default(); - config.workspace_directories = Some(vec![workspace_dir.clone()]); + let config = Configuration { + workspace_directories: Some(vec![workspace_dir.clone()]), + ..Default::default() + }; let locators = create_locators(conda_locator.clone(), poetry_locator.clone(), &environment); for locator in locators.iter() { locator.configure(&config); diff --git a/crates/pet/tests/ci_test.rs b/crates/pet/tests/ci_test.rs index 7c315efa..7c2a27fa 100644 --- a/crates/pet/tests/ci_test.rs +++ b/crates/pet/tests/ci_test.rs @@ -75,8 +75,10 @@ fn verify_validity_of_discovered_envs() { let environment = EnvironmentApi::new(); let conda_locator = Arc::new(Conda::from(&environment)); let poetry_locator = Arc::new(Poetry::from(&environment)); - let mut config = Configuration::default(); - config.workspace_directories = Some(vec![workspace_dir.clone()]); + let config = Configuration { + workspace_directories: Some(vec![workspace_dir.clone()]), + ..Default::default() + }; let locators = create_locators(conda_locator.clone(), poetry_locator.clone(), &environment); for locator in locators.iter() { locator.configure(&config); @@ -283,19 +285,16 @@ fn verify_validity_of_interpreter_info(environment: PythonEnvironment) { } } if let Some(prefix) = environment.clone().prefix { - if interpreter_info.clone().executable == "/usr/local/python/current/bin/python" + if (interpreter_info.clone().executable == "/usr/local/python/current/bin/python" && (prefix.to_str().unwrap() == "/usr/local/python/current" && interpreter_info.clone().sys_prefix == "/usr/local/python/3.10.13") || (prefix.to_str().unwrap() == "/usr/local/python/3.10.13" - && interpreter_info.clone().sys_prefix == "/usr/local/python/current") - { - // known issue https://github.com/microsoft/python-environment-tools/issues/64 - } else if interpreter_info.clone().executable - == "/home/codespace/.python/current/bin/python" - && (prefix.to_str().unwrap() == "/home/codespace/.python/current" - && interpreter_info.clone().sys_prefix == "/usr/local/python/3.10.13") - || (prefix.to_str().unwrap() == "/usr/local/python/3.10.13" - && interpreter_info.clone().sys_prefix == "/home/codespace/.python/current") + && interpreter_info.clone().sys_prefix == "/usr/local/python/current")) + || (interpreter_info.clone().executable == "/home/codespace/.python/current/bin/python" + && (prefix.to_str().unwrap() == "/home/codespace/.python/current" + && interpreter_info.clone().sys_prefix == "/usr/local/python/3.10.13") + || (prefix.to_str().unwrap() == "/usr/local/python/3.10.13" + && interpreter_info.clone().sys_prefix == "/home/codespace/.python/current")) { // known issue https://github.com/microsoft/python-environment-tools/issues/64 } else { @@ -487,20 +486,19 @@ fn compare_environments(actual: PythonEnvironment, expected: PythonEnvironment, actual.version = expected.clone().version; if let Some(prefix) = expected.clone().prefix { - if actual.clone().executable == Some(PathBuf::from("/usr/local/python/current/bin/python")) + if (actual.clone().executable + == Some(PathBuf::from("/usr/local/python/current/bin/python")) && (prefix.to_str().unwrap() == "/usr/local/python/current" && actual.clone().prefix == Some(PathBuf::from("/usr/local/python/3.10.13"))) || (prefix.to_str().unwrap() == "/usr/local/python/3.10.13" - && actual.clone().prefix == Some(PathBuf::from("/usr/local/python/current"))) - { - // known issue https://github.com/microsoft/python-environment-tools/issues/64 - actual.prefix = expected.clone().prefix; - } else if actual.clone().executable - == Some(PathBuf::from("/home/codespace/.python/current/bin/python")) - && (prefix.to_str().unwrap() == "/home/codespace/.python/current" - && actual.clone().prefix == Some(PathBuf::from("/usr/local/python/3.10.13"))) - || (prefix.to_str().unwrap() == "/usr/local/python/3.10.13" - && actual.clone().prefix == Some(PathBuf::from("/home/codespace/.python/current"))) + && actual.clone().prefix == Some(PathBuf::from("/usr/local/python/current")))) + || (actual.clone().executable + == Some(PathBuf::from("/home/codespace/.python/current/bin/python")) + && (prefix.to_str().unwrap() == "/home/codespace/.python/current" + && actual.clone().prefix == Some(PathBuf::from("/usr/local/python/3.10.13"))) + || (prefix.to_str().unwrap() == "/usr/local/python/3.10.13" + && actual.clone().prefix + == Some(PathBuf::from("/home/codespace/.python/current")))) { // known issue https://github.com/microsoft/python-environment-tools/issues/64 actual.prefix = expected.clone().prefix; @@ -591,8 +589,10 @@ fn verify_we_can_get_same_env_info_using_resolve_with_exe( let os_environment = EnvironmentApi::new(); let conda_locator = Arc::new(Conda::from(&os_environment)); let poetry_locator = Arc::new(Poetry::from(&os_environment)); - let mut config = Configuration::default(); - config.workspace_directories = Some(vec![workspace_dir.clone()]); + let config = Configuration { + workspace_directories: Some(vec![workspace_dir.clone()]), + ..Default::default() + }; let locators = create_locators( conda_locator.clone(), poetry_locator.clone(), @@ -720,13 +720,13 @@ fn get_python_run_command(env: &PythonEnvironment) -> Vec { "python".to_string(), ] } else if let Some(prefix) = env.prefix.clone() { - return vec![ + vec![ conda_exe, "run".to_string(), "-p".to_string(), prefix.to_str().unwrap_or_default().to_string(), "python".to_string(), - ]; + ] } else { panic!("Conda environment without name or prefix") } @@ -741,8 +741,8 @@ fn get_python_run_command(env: &PythonEnvironment) -> Vec { } } -fn get_python_interpreter_info(cli: &Vec) -> InterpreterInfo { - let mut cli = cli.clone(); +fn get_python_interpreter_info(cli: &[String]) -> InterpreterInfo { + let mut cli = cli.to_owned(); cli.push( resolve_test_path(&["interpreterInfo.py"]) .to_str() diff --git a/crates/pet/tests/common.rs b/crates/pet/tests/common.rs index 634fc59d..07d15da9 100644 --- a/crates/pet/tests/common.rs +++ b/crates/pet/tests/common.rs @@ -22,12 +22,12 @@ pub fn resolve_test_path(paths: &[&str]) -> PathBuf { } #[allow(dead_code)] -pub fn does_version_match(version: &String, expected_version: &String) -> bool { +pub fn does_version_match(version: &str, expected_version: &str) -> bool { let version = get_version(version); expected_version.starts_with(&version) } -fn get_version(value: &String) -> String { +fn get_version(value: &str) -> String { // Regex to extract just the d.d.d version from the full version string let captures = PYTHON_VERSION.captures(value).unwrap(); let version = captures.get(1).unwrap().as_str().to_string(); @@ -39,6 +39,6 @@ fn get_version(value: &String) -> String { } #[allow(dead_code)] -pub fn is_valid_version(value: &String) -> bool { +pub fn is_valid_version(value: &str) -> bool { PYTHON_FULLVERSION.is_match(value) }