Skip to content
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

Fix linters and tests #4

Merged
merged 6 commits into from
Jun 3, 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 .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ on:
- release*

jobs:
unit-tests:
name: Unit Tests
tests:
name: Tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
targets: ${{ matrix.target }}

- name: Cargo Fetch
run: cargo fetch
run: cargo fetch

- name: Run Tests
run: cargo test --frozen --all-features
14 changes: 7 additions & 7 deletions crates/pet-conda/src/conda_rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn get_conda_rc_search_paths(environment: &CondaEnvironmentVariables) -> Vec<Pat
"C:\\ProgramData\\conda\\condarc.d",
]
.iter()
.map(|p| PathBuf::from(p))
.map(PathBuf::from)
.collect();

if let Some(ref conda_root) = environment.conda_root {
Expand Down Expand Up @@ -61,7 +61,7 @@ fn get_conda_rc_search_paths(environment: &CondaEnvironmentVariables) -> Vec<Pat

#[cfg(unix)]
fn get_conda_rc_search_paths(environment: &CondaEnvironmentVariables) -> Vec<PathBuf> {
let mut search_paths: Vec<PathBuf> = vec![
let mut search_paths: Vec<PathBuf> = [
"/etc/conda/.condarc",
"/etc/conda/condarc",
"/etc/conda/condarc.d",
Expand All @@ -70,13 +70,13 @@ fn get_conda_rc_search_paths(environment: &CondaEnvironmentVariables) -> Vec<Pat
"/var/lib/conda/condarc.d",
]
.iter()
.map(|p| PathBuf::from(p))
.map(PathBuf::from)
.map(|p| {
// This only applies in tests.
// We need this, as the root folder cannot be mocked.
if let Some(ref root) = environment.root {
// Strip the first `/` (this path is only for testing purposes)
root.join(p.to_string_lossy()[1..].to_string())
root.join(&p.to_string_lossy()[1..])
} else {
p
}
Expand Down Expand Up @@ -147,8 +147,8 @@ fn parse_conda_rc(conda_rc: &PathBuf) -> Option<Condarc> {
continue;
}
if start_consuming_values {
if line.trim().starts_with("-") {
if let Some(env_dir) = line.splitn(2, '-').nth(1) {
if line.trim().starts_with('-') {
if let Some(env_dir) = line.split_once('-').map(|x| x.1) {
// Directories in conda-rc are where `envs` are stored.
env_dirs.push(PathBuf::from(env_dir.trim()).join("envs"));
}
Expand All @@ -158,5 +158,5 @@ fn parse_conda_rc(conda_rc: &PathBuf) -> Option<Condarc> {
}
}
}
return Some(Condarc { env_dirs });
Some(Condarc { env_dirs })
}
18 changes: 9 additions & 9 deletions crates/pet-conda/src/environment_locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
pub fn get_conda_environment_paths(environment: &CondaEnvironmentVariables) -> Vec<PathBuf> {
let mut env_paths = get_conda_envs_from_environment_txt(environment)
.iter()
.map(|e| PathBuf::from(e))
.map(PathBuf::from)
.collect::<Vec<PathBuf>>();

let mut env_paths_from_conda_rc = get_conda_environment_paths_from_conda_rc(environment);
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn get_conda_environment_paths_from_known_paths(
if let Ok(entries) = fs::read_dir(full_path) {
for entry in entries.filter_map(Result::ok) {
let path = entry.path();
if let Some(meta) = fs::metadata(&path).ok() {
if let Ok(meta) = fs::metadata(&path) {
if meta.is_dir() {
env_paths.push(path);
}
Expand All @@ -92,7 +92,7 @@ pub fn get_conda_environment_paths_from_known_paths(
}
}
}
return env_paths;
env_paths
}

pub fn get_environments(conda_dir: &Path) -> Vec<PathBuf> {
Expand Down Expand Up @@ -179,10 +179,10 @@ pub fn get_known_conda_install_locations(environment: &CondaEnvironmentVariables
PathBuf::from("/miniforge3"),
];
if let Some(ref home) = environment.home {
known_paths.push(PathBuf::from(home.clone()).join("anaconda3"));
known_paths.push(PathBuf::from(home.clone()).join("miniconda3"));
known_paths.push(PathBuf::from(home.clone()).join("miniforge3"));
known_paths.push(PathBuf::from(home).join(".conda"));
known_paths.push(home.clone().join("anaconda3"));
known_paths.push(home.clone().join("miniconda3"));
known_paths.push(home.clone().join("miniforge3"));
known_paths.push(home.join(".conda"));
}
known_paths.append(get_known_conda_locations(environment).as_mut());
known_paths.dedup();
Expand Down Expand Up @@ -224,8 +224,8 @@ pub fn get_known_conda_locations(environment: &CondaEnvironmentVariables) -> Vec
PathBuf::from("/miniconda3/bin"),
];
if let Some(ref home) = environment.home {
known_paths.push(PathBuf::from(home.clone()).join("anaconda3/bin"));
known_paths.push(PathBuf::from(home).join("miniconda3/bin"));
known_paths.push(home.clone().join("anaconda3/bin"));
known_paths.push(home.join("miniconda3/bin"));
}
known_paths.append(&mut environment.known_global_search_locations.clone());
known_paths
Expand Down
42 changes: 19 additions & 23 deletions crates/pet-conda/src/environments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct CondaEnvironment {

impl CondaEnvironment {
pub fn from(path: &Path, manager: &Option<CondaManager>) -> Option<Self> {
get_conda_environment_info(&path.into(), manager)
get_conda_environment_info(path, manager)
}

pub fn to_python_environment(
Expand All @@ -38,15 +38,15 @@ impl CondaEnvironment {
if is_conda_install(&self.prefix) {
name = Some("base".to_string());
} else {
name = match self.prefix.file_name() {
Some(name) => Some(name.to_str().unwrap_or_default().to_string()),
None => None,
};
name = self
.prefix
.file_name()
.map(|name| name.to_str().unwrap_or_default().to_string());
}
// if the conda install folder is parent of the env folder, then we can use named activation.
// E.g. conda env is = <conda install>/envs/<env name>
// Then we can use `<conda install>/bin/conda activate -n <env name>`
if !self.prefix.starts_with(&conda_dir) {
if !self.prefix.starts_with(conda_dir) {
name = None;
}
// This is a root env.
Expand All @@ -62,7 +62,7 @@ impl CondaEnvironment {
}
}
fn get_conda_environment_info(
env_path: &PathBuf,
env_path: &Path,
manager: &Option<CondaManager>,
) -> Option<CondaEnvironment> {
if !is_conda_env(env_path) {
Expand All @@ -74,35 +74,34 @@ fn get_conda_environment_info(
Some(manager) => Some(manager.conda_dir.clone()),
None => get_conda_installation_used_to_create_conda_env(env_path),
};
let env_path = env_path.clone();
if let Some(python_binary) = find_executable(&env_path) {
if let Some(package_info) = CondaPackageInfo::from(&env_path, &Package::Python) {
return Some(CondaEnvironment {
prefix: env_path,
if let Some(python_binary) = find_executable(env_path) {
if let Some(package_info) = CondaPackageInfo::from(env_path, &Package::Python) {
Some(CondaEnvironment {
prefix: env_path.into(),
executable: Some(python_binary),
version: Some(package_info.version),
conda_dir: conda_install_folder,
arch: package_info.arch,
});
})
} else {
// No python in this environment.
return Some(CondaEnvironment {
prefix: env_path,
Some(CondaEnvironment {
prefix: env_path.into(),
executable: Some(python_binary),
version: None,
conda_dir: conda_install_folder,
arch: None,
});
})
}
} else {
// No python in this environment.
return Some(CondaEnvironment {
prefix: env_path,
Some(CondaEnvironment {
prefix: env_path.into(),
executable: None,
version: None,
conda_dir: conda_install_folder,
arch: None,
});
})
}
}

Expand All @@ -114,7 +113,7 @@ fn get_conda_environment_info(
* Sometimes the cmd line contains the fully qualified path to the conda install folder.
* This function returns the path to the conda installation that was used to create the environment.
*/
pub fn get_conda_installation_used_to_create_conda_env(env_path: &PathBuf) -> Option<PathBuf> {
pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Option<PathBuf> {
// Possible the env_path is the root conda install folder.
if is_conda_install(env_path) {
return Some(env_path.to_path_buf());
Expand Down Expand Up @@ -162,9 +161,6 @@ pub fn get_activation_command(
manager: &EnvManager,
name: Option<String>,
) -> Option<Vec<String>> {
if env.executable.is_none() {
return None;
}
let conda_exe = manager.executable.to_str().unwrap_or_default().to_string();
if let Some(name) = name {
Some(vec![
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-conda/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn find_conda_binary_in_known_locations(
for location in known_locations {
for bin in &conda_bin_names {
let conda_path = location.join(bin);
if let Some(metadata) = std::fs::metadata(&conda_path).ok() {
if let Ok(metadata) = std::fs::metadata(&conda_path) {
if metadata.is_file() || metadata.is_symlink() {
return Some(conda_path);
}
Expand Down
9 changes: 5 additions & 4 deletions crates/pet-conda/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ fn get_conda_manager_from_env(env_path: &Path) -> Option<CondaManager> {

// We've been given an env thats been created using the -p flag.
// Get the conda install folder from the history file.
if let Some(conda_install_folder) =
get_conda_installation_used_to_create_conda_env(&env_path.to_path_buf())
{
if let Some(conda_install_folder) = get_conda_installation_used_to_create_conda_env(env_path) {
return get_conda_manager(&conda_install_folder);
}
None
Expand Down Expand Up @@ -159,7 +157,10 @@ impl Locator for Conda<'_> {
return Some(env);
}
} else {
error!("Unable to find conda Install folder conda install folder env: {:?}", env);
error!(
"Unable to find conda Install folder conda install folder env: {:?}",
env
);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions crates/pet-conda/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf

let history = path.join("history");
let package_entry = format!(":{}-", name.to_name());
if let Some(history_contents) = fs::read_to_string(&history).ok() {
if let Ok(history_contents) = fs::read_to_string(history) {
for line in history_contents
.lines()
.filter(|l| l.contains(&package_entry))
Expand All @@ -88,8 +88,8 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
// +conda-forge/osx-arm64::psutil-5.9.8-py312he37b823_0
// +conda-forge/osx-arm64::python-3.12.2-hdf0ec26_0_cpython
// +conda-forge/osx-arm64::python_abi-3.12-4_cp312
let regex = get_package_version_history_regex(&name);
if let Some(captures) = regex.captures(&line) {
let regex = get_package_version_history_regex(name);
if let Some(captures) = regex.captures(line) {
if let Some(version) = captures.get(1) {
if let Some(hash) = captures.get(2) {
let package_path = format!(
Expand All @@ -109,9 +109,9 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
// }
// 32bit channel is https://repo.anaconda.com/pkgs/main/win-32/
// 64bit channel is "channel": "https://repo.anaconda.com/pkgs/main/osx-arm64",
if let Some(contents) = read_to_string(&package_path).ok() {
if let Some(js) =
serde_json::from_str::<CondaMetaPackageStructure>(&contents).ok()
if let Ok(contents) = read_to_string(&package_path) {
if let Ok(js) =
serde_json::from_str::<CondaMetaPackageStructure>(&contents)
{
if let Some(channel) = js.channel {
if channel.ends_with("64") {
Expand Down Expand Up @@ -147,7 +147,7 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
);

let package_name = format!("{}-", name.to_name());
let regex = get_package_version_regex(&name);
let regex = get_package_version_regex(name);

// Fallback, slower approach of enumerating all files.
if let Ok(entries) = fs::read_dir(path) {
Expand All @@ -167,9 +167,9 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
// }
// 32bit channel is https://repo.anaconda.com/pkgs/main/win-32/
// 64bit channel is "channel": "https://repo.anaconda.com/pkgs/main/osx-arm64",
if let Some(contents) = read_to_string(&path).ok() {
if let Some(js) =
serde_json::from_str::<CondaMetaPackageStructure>(&contents).ok()
if let Ok(contents) = read_to_string(&path) {
if let Ok(js) =
serde_json::from_str::<CondaMetaPackageStructure>(&contents)
{
if let Some(channel) = js.channel {
if channel.ends_with("64") {
Expand Down
2 changes: 1 addition & 1 deletion crates/pet-conda/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn is_conda_install(path: &Path) -> bool {
// conda-meta must exist as this contains a mandatory `history` file.
// The root conda installation folder is also a conda environment (its the base environment).
pub fn is_conda_env(path: &Path) -> bool {
if let Some(metadata) = fs::metadata(path.join("conda-meta")).ok() {
if let Ok(metadata) = fs::metadata(path.join("conda-meta")) {
metadata.is_dir()
} else {
false
Expand Down
5 changes: 5 additions & 0 deletions crates/pet-core/src/os_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ impl EnvironmentApi {
EnvironmentApi {}
}
}
impl Default for EnvironmentApi {
fn default() -> Self {
Self::new()
}
}

#[cfg(windows)]
impl Environment for EnvironmentApi {
Expand Down
Loading