From 06b4f3dc8144ebc20963dc1084519c86382d3078 Mon Sep 17 00:00:00 2001 From: Chris Olszewski Date: Wed, 12 Jun 2024 15:10:07 -0700 Subject: [PATCH] fix(shim): avoid panic if user has malformed lockfile (#8461) ### 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 --- .../src/shim/local_turbo_state.rs | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/crates/turborepo-lib/src/shim/local_turbo_state.rs b/crates/turborepo-lib/src/shim/local_turbo_state.rs index 9f972c51a4975..945b8e879ad54 100644 --- a/crates/turborepo-lib/src/shim/local_turbo_state.rs +++ b/crates/turborepo-lib/src/shim/local_turbo_state.rs @@ -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); } }