Skip to content

Commit b075d49

Browse files
authored
Fix linters and tests (#4)
1 parent 898ef6c commit b075d49

File tree

16 files changed

+88
-93
lines changed

16 files changed

+88
-93
lines changed

.github/workflows/pr-check.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ on:
88
- release*
99

1010
jobs:
11-
unit-tests:
12-
name: Unit Tests
11+
tests:
12+
name: Tests
1313
runs-on: ${{ matrix.os }}
1414
strategy:
1515
fail-fast: false
@@ -45,7 +45,7 @@ jobs:
4545
targets: ${{ matrix.target }}
4646

4747
- name: Cargo Fetch
48-
run: cargo fetch
48+
run: cargo fetch
4949

5050
- name: Run Tests
5151
run: cargo test --frozen --all-features

crates/pet-conda/src/conda_rc.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn get_conda_rc_search_paths(environment: &CondaEnvironmentVariables) -> Vec<Pat
2424
"C:\\ProgramData\\conda\\condarc.d",
2525
]
2626
.iter()
27-
.map(|p| PathBuf::from(p))
27+
.map(PathBuf::from)
2828
.collect();
2929

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

6262
#[cfg(unix)]
6363
fn get_conda_rc_search_paths(environment: &CondaEnvironmentVariables) -> Vec<PathBuf> {
64-
let mut search_paths: Vec<PathBuf> = vec![
64+
let mut search_paths: Vec<PathBuf> = [
6565
"/etc/conda/.condarc",
6666
"/etc/conda/condarc",
6767
"/etc/conda/condarc.d",
@@ -70,13 +70,13 @@ fn get_conda_rc_search_paths(environment: &CondaEnvironmentVariables) -> Vec<Pat
7070
"/var/lib/conda/condarc.d",
7171
]
7272
.iter()
73-
.map(|p| PathBuf::from(p))
73+
.map(PathBuf::from)
7474
.map(|p| {
7575
// This only applies in tests.
7676
// We need this, as the root folder cannot be mocked.
7777
if let Some(ref root) = environment.root {
7878
// Strip the first `/` (this path is only for testing purposes)
79-
root.join(p.to_string_lossy()[1..].to_string())
79+
root.join(&p.to_string_lossy()[1..])
8080
} else {
8181
p
8282
}
@@ -147,8 +147,8 @@ fn parse_conda_rc(conda_rc: &PathBuf) -> Option<Condarc> {
147147
continue;
148148
}
149149
if start_consuming_values {
150-
if line.trim().starts_with("-") {
151-
if let Some(env_dir) = line.splitn(2, '-').nth(1) {
150+
if line.trim().starts_with('-') {
151+
if let Some(env_dir) = line.split_once('-').map(|x| x.1) {
152152
// Directories in conda-rc are where `envs` are stored.
153153
env_dirs.push(PathBuf::from(env_dir.trim()).join("envs"));
154154
}
@@ -158,5 +158,5 @@ fn parse_conda_rc(conda_rc: &PathBuf) -> Option<Condarc> {
158158
}
159159
}
160160
}
161-
return Some(Condarc { env_dirs });
161+
Some(Condarc { env_dirs })
162162
}

crates/pet-conda/src/environment_locations.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
pub fn get_conda_environment_paths(environment: &CondaEnvironmentVariables) -> Vec<PathBuf> {
1616
let mut env_paths = get_conda_envs_from_environment_txt(environment)
1717
.iter()
18-
.map(|e| PathBuf::from(e))
18+
.map(PathBuf::from)
1919
.collect::<Vec<PathBuf>>();
2020

2121
let mut env_paths_from_conda_rc = get_conda_environment_paths_from_conda_rc(environment);
@@ -83,7 +83,7 @@ pub fn get_conda_environment_paths_from_known_paths(
8383
if let Ok(entries) = fs::read_dir(full_path) {
8484
for entry in entries.filter_map(Result::ok) {
8585
let path = entry.path();
86-
if let Some(meta) = fs::metadata(&path).ok() {
86+
if let Ok(meta) = fs::metadata(&path) {
8787
if meta.is_dir() {
8888
env_paths.push(path);
8989
}
@@ -92,7 +92,7 @@ pub fn get_conda_environment_paths_from_known_paths(
9292
}
9393
}
9494
}
95-
return env_paths;
95+
env_paths
9696
}
9797

9898
pub fn get_environments(conda_dir: &Path) -> Vec<PathBuf> {
@@ -179,10 +179,10 @@ pub fn get_known_conda_install_locations(environment: &CondaEnvironmentVariables
179179
PathBuf::from("/miniforge3"),
180180
];
181181
if let Some(ref home) = environment.home {
182-
known_paths.push(PathBuf::from(home.clone()).join("anaconda3"));
183-
known_paths.push(PathBuf::from(home.clone()).join("miniconda3"));
184-
known_paths.push(PathBuf::from(home.clone()).join("miniforge3"));
185-
known_paths.push(PathBuf::from(home).join(".conda"));
182+
known_paths.push(home.clone().join("anaconda3"));
183+
known_paths.push(home.clone().join("miniconda3"));
184+
known_paths.push(home.clone().join("miniforge3"));
185+
known_paths.push(home.join(".conda"));
186186
}
187187
known_paths.append(get_known_conda_locations(environment).as_mut());
188188
known_paths.dedup();
@@ -224,8 +224,8 @@ pub fn get_known_conda_locations(environment: &CondaEnvironmentVariables) -> Vec
224224
PathBuf::from("/miniconda3/bin"),
225225
];
226226
if let Some(ref home) = environment.home {
227-
known_paths.push(PathBuf::from(home.clone()).join("anaconda3/bin"));
228-
known_paths.push(PathBuf::from(home).join("miniconda3/bin"));
227+
known_paths.push(home.clone().join("anaconda3/bin"));
228+
known_paths.push(home.join("miniconda3/bin"));
229229
}
230230
known_paths.append(&mut environment.known_global_search_locations.clone());
231231
known_paths

crates/pet-conda/src/environments.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct CondaEnvironment {
2525

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

3131
pub fn to_python_environment(
@@ -38,15 +38,15 @@ impl CondaEnvironment {
3838
if is_conda_install(&self.prefix) {
3939
name = Some("base".to_string());
4040
} else {
41-
name = match self.prefix.file_name() {
42-
Some(name) => Some(name.to_str().unwrap_or_default().to_string()),
43-
None => None,
44-
};
41+
name = self
42+
.prefix
43+
.file_name()
44+
.map(|name| name.to_str().unwrap_or_default().to_string());
4545
}
4646
// if the conda install folder is parent of the env folder, then we can use named activation.
4747
// E.g. conda env is = <conda install>/envs/<env name>
4848
// Then we can use `<conda install>/bin/conda activate -n <env name>`
49-
if !self.prefix.starts_with(&conda_dir) {
49+
if !self.prefix.starts_with(conda_dir) {
5050
name = None;
5151
}
5252
// This is a root env.
@@ -62,7 +62,7 @@ impl CondaEnvironment {
6262
}
6363
}
6464
fn get_conda_environment_info(
65-
env_path: &PathBuf,
65+
env_path: &Path,
6666
manager: &Option<CondaManager>,
6767
) -> Option<CondaEnvironment> {
6868
if !is_conda_env(env_path) {
@@ -74,35 +74,34 @@ fn get_conda_environment_info(
7474
Some(manager) => Some(manager.conda_dir.clone()),
7575
None => get_conda_installation_used_to_create_conda_env(env_path),
7676
};
77-
let env_path = env_path.clone();
78-
if let Some(python_binary) = find_executable(&env_path) {
79-
if let Some(package_info) = CondaPackageInfo::from(&env_path, &Package::Python) {
80-
return Some(CondaEnvironment {
81-
prefix: env_path,
77+
if let Some(python_binary) = find_executable(env_path) {
78+
if let Some(package_info) = CondaPackageInfo::from(env_path, &Package::Python) {
79+
Some(CondaEnvironment {
80+
prefix: env_path.into(),
8281
executable: Some(python_binary),
8382
version: Some(package_info.version),
8483
conda_dir: conda_install_folder,
8584
arch: package_info.arch,
86-
});
85+
})
8786
} else {
8887
// No python in this environment.
89-
return Some(CondaEnvironment {
90-
prefix: env_path,
88+
Some(CondaEnvironment {
89+
prefix: env_path.into(),
9190
executable: Some(python_binary),
9291
version: None,
9392
conda_dir: conda_install_folder,
9493
arch: None,
95-
});
94+
})
9695
}
9796
} else {
9897
// No python in this environment.
99-
return Some(CondaEnvironment {
100-
prefix: env_path,
98+
Some(CondaEnvironment {
99+
prefix: env_path.into(),
101100
executable: None,
102101
version: None,
103102
conda_dir: conda_install_folder,
104103
arch: None,
105-
});
104+
})
106105
}
107106
}
108107

@@ -114,7 +113,7 @@ fn get_conda_environment_info(
114113
* Sometimes the cmd line contains the fully qualified path to the conda install folder.
115114
* This function returns the path to the conda installation that was used to create the environment.
116115
*/
117-
pub fn get_conda_installation_used_to_create_conda_env(env_path: &PathBuf) -> Option<PathBuf> {
116+
pub fn get_conda_installation_used_to_create_conda_env(env_path: &Path) -> Option<PathBuf> {
118117
// Possible the env_path is the root conda install folder.
119118
if is_conda_install(env_path) {
120119
return Some(env_path.to_path_buf());
@@ -162,9 +161,6 @@ pub fn get_activation_command(
162161
manager: &EnvManager,
163162
name: Option<String>,
164163
) -> Option<Vec<String>> {
165-
if env.executable.is_none() {
166-
return None;
167-
}
168164
let conda_exe = manager.executable.to_str().unwrap_or_default().to_string();
169165
if let Some(name) = name {
170166
Some(vec![

crates/pet-conda/src/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn find_conda_binary_in_known_locations(
6868
for location in known_locations {
6969
for bin in &conda_bin_names {
7070
let conda_path = location.join(bin);
71-
if let Some(metadata) = std::fs::metadata(&conda_path).ok() {
71+
if let Ok(metadata) = std::fs::metadata(&conda_path) {
7272
if metadata.is_file() || metadata.is_symlink() {
7373
return Some(conda_path);
7474
}

crates/pet-conda/src/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ fn get_conda_manager_from_env(env_path: &Path) -> Option<CondaManager> {
5252

5353
// We've been given an env thats been created using the -p flag.
5454
// Get the conda install folder from the history file.
55-
if let Some(conda_install_folder) =
56-
get_conda_installation_used_to_create_conda_env(&env_path.to_path_buf())
57-
{
55+
if let Some(conda_install_folder) = get_conda_installation_used_to_create_conda_env(env_path) {
5856
return get_conda_manager(&conda_install_folder);
5957
}
6058
None
@@ -159,7 +157,10 @@ impl Locator for Conda<'_> {
159157
return Some(env);
160158
}
161159
} else {
162-
error!("Unable to find conda Install folder conda install folder env: {:?}", env);
160+
error!(
161+
"Unable to find conda Install folder conda install folder env: {:?}",
162+
env
163+
);
163164
}
164165
}
165166
}

crates/pet-conda/src/package.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
7979

8080
let history = path.join("history");
8181
let package_entry = format!(":{}-", name.to_name());
82-
if let Some(history_contents) = fs::read_to_string(&history).ok() {
82+
if let Ok(history_contents) = fs::read_to_string(history) {
8383
for line in history_contents
8484
.lines()
8585
.filter(|l| l.contains(&package_entry))
@@ -88,8 +88,8 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
8888
// +conda-forge/osx-arm64::psutil-5.9.8-py312he37b823_0
8989
// +conda-forge/osx-arm64::python-3.12.2-hdf0ec26_0_cpython
9090
// +conda-forge/osx-arm64::python_abi-3.12-4_cp312
91-
let regex = get_package_version_history_regex(&name);
92-
if let Some(captures) = regex.captures(&line) {
91+
let regex = get_package_version_history_regex(name);
92+
if let Some(captures) = regex.captures(line) {
9393
if let Some(version) = captures.get(1) {
9494
if let Some(hash) = captures.get(2) {
9595
let package_path = format!(
@@ -109,9 +109,9 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
109109
// }
110110
// 32bit channel is https://repo.anaconda.com/pkgs/main/win-32/
111111
// 64bit channel is "channel": "https://repo.anaconda.com/pkgs/main/osx-arm64",
112-
if let Some(contents) = read_to_string(&package_path).ok() {
113-
if let Some(js) =
114-
serde_json::from_str::<CondaMetaPackageStructure>(&contents).ok()
112+
if let Ok(contents) = read_to_string(&package_path) {
113+
if let Ok(js) =
114+
serde_json::from_str::<CondaMetaPackageStructure>(&contents)
115115
{
116116
if let Some(channel) = js.channel {
117117
if channel.ends_with("64") {
@@ -147,7 +147,7 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
147147
);
148148

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

152152
// Fallback, slower approach of enumerating all files.
153153
if let Ok(entries) = fs::read_dir(path) {
@@ -167,9 +167,9 @@ fn get_conda_package_info(path: &Path, name: &Package) -> Option<CondaPackageInf
167167
// }
168168
// 32bit channel is https://repo.anaconda.com/pkgs/main/win-32/
169169
// 64bit channel is "channel": "https://repo.anaconda.com/pkgs/main/osx-arm64",
170-
if let Some(contents) = read_to_string(&path).ok() {
171-
if let Some(js) =
172-
serde_json::from_str::<CondaMetaPackageStructure>(&contents).ok()
170+
if let Ok(contents) = read_to_string(&path) {
171+
if let Ok(js) =
172+
serde_json::from_str::<CondaMetaPackageStructure>(&contents)
173173
{
174174
if let Some(channel) = js.channel {
175175
if channel.ends_with("64") {

crates/pet-conda/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn is_conda_install(path: &Path) -> bool {
1414
// conda-meta must exist as this contains a mandatory `history` file.
1515
// The root conda installation folder is also a conda environment (its the base environment).
1616
pub fn is_conda_env(path: &Path) -> bool {
17-
if let Some(metadata) = fs::metadata(path.join("conda-meta")).ok() {
17+
if let Ok(metadata) = fs::metadata(path.join("conda-meta")) {
1818
metadata.is_dir()
1919
} else {
2020
false

crates/pet-conda/tests/unix/anaconda3-2023.03-without-history/envs/.conda_envs_dir_test

Whitespace-only changes.

crates/pet-core/src/os_environment.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ impl EnvironmentApi {
2020
EnvironmentApi {}
2121
}
2222
}
23+
impl Default for EnvironmentApi {
24+
fn default() -> Self {
25+
Self::new()
26+
}
27+
}
2328

2429
#[cfg(windows)]
2530
impl Environment for EnvironmentApi {

0 commit comments

Comments
 (0)