11use crate :: run:: runner:: helpers:: apt;
22use 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 ;
48use std:: { env, path:: Path , process:: Command } ;
59use 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+
2854fn 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
62115pub 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