Skip to content

Commit

Permalink
test(proto-compiler): test protoc version dependency check
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Apr 16, 2024
1 parent f64fe5e commit 2ac3910
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions proto-compiler/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ pub(crate) fn check_deps() -> Result<(), String> {
// Check if the required
let mut errors = vec![];

if let Err(e) = dep_protoc() {
if let Err(e) = dep_protoc(DEP_PROTOC_VERSION) {
errors.push(format!("protoc: {}", e));
};

Expand All @@ -377,7 +377,7 @@ pub(crate) fn check_deps() -> Result<(), String> {
}

/// Check if protoc is installed and has the required version
fn dep_protoc() -> Result<f32, String> {
fn dep_protoc(expected_version: f32) -> Result<f32, String> {
let protoc = prost_build::protoc_from_env();

// Run `protoc --version` and capture the output
Expand All @@ -400,12 +400,25 @@ fn dep_protoc() -> Result<f32, String> {
.map_err(|e| format!("failed to parse protoc version {}: {}", version_output, e))?;

// Check if the version is equal or higher than 25.1
if version < DEP_PROTOC_VERSION {
if version < expected_version {
Err(format!(
"protoc version must be {} or higher, but found {}; please upgrade",
DEP_PROTOC_VERSION, version
"protoc version must be {} or higher, but found {}; please upgrade: https://github.com/protocolbuffers/protobuf/releases/",
expected_version, version
))
} else {
Ok(version)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_protoc_dep() {
let expected_versions = vec![(10.1, true), (DEP_PROTOC_VERSION, true), (90.5, false)];
for expect in expected_versions {
assert_eq!(dep_protoc(expect.0).is_ok(), expect.1);
}
}
}

0 comments on commit 2ac3910

Please sign in to comment.