Skip to content

Commit 36cb3ba

Browse files
committed
feat: allow wider range of valgrind codspeed version usage
1 parent d5fae3d commit 36cb3ba

4 files changed

Lines changed: 120 additions & 26 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ lazy_static = "1.4.0"
1818
log = "0.4.20"
1919
rand = "0.8.5"
2020
regex = "1.10.2"
21+
semver = "1.0"
2122
reqwest = { version = "0.11.22", features = [
2223
"json",
2324
"stream",

src/main.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ use console::style;
1313
use lazy_static::lazy_static;
1414
use local_logger::clean_logger;
1515
use prelude::*;
16+
use semver::Version;
1617

1718
use log::log_enabled;
1819

1920
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
2021
pub const MONGODB_TRACER_VERSION: &str = "cs-mongo-tracer-v0.2.0";
2122

22-
const VALGRIND_CODSPEED_BASE_VERSION: &str = "3.24.0";
23+
pub const VALGRIND_CODSPEED_VERSION: Version = Version::new(3, 24, 0);
24+
pub const VALGRIND_CODSPEED_DEB_REVISION_SUFFIX: &str = "0codspeed1";
2325
lazy_static! {
24-
pub static ref VALGRIND_CODSPEED_VERSION: String =
25-
format!("{VALGRIND_CODSPEED_BASE_VERSION}.codspeed");
26+
pub static ref VALGRIND_CODSPEED_VERSION_STRING: String =
27+
format!("{VALGRIND_CODSPEED_VERSION}.codspeed");
2628
pub static ref VALGRIND_CODSPEED_DEB_VERSION: String =
27-
format!("{VALGRIND_CODSPEED_BASE_VERSION}-0codspeed1");
29+
format!("{VALGRIND_CODSPEED_VERSION}-{VALGRIND_CODSPEED_DEB_REVISION_SUFFIX}");
2830
}
2931

3032
#[tokio::main(flavor = "current_thread")]

src/run/runner/valgrind/setup.rs

Lines changed: 112 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::run::runner::helpers::apt;
22
use crate::{VALGRIND_CODSPEED_DEB_VERSION, run::check_system::SystemInfo};
3-
use crate::{VALGRIND_CODSPEED_VERSION, prelude::*, run::helpers::download_file};
3+
use crate::{
4+
VALGRIND_CODSPEED_VERSION, VALGRIND_CODSPEED_VERSION_STRING, prelude::*,
5+
run::helpers::download_file,
6+
};
7+
use semver::Version;
48
use std::{env, path::Path, process::Command};
59
use url::Url;
610

@@ -25,6 +29,28 @@ fn get_codspeed_valgrind_filename(system_info: &SystemInfo) -> Result<String> {
2529
))
2630
}
2731

32+
/// Parse a valgrind version string and extract the semantic version.
33+
/// Expected format: "valgrind-3.25.1.codspeed" or "3.25.1.codspeed"
34+
/// Returns Some(Version) if parsing succeeds, None otherwise.
35+
fn parse_valgrind_codspeed_version(version_str: &str) -> Option<Version> {
36+
let version_str = version_str.trim();
37+
38+
// Extract the version numbers before .codspeed
39+
let version_part = if let Some(codspeed_idx) = version_str.find(".codspeed") {
40+
&version_str[..codspeed_idx]
41+
} else {
42+
return None;
43+
};
44+
45+
// Remove "valgrind-" prefix if present
46+
let version_part = version_part
47+
.strip_prefix("valgrind-")
48+
.unwrap_or(version_part);
49+
50+
// Parse using semver
51+
Version::parse(version_part).ok()
52+
}
53+
2854
fn is_valgrind_installed() -> bool {
2955
let is_valgrind_installed = Command::new("which")
3056
.arg("valgrind")
@@ -35,28 +61,55 @@ fn is_valgrind_installed() -> bool {
3561
return false;
3662
}
3763

38-
if let Ok(version_output) = Command::new("valgrind").arg("--version").output() {
39-
if !version_output.status.success() {
40-
debug!(
41-
"Failed to get valgrind version. stderr: {}",
42-
String::from_utf8_lossy(&version_output.stderr)
43-
);
44-
return false;
45-
}
46-
47-
let version = String::from_utf8_lossy(&version_output.stdout);
48-
let result = version.contains(VALGRIND_CODSPEED_VERSION.as_str());
49-
if !result {
50-
warn!(
51-
"Valgrind is installed but the version is not the expected one. expecting {} but found installed: {}",
52-
VALGRIND_CODSPEED_VERSION.as_str(),
53-
version
54-
);
55-
}
56-
result
57-
} else {
58-
false
64+
let Ok(version_output) = Command::new("valgrind").arg("--version").output() else {
65+
return false;
66+
};
67+
68+
if !version_output.status.success() {
69+
debug!(
70+
"Failed to get valgrind version. stderr: {}",
71+
String::from_utf8_lossy(&version_output.stderr)
72+
);
73+
return false;
74+
}
75+
76+
let version = String::from_utf8_lossy(&version_output.stdout);
77+
78+
// Check if it's a codspeed version
79+
if !version.contains(".codspeed") {
80+
warn!(
81+
"Valgrind is installed but is not a CodSpeed version. expecting {} but found installed: {}",
82+
VALGRIND_CODSPEED_VERSION_STRING.as_str(),
83+
version.trim()
84+
);
85+
return false;
86+
}
87+
88+
// Parse the installed version
89+
let Some(installed_version) = parse_valgrind_codspeed_version(&version) else {
90+
warn!(
91+
"Could not parse valgrind version. expecting {} but found installed: {}",
92+
VALGRIND_CODSPEED_VERSION_STRING.as_str(),
93+
version.trim()
94+
);
95+
return false;
96+
};
97+
if installed_version < VALGRIND_CODSPEED_VERSION {
98+
warn!(
99+
"Valgrind is installed but the version is too old. expecting {} or higher but found installed: {}",
100+
VALGRIND_CODSPEED_VERSION_STRING.as_str(),
101+
version.trim()
102+
);
103+
return false;
59104
}
105+
if installed_version > VALGRIND_CODSPEED_VERSION {
106+
warn!(
107+
"Using experimental valgrind version {}.codspeed. The recommended version is {}",
108+
installed_version,
109+
VALGRIND_CODSPEED_VERSION_STRING.as_str()
110+
);
111+
}
112+
true
60113
}
61114

62115
pub async fn install_valgrind(
@@ -148,4 +201,41 @@ mod tests {
148201
@"valgrind_3.24.0-0codspeed1_ubuntu-22.04_arm64.deb"
149202
);
150203
}
204+
205+
#[test]
206+
fn test_parse_valgrind_codspeed_version_with_prefix() {
207+
let version = parse_valgrind_codspeed_version("valgrind-3.25.1.codspeed").unwrap();
208+
assert_eq!(version, Version::new(3, 25, 1));
209+
}
210+
211+
#[test]
212+
fn test_parse_valgrind_codspeed_version_without_prefix() {
213+
let version = parse_valgrind_codspeed_version("3.25.1.codspeed").unwrap();
214+
assert_eq!(version, Version::new(3, 25, 1));
215+
}
216+
217+
#[test]
218+
fn test_parse_valgrind_codspeed_version_higher_patch() {
219+
let version = parse_valgrind_codspeed_version("valgrind-3.25.2.codspeed").unwrap();
220+
assert_eq!(version, Version::new(3, 25, 2));
221+
}
222+
223+
#[test]
224+
fn test_parse_valgrind_codspeed_version_with_newline() {
225+
let version = parse_valgrind_codspeed_version("valgrind-3.25.1.codspeed\n").unwrap();
226+
assert_eq!(version, Version::new(3, 25, 1));
227+
}
228+
229+
#[test]
230+
fn test_parse_valgrind_codspeed_version_without_codspeed_suffix() {
231+
assert_eq!(parse_valgrind_codspeed_version("valgrind-3.25.1"), None);
232+
}
233+
234+
#[test]
235+
fn test_parse_valgrind_codspeed_version_invalid_format() {
236+
assert_eq!(
237+
parse_valgrind_codspeed_version("valgrind-3.25.codspeed"),
238+
None
239+
);
240+
}
151241
}

0 commit comments

Comments
 (0)