jarvy 0.3.0

Jarvy is a fast, cross-platform CLI that installs and manages developer tools across macOS and Linux.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Semantic version extraction and comparison for tool version checking.
//!
//! This module provides proper semantic versioning support, replacing the
//! previous substring-based matching which caused false positives.

use once_cell::sync::Lazy;
use regex::Regex;
use semver::{Version, VersionReq};

/// Regex pattern to extract semantic versions from tool output.
/// Handles formats like:
/// - "git version 2.44.0"
/// - "v20.10.0"
/// - "Python 3.12.1"
/// - "Docker version 24.0.7, build afdd53b"
/// - "rustc 1.75.0 (82e1608df 2023-12-21)"
/// - "go version go1.21.5 darwin/arm64"
static VERSION_REGEX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"v?(\d+)\.(\d+)(?:\.(\d+))?(?:-([a-zA-Z0-9.-]+))?").unwrap());

/// Represents an extracted version with optional prerelease tag.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtractedVersion {
    pub major: u64,
    pub minor: u64,
    pub patch: u64,
    pub prerelease: Option<String>,
}

impl ExtractedVersion {
    /// Convert to a semver::Version for comparison.
    #[inline]
    pub fn to_semver(&self) -> Option<Version> {
        let version_str = if let Some(ref pre) = self.prerelease {
            format!("{}.{}.{}-{}", self.major, self.minor, self.patch, pre)
        } else {
            format!("{}.{}.{}", self.major, self.minor, self.patch)
        };
        Version::parse(&version_str).ok()
    }
}

impl std::fmt::Display for ExtractedVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(ref pre) = self.prerelease {
            write!(f, "{}.{}.{}-{}", self.major, self.minor, self.patch, pre)
        } else {
            write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
        }
    }
}

/// Extract a version from tool output string.
///
/// # Examples
/// ```
/// use jarvy::tools::version::extract_version;
///
/// let v = extract_version("git version 2.44.0").unwrap();
/// assert_eq!(v.major, 2);
/// assert_eq!(v.minor, 44);
/// assert_eq!(v.patch, 0);
/// ```
pub fn extract_version(output: &str) -> Option<ExtractedVersion> {
    VERSION_REGEX.captures(output).map(|caps| ExtractedVersion {
        major: caps.get(1).unwrap().as_str().parse().unwrap_or(0),
        minor: caps.get(2).unwrap().as_str().parse().unwrap_or(0),
        patch: caps
            .get(3)
            .map(|m| m.as_str().parse().unwrap_or(0))
            .unwrap_or(0),
        prerelease: caps.get(4).map(|m| m.as_str().to_string()),
    })
}

/// Check if an installed version satisfies a version requirement.
///
/// Supports multiple requirement formats:
/// - `"latest"` or `"*"`: Always passes (skip check)
/// - `"3.10"`: Prefix match - accepts 3.10.x
/// - `"3.10.0"`: Exact version match
/// - `">= 3.10"`: Minimum version
/// - `"< 4.0"`: Maximum version
/// - `">= 3.10, < 4.0"`: Range expression
/// - `"~3.10"`: Compatible with 3.10.x
/// - `"^3.10"`: Compatible with 3.x
///
/// # Arguments
/// * `installed_output` - The output from running `cmd --version`
/// * `requirement` - The version requirement string from config
///
/// # Returns
/// `true` if the installed version satisfies the requirement
#[inline]
pub fn version_satisfies(installed_output: &str, requirement: &str) -> bool {
    let requirement = requirement.trim();

    // Handle special "any version" cases
    if requirement.is_empty() || requirement == "latest" || requirement == "*" {
        return true;
    }

    // Try to extract the installed version
    let installed = match extract_version(installed_output) {
        Some(v) => v,
        None => {
            // Can't parse installed version - fall back to substring match
            // but be smarter about it: require word boundary
            return contains_version_prefix(installed_output, requirement);
        }
    };

    // Convert to semver for comparison
    let installed_semver = match installed.to_semver() {
        Some(v) => v,
        None => {
            // Shouldn't happen, but fallback to substring
            return contains_version_prefix(installed_output, requirement);
        }
    };

    // Try to parse requirement as semver requirement
    // First, handle simple version prefixes like "3.10" which should match "3.10.x"
    if let Some(req) = parse_requirement(requirement) {
        return req.matches(&installed_semver);
    }

    // Final fallback: smart substring matching
    contains_version_prefix(installed_output, requirement)
}

/// Parse a version requirement string into a VersionReq.
/// Handles both semver requirements and simple version prefixes.
fn parse_requirement(requirement: &str) -> Option<VersionReq> {
    // Check if it starts with an operator - if so, let semver handle it directly
    let has_operator = requirement.starts_with('>')
        || requirement.starts_with('<')
        || requirement.starts_with('=')
        || requirement.starts_with('^')
        || requirement.starts_with('~');

    if has_operator {
        return VersionReq::parse(requirement).ok();
    }

    // Handle simple version prefixes - DON'T pass bare versions to semver
    // as it interprets "3.10.5" as "^3.10.5" which is not what we want
    let parts: Vec<&str> = requirement.split('.').collect();
    let prefix_req = match parts.len() {
        1 => {
            // Just major version: "20" -> ">=20.0.0, <21.0.0"
            if let Ok(major) = parts[0].parse::<u64>() {
                format!(">={}.0.0, <{}.0.0", major, major + 1)
            } else {
                return None;
            }
        }
        2 => {
            // Major.minor: "3.10" -> ">=3.10.0, <3.11.0"
            if let (Ok(major), Ok(minor)) = (parts[0].parse::<u64>(), parts[1].parse::<u64>()) {
                format!(">={}.{}.0, <{}.{}.0", major, minor, major, minor + 1)
            } else {
                return None;
            }
        }
        3 => {
            // Full version: "3.10.5" -> "=3.10.5" (exact match)
            format!("={}", requirement)
        }
        _ => return None,
    };

    VersionReq::parse(&prefix_req).ok()
}

/// Smart substring matching that avoids false positives.
/// Ensures the requirement matches at a version boundary.
fn contains_version_prefix(output: &str, requirement: &str) -> bool {
    // Find all version-like patterns in the output
    for caps in VERSION_REGEX.captures_iter(output) {
        let full_match = caps.get(0).unwrap().as_str();

        // Check if the matched version starts with the requirement
        // This prevents "2.4" from matching "12.40"
        if full_match.starts_with(requirement)
            || full_match.trim_start_matches('v').starts_with(requirement)
        {
            return true;
        }
    }

    false
}

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

    // ===== Version Extraction Tests =====

    #[test]
    fn test_extract_git_version() {
        let v = extract_version("git version 2.44.0").unwrap();
        assert_eq!(v.major, 2);
        assert_eq!(v.minor, 44);
        assert_eq!(v.patch, 0);
        assert_eq!(v.prerelease, None);
    }

    #[test]
    fn test_extract_node_version() {
        let v = extract_version("v20.10.0").unwrap();
        assert_eq!(v.major, 20);
        assert_eq!(v.minor, 10);
        assert_eq!(v.patch, 0);
    }

    #[test]
    fn test_extract_python_version() {
        let v = extract_version("Python 3.12.1").unwrap();
        assert_eq!(v.major, 3);
        assert_eq!(v.minor, 12);
        assert_eq!(v.patch, 1);
    }

    #[test]
    fn test_extract_docker_version() {
        let v = extract_version("Docker version 24.0.7, build afdd53b").unwrap();
        assert_eq!(v.major, 24);
        assert_eq!(v.minor, 0);
        assert_eq!(v.patch, 7);
    }

    #[test]
    fn test_extract_rustc_version() {
        let v = extract_version("rustc 1.75.0 (82e1608df 2023-12-21)").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 75);
        assert_eq!(v.patch, 0);
    }

    #[test]
    fn test_extract_go_version() {
        // Go has a quirky format: go1.21.5 (no space after 'go')
        let v = extract_version("go version go1.21.5 darwin/arm64").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 21);
        assert_eq!(v.patch, 5);
    }

    #[test]
    fn test_extract_version_with_prerelease() {
        let v = extract_version("v3.10.0-beta.1").unwrap();
        assert_eq!(v.major, 3);
        assert_eq!(v.minor, 10);
        assert_eq!(v.patch, 0);
        assert_eq!(v.prerelease, Some("beta.1".to_string()));
    }

    #[test]
    fn test_extract_version_missing_patch() {
        let v = extract_version("v3.10").unwrap();
        assert_eq!(v.major, 3);
        assert_eq!(v.minor, 10);
        assert_eq!(v.patch, 0);
    }

    // ===== Version Satisfies Tests =====

    #[test]
    fn test_satisfies_latest() {
        assert!(version_satisfies("anything 1.0.0", "latest"));
        assert!(version_satisfies("anything 99.99.99", "latest"));
    }

    #[test]
    fn test_satisfies_wildcard() {
        assert!(version_satisfies("version 1.0.0", "*"));
        assert!(version_satisfies("version 99.0.0", "*"));
    }

    #[test]
    fn test_satisfies_empty_requirement() {
        assert!(version_satisfies("version 1.0.0", ""));
    }

    #[test]
    fn test_satisfies_prefix_major_minor() {
        // "2.44" should match 2.44.x
        assert!(version_satisfies("git version 2.44.0", "2.44"));
        assert!(version_satisfies("git version 2.44.5", "2.44"));
        // But not 2.43 or 2.45
        assert!(!version_satisfies("git version 2.43.0", "2.44"));
        assert!(!version_satisfies("git version 2.45.0", "2.44"));
    }

    #[test]
    fn test_satisfies_prefix_major_only() {
        // "20" should match 20.x.x
        assert!(version_satisfies("v20.0.0", "20"));
        assert!(version_satisfies("v20.10.5", "20"));
        // But not 19 or 21
        assert!(!version_satisfies("v19.0.0", "20"));
        assert!(!version_satisfies("v21.0.0", "20"));
    }

    #[test]
    fn test_satisfies_exact_version() {
        assert!(version_satisfies("v3.10.5", "3.10.5"));
        assert!(!version_satisfies("v3.10.4", "3.10.5"));
        assert!(!version_satisfies("v3.10.6", "3.10.5"));
    }

    #[test]
    fn test_satisfies_minimum_version() {
        assert!(version_satisfies("Python 3.10.0", ">= 3.10"));
        assert!(version_satisfies("Python 3.11.0", ">= 3.10"));
        assert!(version_satisfies("Python 4.0.0", ">= 3.10"));
        assert!(!version_satisfies("Python 3.9.0", ">= 3.10"));
    }

    #[test]
    fn test_satisfies_maximum_version() {
        assert!(version_satisfies("Python 3.9.0", "< 4.0"));
        assert!(version_satisfies("Python 3.99.99", "< 4.0"));
        assert!(!version_satisfies("Python 4.0.0", "< 4.0"));
        assert!(!version_satisfies("Python 4.1.0", "< 4.0"));
    }

    #[test]
    fn test_satisfies_range() {
        // ">= 3.10, < 4.0" should match 3.10 through 3.x
        assert!(version_satisfies("Python 3.10.0", ">= 3.10, < 4.0"));
        assert!(version_satisfies("Python 3.12.5", ">= 3.10, < 4.0"));
        assert!(!version_satisfies("Python 3.9.0", ">= 3.10, < 4.0"));
        assert!(!version_satisfies("Python 4.0.0", ">= 3.10, < 4.0"));
    }

    #[test]
    fn test_satisfies_caret() {
        // "^1.70" should match 1.70.x through 1.x (but not 2.x)
        assert!(version_satisfies("rustc 1.70.0", "^1.70"));
        assert!(version_satisfies("rustc 1.75.0", "^1.70"));
        assert!(!version_satisfies("rustc 1.69.0", "^1.70"));
        assert!(!version_satisfies("rustc 2.0.0", "^1.70"));
    }

    #[test]
    fn test_satisfies_tilde() {
        // "~3.10" should match 3.10.x only
        assert!(version_satisfies("Python 3.10.0", "~3.10"));
        assert!(version_satisfies("Python 3.10.5", "~3.10"));
        assert!(!version_satisfies("Python 3.11.0", "~3.10"));
        assert!(!version_satisfies("Python 3.9.0", "~3.10"));
    }

    // ===== Bug Fix Tests =====

    #[test]
    fn test_no_false_positive_substring() {
        // This was the bug: "2.4" should NOT match "12.40.0"
        assert!(!version_satisfies("version 12.40.0", "2.4"));

        // And "2.4" should NOT match "1.24.0"
        assert!(!version_satisfies("version 1.24.0", "2.4"));
    }

    #[test]
    fn test_no_false_positive_embedded_number() {
        // "3.1" should NOT match "3.10" or "3.11"
        // because 3.1.x is different from 3.10.x
        assert!(!version_satisfies("Python 3.10.0", "3.1"));
        assert!(!version_satisfies("Python 3.11.0", "3.1"));

        // But "3.1" should match "3.1.0" and "3.1.5"
        assert!(version_satisfies("Python 3.1.0", "3.1"));
        assert!(version_satisfies("Python 3.1.5", "3.1"));
    }

    // ===== Edge Cases =====

    #[test]
    fn test_handles_unparseable_output() {
        // If we can't parse the version, fall back to smart substring match
        assert!(version_satisfies("some weird output 2.0", "2.0"));
    }

    #[test]
    fn test_handles_multiple_versions_in_output() {
        // Some tools show multiple versions - we match the first
        let output = "tool 1.0.0 (built with lib 2.5.0)";
        assert!(version_satisfies(output, "1.0"));
        // Should not match the embedded library version incorrectly
    }

    #[test]
    fn test_prerelease_not_matched_by_release() {
        // "3.10" should not match prerelease versions by default
        // (following semver semantics)
        let pre = extract_version("v3.10.0-beta.1").unwrap();
        let release = extract_version("v3.10.0").unwrap();
        assert_ne!(pre, release);
    }
}