Skip to content

Commit

Permalink
fix(shim): avoid panic if user has malformed lockfile (#8461)
Browse files Browse the repository at this point in the history
### Description

Since this function is now possibly getting user input, we shouldn't
hard crash if whatever they have listed in their lockfile doesn't pass
the `semver` crate's version requirement.

This shouldn't happen as all lockfiles are generated with fully resolved
versions, but this prevents a panic if that expectation changes.

### Testing Instructions

Added unit tests that would trigger a panic previously
  • Loading branch information
chris-olszewski authored Jun 12, 2024
1 parent a8697c8 commit 06b4f3d
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions crates/turborepo-lib/src/shim/local_turbo_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,37 +185,39 @@ impl Default for YarnRc {
}

pub fn turbo_version_has_shim(version: &str) -> bool {
let version = Version::parse(version).unwrap();
// only need to check major and minor (this will include canaries)
if version.major == 1 {
return version.minor >= 7;
if let Ok(version) = Version::parse(version) {
// only need to check major and minor (this will include canaries)
if version.major == 1 {
return version.minor >= 7;
}
version.major > 1
} else {
// In the case that we don't get passed a valid semver we should avoid a panic.
// We shouldn't hit this we introduce back infering package version from schema
// or package.json.
true
}

version.major > 1
}

#[cfg(test)]
mod test {
use test_case::test_case;

use super::*;

#[test]
fn test_skip_infer_version_constraint() {
let canary = "1.7.0-canary.0";
let newer_canary = "1.7.0-canary.1";
let newer_minor_canary = "1.7.1-canary.6";
let release = "1.7.0";
let old = "1.6.3";
let old_canary = "1.6.2-canary.1";
let new = "1.8.0";
let new_major = "2.1.0";

assert!(turbo_version_has_shim(release));
assert!(turbo_version_has_shim(canary));
assert!(turbo_version_has_shim(newer_canary));
assert!(turbo_version_has_shim(newer_minor_canary));
assert!(turbo_version_has_shim(new));
assert!(turbo_version_has_shim(new_major));
assert!(!turbo_version_has_shim(old));
assert!(!turbo_version_has_shim(old_canary));
#[test_case("1.7.0-canary.0", true; "canary")]
#[test_case("1.7.0-canary.1", true; "newer_canary")]
#[test_case("1.7.1-canary.6", true; "newer_minor_canary")]
#[test_case("1.7.0", true; "release")]
#[test_case("1.6.3", false; "old")]
#[test_case("1.6.2-canary.1", false; "old_canary")]
#[test_case("1.8.0", true; "new")]
#[test_case("2.1.0", true; "new major")]
#[test_case("*", true; "star")]
#[test_case("2.0", true; "version 2 0")]
#[test_case("latest", true; "latest")]
#[test_case("canary", true; "canary tag")]
fn test_skip_infer_version_constraint(version: &str, expected: bool) {
assert_eq!(turbo_version_has_shim(version), expected);
}
}

0 comments on commit 06b4f3d

Please sign in to comment.