-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add depchecks to
doctor
, also split checks into production and non …
…production (#931) * impl dep checks, add --production flag to doctor
- Loading branch information
Showing
11 changed files
with
355 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
use std::collections::HashMap; | ||
|
||
use semver::{Version, VersionReq}; | ||
use thiserror::Error; | ||
use toml::Value; | ||
|
||
#[derive(Debug, PartialEq, Eq, Ord, PartialOrd)] | ||
pub enum VersionStatus { | ||
NotFound, | ||
Invalid { | ||
version: String, | ||
min_version: String, | ||
}, | ||
Ok(String), | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Ord, PartialOrd)] | ||
pub struct CrateStatus { | ||
pub crate_name: String, | ||
pub status: VersionStatus, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum VersionCheckError { | ||
#[error("Failed to parse Cargo.lock: {0}")] | ||
ParseError(#[from] toml::de::Error), | ||
|
||
#[error("Error with crate {crate_name}: {msg}")] | ||
CrateError { crate_name: String, msg: String }, | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, VersionCheckError>; | ||
|
||
pub fn check_crate_versions( | ||
cargo_lock_content: &str, | ||
min_versions: HashMap<&str, &str>, | ||
) -> Result<Vec<CrateStatus>> { | ||
let lock_file: Value = cargo_lock_content.parse()?; | ||
|
||
let packages = lock_file | ||
.get("package") | ||
.and_then(|v| v.as_array()) | ||
.ok_or_else(|| { | ||
VersionCheckError::ParseError(serde::de::Error::custom( | ||
"Missing package array in Cargo.lock", | ||
)) | ||
})?; | ||
|
||
let mut results = Vec::new(); | ||
|
||
for (crate_name, min_version) in min_versions { | ||
let min_version_req = | ||
VersionReq::parse(min_version).map_err(|_| VersionCheckError::CrateError { | ||
crate_name: crate_name.to_string(), | ||
msg: format!("Invalid minimum version format: {min_version}"), | ||
})?; | ||
|
||
let mut found = false; | ||
for package in packages { | ||
if let Some(name) = package.get("name").and_then(|v| v.as_str()) { | ||
if name == crate_name { | ||
found = true; | ||
let version_str = | ||
package | ||
.get("version") | ||
.and_then(|v| v.as_str()) | ||
.ok_or_else(|| VersionCheckError::CrateError { | ||
crate_name: crate_name.to_string(), | ||
msg: "Invalid version format in Cargo.lock".to_string(), | ||
})?; | ||
|
||
let version = | ||
Version::parse(version_str).map_err(|_| VersionCheckError::CrateError { | ||
crate_name: crate_name.to_string(), | ||
msg: format!("Invalid version format in Cargo.lock: {version_str}"), | ||
})?; | ||
|
||
let status = if min_version_req.matches(&version) { | ||
VersionStatus::Ok(version.to_string()) | ||
} else { | ||
VersionStatus::Invalid { | ||
version: version.to_string(), | ||
min_version: min_version.to_string(), | ||
} | ||
}; | ||
results.push(CrateStatus { | ||
crate_name: crate_name.to_string(), | ||
status, | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if !found { | ||
results.push(CrateStatus { | ||
crate_name: crate_name.to_string(), | ||
status: VersionStatus::NotFound, | ||
}); | ||
} | ||
} | ||
|
||
Ok(results) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_multiple_crates_mixed_results() { | ||
let cargo_lock_content = r#" | ||
[[package]] | ||
name = "serde" | ||
version = "1.0.130" | ||
[[package]] | ||
name = "tokio" | ||
version = "0.3.0" | ||
[[package]] | ||
name = "rand" | ||
version = "0.8.4" | ||
"#; | ||
|
||
let mut min_versions = HashMap::new(); | ||
min_versions.insert("serde", "1.0.130"); | ||
min_versions.insert("tokio", "1.0"); | ||
min_versions.insert("rand", "0.8.0"); | ||
|
||
let mut result = check_crate_versions(cargo_lock_content, min_versions).unwrap(); | ||
result.sort(); | ||
assert_eq!( | ||
result, | ||
vec![ | ||
CrateStatus { | ||
crate_name: "rand".to_string(), | ||
status: VersionStatus::Ok("0.8.4".to_string()) | ||
}, | ||
CrateStatus { | ||
crate_name: "serde".to_string(), | ||
status: VersionStatus::Ok("1.0.130".to_string()) | ||
}, | ||
CrateStatus { | ||
crate_name: "tokio".to_string(), | ||
status: VersionStatus::Invalid { | ||
version: "0.3.0".to_string(), | ||
min_version: "1.0".to_string() | ||
} | ||
} | ||
] | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_invalid_version_format_in_cargo_lock() { | ||
let cargo_lock_content = r#" | ||
[[package]] | ||
name = "serde" | ||
version = "1.0.x" | ||
"#; | ||
|
||
let mut min_versions = HashMap::new(); | ||
min_versions.insert("serde", "1.0.0"); | ||
|
||
let result = check_crate_versions(cargo_lock_content, min_versions); | ||
assert!(matches!( | ||
result, | ||
Err(VersionCheckError::CrateError { crate_name, msg }) if crate_name == "serde" && msg.contains("Invalid version format") | ||
)); | ||
} | ||
|
||
#[test] | ||
fn test_no_package_section_in_cargo_lock() { | ||
let cargo_lock_content = r" | ||
# No packages listed in this Cargo.lock | ||
"; | ||
|
||
let mut min_versions = HashMap::new(); | ||
min_versions.insert("serde", "1.0.130"); | ||
|
||
let result = check_crate_versions(cargo_lock_content, min_versions); | ||
assert!(matches!(result, Err(VersionCheckError::ParseError(_)))); | ||
} | ||
|
||
#[test] | ||
fn test_exact_version_match_for_minimum_requirement() { | ||
let cargo_lock_content = r#" | ||
[[package]] | ||
name = "serde" | ||
version = "1.0.130" | ||
"#; | ||
|
||
let mut min_versions = HashMap::new(); | ||
min_versions.insert("serde", "1.0.130"); | ||
|
||
let mut result = check_crate_versions(cargo_lock_content, min_versions).unwrap(); | ||
result.sort(); | ||
assert_eq!( | ||
result, | ||
vec![CrateStatus { | ||
crate_name: "serde".to_string(), | ||
status: VersionStatus::Ok("1.0.130".to_string()), | ||
}] | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_no_crates_in_min_versions_map() { | ||
let cargo_lock_content = r#" | ||
[[package]] | ||
name = "serde" | ||
version = "1.0.130" | ||
"#; | ||
|
||
let min_versions = HashMap::new(); // Empty map | ||
|
||
let result = check_crate_versions(cargo_lock_content, min_versions).unwrap(); | ||
assert!(result.is_empty()); | ||
} | ||
} |
Oops, something went wrong.