From a37fca6accdb8635b433206703ddad75af457d3a Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Thu, 7 Sep 2023 14:32:50 -0700 Subject: [PATCH] Rename enums. --- crates/cli/src/commands/alias.rs | 10 +-- crates/cli/src/commands/bin.rs | 6 +- crates/cli/src/commands/clean.rs | 6 +- crates/cli/src/commands/global.rs | 10 +-- crates/cli/src/commands/install.rs | 6 +- crates/cli/src/commands/install_all.rs | 10 ++- crates/cli/src/commands/list_remote.rs | 6 +- crates/cli/src/commands/local.rs | 10 +-- crates/cli/src/commands/run.rs | 8 +-- crates/cli/src/commands/uninstall.rs | 4 +- crates/cli/tests/alias_test.rs | 19 +++-- crates/cli/tests/global_test.rs | 26 ++++++- crates/cli/tests/install_uninstall_test.rs | 20 +++--- crates/cli/tests/list_test.rs | 10 +-- crates/cli/tests/local_test.rs | 22 ++++-- crates/cli/tests/plugins_test.rs | 5 +- crates/cli/tests/run_test.rs | 4 +- crates/cli/tests/unalias_test.rs | 21 +++--- crates/core/src/events.rs | 12 ++-- crates/core/src/tool.rs | 45 +++++++----- crates/core/src/tool_manifest.rs | 18 ++--- crates/core/src/tools_config.rs | 4 +- crates/core/src/version.rs | 76 ++++++++++---------- crates/core/src/version_detector.rs | 8 +-- crates/core/src/version_resolver.rs | 30 ++++---- crates/core/tests/tools_config_test.rs | 41 +++++++---- crates/core/tests/version_resolver_test.rs | 82 ++++++++++++++-------- crates/core/tests/version_test.rs | 58 +++++++-------- crates/pdk-test-utils/src/lib.rs | 4 +- 29 files changed, 339 insertions(+), 242 deletions(-) diff --git a/crates/cli/src/commands/alias.rs b/crates/cli/src/commands/alias.rs index 2458217da..dc87a7087 100644 --- a/crates/cli/src/commands/alias.rs +++ b/crates/cli/src/commands/alias.rs @@ -1,5 +1,5 @@ use clap::Args; -use proto_core::{is_alias_name, load_tool, Id, ProtoError, VersionType}; +use proto_core::{is_alias_name, load_tool, Id, ProtoError, UnresolvedVersionSpec}; use starbase::system; use starbase_styles::color; use tracing::info; @@ -13,12 +13,12 @@ pub struct AliasArgs { alias: String, #[arg(required = true, help = "Version or alias to associate with")] - semver: VersionType, + spec: UnresolvedVersionSpec, } #[system] pub async fn alias(args: ArgsRef) { - if let VersionType::Alias(inner_alias) = &args.semver { + if let UnresolvedVersionSpec::Alias(inner_alias) = &args.spec { if &args.alias == inner_alias { return Err(ProtoError::Message("Cannot map an alias to itself.".into()))?; } @@ -34,13 +34,13 @@ pub async fn alias(args: ArgsRef) { tool.manifest .aliases - .insert(args.alias.clone(), args.semver.clone()); + .insert(args.alias.clone(), args.spec.clone()); tool.manifest.save()?; info!( "Added alias {} ({}) for {}", color::id(&args.alias), - color::muted_light(args.semver.to_string()), + color::muted_light(args.spec.to_string()), tool.get_name(), ); } diff --git a/crates/cli/src/commands/bin.rs b/crates/cli/src/commands/bin.rs index 70ae64a39..f3f3028a4 100644 --- a/crates/cli/src/commands/bin.rs +++ b/crates/cli/src/commands/bin.rs @@ -1,5 +1,5 @@ use clap::Args; -use proto_core::{detect_version, load_tool, Id, VersionType}; +use proto_core::{detect_version, load_tool, Id, UnresolvedVersionSpec}; use starbase::system; #[derive(Args, Clone, Debug)] @@ -8,7 +8,7 @@ pub struct BinArgs { id: Id, #[arg(help = "Version or alias of tool")] - semver: Option, + spec: Option, #[arg(long, help = "Display shim path when available")] shim: bool, @@ -17,7 +17,7 @@ pub struct BinArgs { #[system] pub async fn bin(args: ArgsRef) { let mut tool = load_tool(&args.id).await?; - let version = detect_version(&tool, args.semver.clone()).await?; + let version = detect_version(&tool, args.spec.clone()).await?; tool.resolve_version(&version).await?; tool.locate_bins().await?; diff --git a/crates/cli/src/commands/clean.rs b/crates/cli/src/commands/clean.rs index 5d9f77e6f..ed538a85e 100644 --- a/crates/cli/src/commands/clean.rs +++ b/crates/cli/src/commands/clean.rs @@ -1,7 +1,7 @@ use clap::Args; use dialoguer::Confirm; use proto_core::{ - get_plugins_dir, get_shim_file_name, load_tool, AliasOrVersion, Id, Tool, ToolsConfig, + get_plugins_dir, get_shim_file_name, load_tool, Id, Tool, ToolsConfig, VersionSpec, }; use proto_pdk_api::{CreateShimsInput, CreateShimsOutput}; use starbase::diagnostics::IntoDiagnostic; @@ -52,7 +52,7 @@ pub async fn clean_tool(mut tool: Tool, now: u128, days: u8, yes: bool) -> miett return Ok(0); } - let mut versions_to_clean = HashSet::::new(); + let mut versions_to_clean = HashSet::::new(); debug!("Scanning file system for stale and untracked versions"); @@ -70,7 +70,7 @@ pub async fn clean_tool(mut tool: Tool, now: u128, days: u8, yes: bool) -> miett continue; } - let version = AliasOrVersion::parse(&dir_name)?; + let version = VersionSpec::parse(&dir_name)?; if !tool.manifest.versions.contains_key(&version) { debug!( diff --git a/crates/cli/src/commands/global.rs b/crates/cli/src/commands/global.rs index 93bf2e4f1..758a65db9 100644 --- a/crates/cli/src/commands/global.rs +++ b/crates/cli/src/commands/global.rs @@ -1,5 +1,5 @@ use clap::Args; -use proto_core::{load_tool, Id, VersionType}; +use proto_core::{load_tool, Id, UnresolvedVersionSpec}; use starbase::system; use starbase_styles::color; use tracing::{debug, info}; @@ -10,18 +10,18 @@ pub struct GlobalArgs { id: Id, #[arg(required = true, help = "Version or alias of tool")] - semver: VersionType, + spec: UnresolvedVersionSpec, } #[system] pub async fn global(args: ArgsRef) -> SystemResult { let mut tool = load_tool(&args.id).await?; - tool.manifest.default_version = Some(args.semver.clone()); + tool.manifest.default_version = Some(args.spec.clone()); tool.manifest.save()?; debug!( - version = args.semver.to_string(), + version = args.spec.to_string(), manifest = ?tool.manifest.path, "Wrote the global version", ); @@ -29,6 +29,6 @@ pub async fn global(args: ArgsRef) -> SystemResult { info!( "Set the global {} version to {}", tool.get_name(), - color::hash(args.semver.to_string()) + color::hash(args.spec.to_string()) ); } diff --git a/crates/cli/src/commands/install.rs b/crates/cli/src/commands/install.rs index c23d54e1a..d843294c1 100644 --- a/crates/cli/src/commands/install.rs +++ b/crates/cli/src/commands/install.rs @@ -2,7 +2,7 @@ use crate::helpers::{create_progress_bar, disable_progress_bars}; use crate::shell; use clap::Args; use miette::IntoDiagnostic; -use proto_core::{load_tool, Id, Tool, VersionType}; +use proto_core::{load_tool, Id, Tool, UnresolvedVersionSpec}; use proto_pdk_api::{InstallHook, SyncShellProfileInput, SyncShellProfileOutput}; use starbase::{system, SystemResult}; use starbase_styles::color; @@ -15,7 +15,7 @@ pub struct InstallArgs { pub id: Id, #[arg(default_value = "latest", help = "Version or alias of tool")] - pub semver: Option, + pub spec: Option, #[arg(long, help = "Pin version as the global default")] pub pin: bool, @@ -26,7 +26,7 @@ pub struct InstallArgs { } pub async fn internal_install(args: InstallArgs) -> SystemResult { - let version = args.semver.clone().unwrap_or_default(); + let version = args.spec.clone().unwrap_or_default(); let mut tool = load_tool(&args.id).await?; if tool.is_setup(&version).await? { diff --git a/crates/cli/src/commands/install_all.rs b/crates/cli/src/commands/install_all.rs index 420909943..e908e957d 100644 --- a/crates/cli/src/commands/install_all.rs +++ b/crates/cli/src/commands/install_all.rs @@ -5,7 +5,9 @@ use crate::{ helpers::create_progress_bar, }; use futures::future::try_join_all; -use proto_core::{load_tool_from_locator, ProtoEnvironment, ToolsConfig, UserConfig, VersionType}; +use proto_core::{ + load_tool_from_locator, ProtoEnvironment, ToolsConfig, UnresolvedVersionSpec, UserConfig, +}; use starbase::system; use starbase_styles::color; use std::env; @@ -38,7 +40,9 @@ pub async fn install_all() { debug!("Detected version {} for {}", version, tool.get_name()); - config.tools.insert(name, VersionType::Version(version)); + config + .tools + .insert(name, UnresolvedVersionSpec::Version(version)); } } @@ -63,9 +67,9 @@ pub async fn install_all() { for (id, version) in config.tools { futures.push(internal_install(InstallArgs { id, - semver: Some(version), pin: false, passthrough: vec![], + spec: Some(version), })); } diff --git a/crates/cli/src/commands/list_remote.rs b/crates/cli/src/commands/list_remote.rs index e27e1754d..be00c5e67 100644 --- a/crates/cli/src/commands/list_remote.rs +++ b/crates/cli/src/commands/list_remote.rs @@ -1,5 +1,5 @@ use clap::Args; -use proto_core::{load_tool, Id, VersionType}; +use proto_core::{load_tool, Id, UnresolvedVersionSpec}; use starbase::system; use std::process; use tracing::debug; @@ -16,7 +16,9 @@ pub async fn list_remote(args: ArgsRef) { debug!("Loading versions"); - let resolver = tool.load_version_resolver(&VersionType::default()).await?; + let resolver = tool + .load_version_resolver(&UnresolvedVersionSpec::default()) + .await?; let mut versions = resolver.versions; if versions.is_empty() { diff --git a/crates/cli/src/commands/local.rs b/crates/cli/src/commands/local.rs index 958e429d9..74051c2ae 100644 --- a/crates/cli/src/commands/local.rs +++ b/crates/cli/src/commands/local.rs @@ -1,5 +1,5 @@ use clap::Args; -use proto_core::{load_tool, Id, ToolsConfig, VersionType}; +use proto_core::{load_tool, Id, ToolsConfig, UnresolvedVersionSpec}; use starbase::system; use starbase_styles::color; use std::{env, path::PathBuf}; @@ -11,7 +11,7 @@ pub struct LocalArgs { id: Id, #[arg(required = true, help = "Version or alias of tool")] - semver: VersionType, + spec: UnresolvedVersionSpec, } #[system] @@ -20,11 +20,11 @@ pub async fn local(args: ArgsRef) { let local_path = env::current_dir().unwrap_or_else(|_| PathBuf::from(".")); let mut config = ToolsConfig::load_from(local_path)?; - config.tools.insert(args.id.clone(), args.semver.clone()); + config.tools.insert(args.id.clone(), args.spec.clone()); config.save()?; debug!( - version = args.semver.to_string(), + version = args.spec.to_string(), config = ?config.path, "Wrote the local version", ); @@ -32,6 +32,6 @@ pub async fn local(args: ArgsRef) { info!( "Set the local {} version to {}", tool.get_name(), - color::hash(args.semver.to_string()) + color::hash(args.spec.to_string()) ); } diff --git a/crates/cli/src/commands/run.rs b/crates/cli/src/commands/run.rs index a71aa16c5..d1ab7470e 100644 --- a/crates/cli/src/commands/run.rs +++ b/crates/cli/src/commands/run.rs @@ -1,7 +1,7 @@ use crate::commands::install::{internal_install, InstallArgs}; use clap::Args; use miette::IntoDiagnostic; -use proto_core::{detect_version, load_tool, Id, ProtoError, UserConfig, VersionType}; +use proto_core::{detect_version, load_tool, Id, ProtoError, UnresolvedVersionSpec, UserConfig}; use proto_pdk_api::RunHook; use starbase::system; use starbase_styles::color; @@ -16,7 +16,7 @@ pub struct RunArgs { id: Id, #[arg(help = "Version or alias of tool")] - semver: Option, + spec: Option, #[arg(long, help = "Path to an alternate binary to run")] bin: Option, @@ -32,7 +32,7 @@ pub struct RunArgs { #[system] pub async fn run(args: ArgsRef) -> SystemResult { let mut tool = load_tool(&args.id).await?; - let version = detect_version(&tool, args.semver.clone()).await?; + let version = detect_version(&tool, args.spec.clone()).await?; let user_config = UserConfig::load()?; // Check if installed or install @@ -51,7 +51,7 @@ pub async fn run(args: ArgsRef) -> SystemResult { internal_install(InstallArgs { id: args.id.clone(), - semver: Some(tool.get_resolved_version().to_implicit_type()), + spec: Some(tool.get_resolved_version().to_implicit_type()), pin: false, passthrough: vec![], }) diff --git a/crates/cli/src/commands/uninstall.rs b/crates/cli/src/commands/uninstall.rs index 354a903cc..8a95ee4ec 100644 --- a/crates/cli/src/commands/uninstall.rs +++ b/crates/cli/src/commands/uninstall.rs @@ -1,6 +1,6 @@ use crate::helpers::{create_progress_bar, disable_progress_bars}; use clap::Args; -use proto_core::{load_tool, Id, VersionType}; +use proto_core::{load_tool, Id, UnresolvedVersionSpec}; use starbase::system; use tracing::{debug, info}; @@ -10,7 +10,7 @@ pub struct UninstallArgs { id: Id, #[arg(required = true, help = "Version or alias of tool")] - semver: VersionType, + semver: UnresolvedVersionSpec, } #[system] diff --git a/crates/cli/tests/alias_test.rs b/crates/cli/tests/alias_test.rs index ff3dfb81c..2bd56e153 100644 --- a/crates/cli/tests/alias_test.rs +++ b/crates/cli/tests/alias_test.rs @@ -1,6 +1,6 @@ mod utils; -use proto_core::{ToolManifest, VersionType}; +use proto_core::{ToolManifest, UnresolvedVersionSpec}; use starbase_sandbox::predicates::prelude::*; use std::collections::BTreeMap; use utils::*; @@ -44,7 +44,10 @@ mod alias { assert_eq!( manifest.aliases, - BTreeMap::from_iter([("example".into(), VersionType::parse("19.0.0").unwrap())]) + BTreeMap::from_iter([( + "example".into(), + UnresolvedVersionSpec::parse("19.0.0").unwrap() + )]) ); } @@ -54,9 +57,10 @@ mod alias { let manifest_file = sandbox.path().join("tools/node/manifest.json"); let mut manifest = ToolManifest::load(&manifest_file).unwrap(); - manifest - .aliases - .insert("example".into(), VersionType::parse("19.0.0").unwrap()); + manifest.aliases.insert( + "example".into(), + UnresolvedVersionSpec::parse("19.0.0").unwrap(), + ); manifest.save().unwrap(); let mut cmd = create_proto_command(sandbox.path()); @@ -71,7 +75,10 @@ mod alias { assert_eq!( manifest.aliases, - BTreeMap::from_iter([("example".into(), VersionType::parse("20.0.0").unwrap())]) + BTreeMap::from_iter([( + "example".into(), + UnresolvedVersionSpec::parse("20.0.0").unwrap() + )]) ); } diff --git a/crates/cli/tests/global_test.rs b/crates/cli/tests/global_test.rs index 3d5dcf41d..3a76c100d 100644 --- a/crates/cli/tests/global_test.rs +++ b/crates/cli/tests/global_test.rs @@ -1,6 +1,6 @@ mod utils; -use proto_core::{ToolManifest, VersionType}; +use proto_core::{ToolManifest, UnresolvedVersionSpec}; use utils::*; mod global { @@ -26,7 +26,7 @@ mod global { assert_eq!( manifest.default_version, - Some(VersionType::parse("19.0.0").unwrap()) + Some(UnresolvedVersionSpec::parse("19.0.0").unwrap()) ); } @@ -50,7 +50,27 @@ mod global { assert_eq!( manifest.default_version, - Some(VersionType::Alias("bundled".into())) + Some(UnresolvedVersionSpec::Alias("bundled".into())) + ); + } + + #[test] + fn can_set_partial_version_as_default() { + let temp = create_empty_sandbox(); + let manifest_file = temp.path().join("tools/npm/manifest.json"); + + assert!(!manifest_file.exists()); + + let mut cmd = create_proto_command(temp.path()); + cmd.arg("global").arg("npm").arg("1.2").assert().success(); + + assert!(manifest_file.exists()); + + let manifest = ToolManifest::load(manifest_file).unwrap(); + + assert_eq!( + manifest.default_version, + Some(UnresolvedVersionSpec::parse("1.2").unwrap()) ); } } diff --git a/crates/cli/tests/install_uninstall_test.rs b/crates/cli/tests/install_uninstall_test.rs index 0df08c054..4dc5598cf 100644 --- a/crates/cli/tests/install_uninstall_test.rs +++ b/crates/cli/tests/install_uninstall_test.rs @@ -1,6 +1,6 @@ mod utils; -use proto_core::{AliasOrVersion, ToolManifest, VersionType}; +use proto_core::{ToolManifest, UnresolvedVersionSpec, VersionSpec}; use starbase_sandbox::predicates::prelude::*; use std::collections::HashSet; use utils::*; @@ -134,15 +134,15 @@ mod install_uninstall { assert_eq!( manifest.default_version, - Some(VersionType::parse("19.0.0").unwrap()) + Some(UnresolvedVersionSpec::parse("19.0.0").unwrap()) ); assert_eq!( manifest.installed_versions, - HashSet::from_iter([AliasOrVersion::parse("19.0.0").unwrap()]) + HashSet::from_iter([VersionSpec::parse("19.0.0").unwrap()]) ); assert!(manifest .versions - .contains_key(&AliasOrVersion::parse("19.0.0").unwrap())); + .contains_key(&VersionSpec::parse("19.0.0").unwrap())); // Uninstall let mut cmd = create_proto_command(temp.path()); @@ -158,7 +158,7 @@ mod install_uninstall { assert_eq!(manifest.installed_versions, HashSet::default()); assert!(!manifest .versions - .contains_key(&AliasOrVersion::parse("19.0.0").unwrap())); + .contains_key(&VersionSpec::parse("19.0.0").unwrap())); } #[test] @@ -167,10 +167,10 @@ mod install_uninstall { let manifest_file = temp.path().join("tools/node/manifest.json"); let mut manifest = ToolManifest::load(&manifest_file).unwrap(); - manifest.default_version = Some(VersionType::parse("18.0.0").unwrap()); + manifest.default_version = Some(UnresolvedVersionSpec::parse("18.0.0").unwrap()); manifest .installed_versions - .insert(AliasOrVersion::parse("18.0.0").unwrap()); + .insert(VersionSpec::parse("18.0.0").unwrap()); manifest.save().unwrap(); let mut cmd = create_proto_command(temp.path()); @@ -186,13 +186,13 @@ mod install_uninstall { assert_eq!( manifest.default_version, - Some(VersionType::parse("19.0.0").unwrap()) + Some(UnresolvedVersionSpec::parse("19.0.0").unwrap()) ); assert_eq!( manifest.installed_versions, HashSet::from_iter([ - AliasOrVersion::parse("18.0.0").unwrap(), - AliasOrVersion::parse("19.0.0").unwrap(), + VersionSpec::parse("18.0.0").unwrap(), + VersionSpec::parse("19.0.0").unwrap(), ]) ); } diff --git a/crates/cli/tests/list_test.rs b/crates/cli/tests/list_test.rs index 3d6daf020..ea4df4021 100644 --- a/crates/cli/tests/list_test.rs +++ b/crates/cli/tests/list_test.rs @@ -1,6 +1,6 @@ mod utils; -use proto_core::{AliasOrVersion, ToolManifest, VersionType}; +use proto_core::{ToolManifest, UnresolvedVersionSpec, VersionSpec}; use utils::*; mod list { @@ -12,16 +12,16 @@ mod list { let mut manifest = ToolManifest::load(temp.path().join("tools/node/manifest.json")).unwrap(); - manifest.default_version = Some(VersionType::parse("19.0.0").unwrap()); + manifest.default_version = Some(UnresolvedVersionSpec::parse("19.0.0").unwrap()); manifest .installed_versions - .insert(AliasOrVersion::parse("19.0.0").unwrap()); + .insert(VersionSpec::parse("19.0.0").unwrap()); manifest .installed_versions - .insert(AliasOrVersion::parse("18.0.0").unwrap()); + .insert(VersionSpec::parse("18.0.0").unwrap()); manifest .installed_versions - .insert(AliasOrVersion::parse("17.0.0").unwrap()); + .insert(VersionSpec::parse("17.0.0").unwrap()); manifest.save().unwrap(); let mut cmd = create_proto_command(temp.path()); diff --git a/crates/cli/tests/local_test.rs b/crates/cli/tests/local_test.rs index c73fc5d16..c300f8a79 100644 --- a/crates/cli/tests/local_test.rs +++ b/crates/cli/tests/local_test.rs @@ -63,15 +63,11 @@ npm = "9.0.0" ); let mut cmd = create_proto_command(temp.path()); - cmd.arg("local") - .arg("node") - .arg("19.0.0") - .assert() - .success(); + cmd.arg("local").arg("node").arg("19").assert().success(); assert_eq!( fs::read_to_string(version_file).unwrap(), - r#"node = "19.0.0" + r#"node = "19" npm = "9.0.0" "# ) @@ -97,4 +93,18 @@ npm = "9.0.0" "npm = \"bundled\"\n" ) } + + #[test] + fn can_set_partial_version() { + let temp = create_empty_sandbox(); + let version_file = temp.path().join(".prototools"); + + assert!(!version_file.exists()); + + let mut cmd = create_proto_command(temp.path()); + cmd.arg("local").arg("npm").arg("1.2").assert().success(); + + assert!(version_file.exists()); + assert_eq!(fs::read_to_string(version_file).unwrap(), "npm = \"1.2\"\n") + } } diff --git a/crates/cli/tests/plugins_test.rs b/crates/cli/tests/plugins_test.rs index 6d9352160..68585e314 100644 --- a/crates/cli/tests/plugins_test.rs +++ b/crates/cli/tests/plugins_test.rs @@ -2,7 +2,8 @@ mod utils; use futures::Future; use proto_core::{ - load_tool_from_locator, Id, PluginLocator, ProtoEnvironment, Tool, UserConfig, VersionType, + load_tool_from_locator, Id, PluginLocator, ProtoEnvironment, Tool, UnresolvedVersionSpec, + UserConfig, }; use std::env; use std::path::{Path, PathBuf}; @@ -20,7 +21,7 @@ where env::set_var("PROTO_HOME", fixture.path().to_string_lossy().to_string()); - tool.setup(&VersionType::parse("1.0.0").unwrap()) + tool.setup(&UnresolvedVersionSpec::parse("1.0.0").unwrap()) .await .unwrap(); diff --git a/crates/cli/tests/run_test.rs b/crates/cli/tests/run_test.rs index 11a562554..2728f011a 100644 --- a/crates/cli/tests/run_test.rs +++ b/crates/cli/tests/run_test.rs @@ -1,6 +1,6 @@ mod utils; -use proto_core::{AliasOrVersion, ToolManifest}; +use proto_core::{ToolManifest, VersionSpec}; use starbase_sandbox::predicates::prelude::*; use std::{env, fs}; use utils::*; @@ -172,7 +172,7 @@ mod run { .assert(); let manifest = ToolManifest::load(&manifest_file).unwrap(); - let version = AliasOrVersion::parse("19.0.0").unwrap(); + let version = VersionSpec::parse("19.0.0").unwrap(); let last_used_at = manifest.versions.get(&version).unwrap().last_used_at; diff --git a/crates/cli/tests/unalias_test.rs b/crates/cli/tests/unalias_test.rs index a0565a097..cc8d10fe3 100644 --- a/crates/cli/tests/unalias_test.rs +++ b/crates/cli/tests/unalias_test.rs @@ -1,6 +1,6 @@ mod utils; -use proto_core::{ToolManifest, VersionType}; +use proto_core::{ToolManifest, UnresolvedVersionSpec}; use starbase_sandbox::predicates::prelude::*; use std::collections::BTreeMap; use utils::*; @@ -24,9 +24,10 @@ mod unalias { let manifest_file = sandbox.path().join("tools/node/manifest.json"); let mut manifest = ToolManifest::load(&manifest_file).unwrap(); - manifest - .aliases - .insert("example".into(), VersionType::parse("19.0.0").unwrap()); + manifest.aliases.insert( + "example".into(), + UnresolvedVersionSpec::parse("19.0.0").unwrap(), + ); manifest.save().unwrap(); let mut cmd = create_proto_command(sandbox.path()); @@ -47,9 +48,10 @@ mod unalias { let manifest_file = sandbox.path().join("tools/node/manifest.json"); let mut manifest = ToolManifest::load(&manifest_file).unwrap(); - manifest - .aliases - .insert("example".into(), VersionType::parse("19.0.0").unwrap()); + manifest.aliases.insert( + "example".into(), + UnresolvedVersionSpec::parse("19.0.0").unwrap(), + ); manifest.save().unwrap(); let mut cmd = create_proto_command(sandbox.path()); @@ -63,7 +65,10 @@ mod unalias { assert_eq!( manifest.aliases, - BTreeMap::from_iter([("example".into(), VersionType::parse("19.0.0").unwrap())]) + BTreeMap::from_iter([( + "example".into(), + UnresolvedVersionSpec::parse("19.0.0").unwrap() + )]) ); } } diff --git a/crates/core/src/events.rs b/crates/core/src/events.rs index 60a0a0131..2404ae1ff 100644 --- a/crates/core/src/events.rs +++ b/crates/core/src/events.rs @@ -16,11 +16,11 @@ macro_rules! impl_event { } impl_event!(InstallingEvent, { - pub version: AliasOrVersion, + pub version: VersionSpec, }); impl_event!(InstalledEvent, { - pub version: AliasOrVersion, + pub version: VersionSpec, }); impl_event!(InstalledGlobalEvent, { @@ -28,11 +28,11 @@ impl_event!(InstalledGlobalEvent, { }); impl_event!(UninstallingEvent, { - pub version: AliasOrVersion, + pub version: VersionSpec, }); impl_event!(UninstalledEvent, { - pub version: AliasOrVersion, + pub version: VersionSpec, }); impl_event!(UninstalledGlobalEvent, { @@ -45,6 +45,6 @@ impl_event!(CreatedShimsEvent, { }); impl_event!(ResolvedVersionEvent, { - pub candidate: VersionType, - pub version: AliasOrVersion, + pub candidate: UnresolvedVersionSpec, + pub version: VersionSpec, }); diff --git a/crates/core/src/tool.rs b/crates/core/src/tool.rs index 392f4df9c..a380c51c0 100644 --- a/crates/core/src/tool.rs +++ b/crates/core/src/tool.rs @@ -6,7 +6,7 @@ use crate::shimmer::{ create_global_shim, create_local_shim, get_shim_file_name, ShimContext, SHIM_VERSION, }; use crate::tool_manifest::ToolManifest; -use crate::version::{AliasOrVersion, VersionType}; +use crate::version::{UnresolvedVersionSpec, VersionSpec}; use crate::version_resolver::VersionResolver; use crate::{ download_from_url, is_archive_file, read_json_file_with_lock, write_json_file_with_lock, @@ -36,7 +36,7 @@ pub struct Tool { pub metadata: ToolMetadataOutput, pub plugin: PluginContainer<'static>, pub proto: ProtoEnvironment, - pub version: Option, + pub version: Option, // Events pub on_created_shims: Emitter, @@ -179,7 +179,7 @@ impl Tool { } /// Return the resolved version or "latest". - pub fn get_resolved_version(&self) -> AliasOrVersion { + pub fn get_resolved_version(&self) -> VersionSpec { self.version.clone().unwrap_or_default() } @@ -214,7 +214,7 @@ impl Tool { } /// Explicitly set the version to use. - pub fn set_version(&mut self, version: AliasOrVersion) { + pub fn set_version(&mut self, version: VersionSpec) { self.version = Some(version); } @@ -319,7 +319,7 @@ impl Tool { if let Some(default) = sync_changes.default_version { modified = true; - self.manifest.default_version = Some(VersionType::parse(&default)?); + self.manifest.default_version = Some(UnresolvedVersionSpec::parse(&default)?); } if let Some(versions) = sync_changes.versions { @@ -329,7 +329,7 @@ impl Tool { let mut installed = HashSet::new(); for version in versions { - let key = AliasOrVersion::Version(version); + let key = VersionSpec::Version(version); let value = self .manifest .versions @@ -360,7 +360,7 @@ impl Tool { /// To reduce network overhead, results will be cached for 24 hours. pub async fn load_version_resolver( &self, - initial_version: &VersionType, + initial_version: &UnresolvedVersionSpec, ) -> miette::Result { debug!(tool = self.id.as_str(), "Loading available versions"); @@ -415,14 +415,17 @@ impl Tool { /// Given an initial version, resolve it to a fully qualifed and semantic version /// (or alias) according to the tool's ecosystem. - pub async fn resolve_version(&mut self, initial_version: &VersionType) -> miette::Result<()> { + pub async fn resolve_version( + &mut self, + initial_version: &UnresolvedVersionSpec, + ) -> miette::Result<()> { if self.version.is_some() { return Ok(()); } // If offline but we have a fully qualified semantic version, // exit early and assume the version is legitimate! - if is_offline() && matches!(initial_version, VersionType::Version(_)) { + if is_offline() && matches!(initial_version, UnresolvedVersionSpec::Version(_)) { let version = initial_version.to_explicit_version(); self.on_resolved_version @@ -444,7 +447,7 @@ impl Tool { ); let resolver = self.load_version_resolver(initial_version).await?; - let mut version = AliasOrVersion::default(); + let mut version = VersionSpec::default(); let mut resolved = false; if self.plugin.has_func("resolve_version") { @@ -464,8 +467,9 @@ impl Tool { ); resolved = true; - version = - AliasOrVersion::Version(resolver.resolve(&VersionType::parse(candidate)?)?); + version = VersionSpec::Version( + resolver.resolve(&UnresolvedVersionSpec::parse(candidate)?)?, + ); } if let Some(candidate) = result.version { @@ -476,12 +480,12 @@ impl Tool { ); resolved = true; - version = AliasOrVersion::parse(candidate)?; + version = VersionSpec::parse(candidate)?; } } if !resolved { - version = AliasOrVersion::Version(resolver.resolve(initial_version)?); + version = VersionSpec::Version(resolver.resolve(initial_version)?); } debug!( @@ -511,7 +515,7 @@ impl Tool { pub async fn detect_version_from( &self, current_dir: &Path, - ) -> miette::Result> { + ) -> miette::Result> { if !self.plugin.has_func("detect_version_files") { return Ok(None); } @@ -558,7 +562,7 @@ impl Tool { "Detected a version" ); - return Ok(Some(VersionType::parse(version)?)); + return Ok(Some(UnresolvedVersionSpec::parse(version)?)); } Ok(None) @@ -1122,7 +1126,10 @@ impl Tool { } /// Return true if the tool has been setup (installed and binaries are located). - pub async fn is_setup(&mut self, initial_version: &VersionType) -> miette::Result { + pub async fn is_setup( + &mut self, + initial_version: &UnresolvedVersionSpec, + ) -> miette::Result { self.resolve_version(initial_version).await?; let install_dir = self.get_tool_dir(); @@ -1155,7 +1162,7 @@ impl Tool { /// Setup the tool by resolving a semantic version, installing the tool, /// locating binaries, creating shims, and more. - pub async fn setup(&mut self, initial_version: &VersionType) -> miette::Result { + pub async fn setup(&mut self, initial_version: &UnresolvedVersionSpec) -> miette::Result { self.resolve_version(initial_version).await?; if self.install().await? { @@ -1166,7 +1173,7 @@ impl Tool { let mut default = None; if let Some(default_version) = &self.metadata.default_version { - default = Some(VersionType::parse(default_version)?); + default = Some(UnresolvedVersionSpec::parse(default_version)?); } self.manifest diff --git a/crates/core/src/tool_manifest.rs b/crates/core/src/tool_manifest.rs index 457e5da0e..0aded1ae6 100644 --- a/crates/core/src/tool_manifest.rs +++ b/crates/core/src/tool_manifest.rs @@ -1,6 +1,6 @@ use crate::{ helpers::{read_json_file_with_lock, write_json_file_with_lock}, - version::{AliasOrVersion, VersionType}, + version::{UnresolvedVersionSpec, VersionSpec}, }; use serde::{Deserialize, Serialize}; use std::{ @@ -42,13 +42,13 @@ impl Default for ToolManifestVersion { #[serde(default)] pub struct ToolManifest { // Partial versions allowed - pub aliases: BTreeMap, - pub default_version: Option, + pub aliases: BTreeMap, + pub default_version: Option, // Full versions only - pub installed_versions: HashSet, + pub installed_versions: HashSet, pub shim_version: u8, - pub versions: BTreeMap, + pub versions: BTreeMap, #[serde(skip)] pub path: PathBuf, @@ -87,8 +87,8 @@ impl ToolManifest { pub fn insert_version( &mut self, - version: AliasOrVersion, - default_version: Option, + version: VersionSpec, + default_version: Option, ) -> miette::Result<()> { if self.default_version.is_none() { self.default_version = @@ -105,7 +105,7 @@ impl ToolManifest { Ok(()) } - pub fn remove_version(&mut self, version: AliasOrVersion) -> miette::Result<()> { + pub fn remove_version(&mut self, version: VersionSpec) -> miette::Result<()> { self.installed_versions.remove(&version); // Remove default version if nothing available @@ -124,7 +124,7 @@ impl ToolManifest { Ok(()) } - pub fn track_used_at(&mut self, version: &AliasOrVersion) { + pub fn track_used_at(&mut self, version: &VersionSpec) { self.versions .entry(version.to_owned()) .and_modify(|v| { diff --git a/crates/core/src/tools_config.rs b/crates/core/src/tools_config.rs index b43b18608..cd321fa6f 100644 --- a/crates/core/src/tools_config.rs +++ b/crates/core/src/tools_config.rs @@ -1,4 +1,4 @@ -use crate::version::VersionType; +use crate::version::UnresolvedVersionSpec; use miette::IntoDiagnostic; use serde::{Deserialize, Serialize}; use starbase_utils::{fs, toml}; @@ -18,7 +18,7 @@ fn is_empty(map: &BTreeMap) -> bool { #[serde(default, rename_all = "kebab-case")] pub struct ToolsConfig { #[serde(flatten, skip_serializing_if = "is_empty")] - pub tools: BTreeMap, + pub tools: BTreeMap, #[serde(skip_serializing_if = "is_empty")] pub plugins: BTreeMap, diff --git a/crates/core/src/version.rs b/crates/core/src/version.rs index add982870..0426c1420 100644 --- a/crates/core/src/version.rs +++ b/crates/core/src/version.rs @@ -10,41 +10,41 @@ use std::str::FromStr; #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] #[serde(untagged, into = "String", try_from = "String")] -pub enum VersionType { +pub enum UnresolvedVersionSpec { Alias(String), - ReqAll(VersionReq), + Req(VersionReq), ReqAny(Vec), Version(Version), } -impl VersionType { +impl UnresolvedVersionSpec { pub fn parse>(value: T) -> miette::Result { Ok(Self::from_str(value.as_ref())?) } - pub fn to_explicit_version(&self) -> AliasOrVersion { + pub fn to_explicit_version(&self) -> VersionSpec { match self { - VersionType::Alias(alias) => AliasOrVersion::Alias(alias.to_owned()), - VersionType::Version(version) => AliasOrVersion::Version(version.to_owned()), + UnresolvedVersionSpec::Alias(alias) => VersionSpec::Alias(alias.to_owned()), + UnresolvedVersionSpec::Version(version) => VersionSpec::Version(version.to_owned()), _ => unreachable!(), } } } -impl Default for VersionType { +impl Default for UnresolvedVersionSpec { fn default() -> Self { Self::Alias("latest".into()) } } -impl FromStr for VersionType { +impl FromStr for UnresolvedVersionSpec { type Err = ProtoError; fn from_str(value: &str) -> Result { let value = remove_space_after_gtlt(remove_v_prefix(value.trim().replace(".*", ""))); if is_alias_name(&value) { - return Ok(VersionType::Alias(value)); + return Ok(UnresolvedVersionSpec::Alias(value)); } let handle_error = |error: semver::Error| ProtoError::Semver { @@ -64,41 +64,41 @@ impl FromStr for VersionType { any.push(VersionReq::parse(req).map_err(handle_error)?); } - return Ok(VersionType::ReqAny(any)); + return Ok(UnresolvedVersionSpec::ReqAny(any)); } // AND requirements if value.contains(',') { - return Ok(VersionType::ReqAll( + return Ok(UnresolvedVersionSpec::Req( VersionReq::parse(&value).map_err(handle_error)?, )); } else if value.contains(' ') { - return Ok(VersionType::ReqAll( + return Ok(UnresolvedVersionSpec::Req( VersionReq::parse(&value.replace(' ', ", ")).map_err(handle_error)?, )); } Ok(match value.chars().next().unwrap() { '=' | '^' | '~' | '>' | '<' | '*' => { - VersionType::ReqAll(VersionReq::parse(&value).map_err(handle_error)?) + UnresolvedVersionSpec::Req(VersionReq::parse(&value).map_err(handle_error)?) } _ => { let dot_count = value.match_indices('.').collect::>().len(); // If not fully qualified, match using a requirement if dot_count < 2 { - VersionType::ReqAll( + UnresolvedVersionSpec::Req( VersionReq::parse(&format!("~{value}")).map_err(handle_error)?, ) } else { - VersionType::Version(Version::parse(&value).map_err(handle_error)?) + UnresolvedVersionSpec::Version(Version::parse(&value).map_err(handle_error)?) } } }) } } -impl TryFrom for VersionType { +impl TryFrom for UnresolvedVersionSpec { type Error = ProtoError; fn try_from(value: String) -> Result { @@ -106,17 +106,17 @@ impl TryFrom for VersionType { } } -impl Into for VersionType { +impl Into for UnresolvedVersionSpec { fn into(self) -> String { self.to_string() } } -impl Display for VersionType { +impl Display for UnresolvedVersionSpec { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Alias(alias) => write!(f, "{}", alias), - Self::ReqAll(req) => write!(f, "{}", req), + Self::Req(req) => write!(f, "{}", req), Self::ReqAny(reqs) => write!( f, "{}", @@ -130,11 +130,11 @@ impl Display for VersionType { } } -impl PartialEq for VersionType { - fn eq(&self, other: &AliasOrVersion) -> bool { +impl PartialEq for UnresolvedVersionSpec { + fn eq(&self, other: &VersionSpec) -> bool { match (self, other) { - (Self::Alias(a1), AliasOrVersion::Alias(a2)) => a1 == a2, - (Self::Version(v1), AliasOrVersion::Version(v2)) => v1 == v2, + (Self::Alias(a1), VersionSpec::Alias(a2)) => a1 == a2, + (Self::Version(v1), VersionSpec::Version(v2)) => v1 == v2, _ => false, } } @@ -142,12 +142,12 @@ impl PartialEq for VersionType { #[derive(Clone, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)] #[serde(untagged, into = "String", try_from = "String")] -pub enum AliasOrVersion { +pub enum VersionSpec { Alias(String), Version(Version), } -impl AliasOrVersion { +impl VersionSpec { pub fn parse>(value: T) -> miette::Result { Ok(Self::from_str(value.as_ref())?) } @@ -166,31 +166,31 @@ impl AliasOrVersion { } } - pub fn to_implicit_type(&self) -> VersionType { + pub fn to_implicit_type(&self) -> UnresolvedVersionSpec { match self { - Self::Alias(alias) => VersionType::Alias(alias.to_owned()), - Self::Version(version) => VersionType::Version(version.to_owned()), + Self::Alias(alias) => UnresolvedVersionSpec::Alias(alias.to_owned()), + Self::Version(version) => UnresolvedVersionSpec::Version(version.to_owned()), } } } -impl Default for AliasOrVersion { +impl Default for VersionSpec { fn default() -> Self { Self::Alias("latest".into()) } } -impl FromStr for AliasOrVersion { +impl FromStr for VersionSpec { type Err = ProtoError; fn from_str(value: &str) -> Result { let value = remove_space_after_gtlt(remove_v_prefix(value.trim().replace(".*", ""))); if is_alias_name(&value) { - return Ok(AliasOrVersion::Alias(value)); + return Ok(VersionSpec::Alias(value)); } - Ok(AliasOrVersion::Version(Version::parse(&value).map_err( + Ok(VersionSpec::Version(Version::parse(&value).map_err( |error| ProtoError::Semver { version: value, error, @@ -199,7 +199,7 @@ impl FromStr for AliasOrVersion { } } -impl TryFrom for AliasOrVersion { +impl TryFrom for VersionSpec { type Error = ProtoError; fn try_from(value: String) -> Result { @@ -207,19 +207,19 @@ impl TryFrom for AliasOrVersion { } } -impl Into for AliasOrVersion { +impl Into for VersionSpec { fn into(self) -> String { self.to_string() } } -impl Debug for AliasOrVersion { +impl Debug for VersionSpec { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self) } } -impl Display for AliasOrVersion { +impl Display for VersionSpec { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Alias(alias) => write!(f, "{}", alias), @@ -228,7 +228,7 @@ impl Display for AliasOrVersion { } } -impl PartialEq<&str> for AliasOrVersion { +impl PartialEq<&str> for VersionSpec { fn eq(&self, other: &&str) -> bool { match self { Self::Alias(alias) => alias == other, @@ -237,7 +237,7 @@ impl PartialEq<&str> for AliasOrVersion { } } -impl PartialEq for AliasOrVersion { +impl PartialEq for VersionSpec { fn eq(&self, other: &Version) -> bool { match self { Self::Version(version) => version == other, diff --git a/crates/core/src/version_detector.rs b/crates/core/src/version_detector.rs index d37c8e708..d105729d5 100644 --- a/crates/core/src/version_detector.rs +++ b/crates/core/src/version_detector.rs @@ -1,14 +1,14 @@ use crate::error::ProtoError; use crate::tool::Tool; use crate::tools_config::ToolsConfig; -use crate::version::VersionType; +use crate::version::UnresolvedVersionSpec; use std::{env, path::Path}; use tracing::{debug, trace}; pub async fn detect_version( tool: &Tool, - forced_version: Option, -) -> miette::Result { + forced_version: Option, +) -> miette::Result { let mut candidate = forced_version; // Env var takes highest priority @@ -23,7 +23,7 @@ pub async fn detect_version( "Detected version from environment variable", ); - candidate = Some(VersionType::parse(session_version)?); + candidate = Some(UnresolvedVersionSpec::parse(session_version)?); } else { trace!( tool = tool.id.as_str(), diff --git a/crates/core/src/version_resolver.rs b/crates/core/src/version_resolver.rs index e7f05f3cf..59762d0e6 100644 --- a/crates/core/src/version_resolver.rs +++ b/crates/core/src/version_resolver.rs @@ -1,14 +1,14 @@ use crate::error::ProtoError; use crate::tool_manifest::ToolManifest; -use crate::version::VersionType; -use crate::AliasOrVersion; +use crate::version::UnresolvedVersionSpec; +use crate::VersionSpec; use proto_pdk_api::LoadVersionsOutput; use semver::{Version, VersionReq}; use std::collections::{BTreeMap, HashSet}; #[derive(Debug, Default)] pub struct VersionResolver<'tool> { - pub aliases: BTreeMap, + pub aliases: BTreeMap, pub versions: Vec, manifest: Option<&'tool ToolManifest>, @@ -22,13 +22,13 @@ impl<'tool> VersionResolver<'tool> { for (alias, version) in output.aliases { resolver .aliases - .insert(alias, VersionType::Version(version)); + .insert(alias, UnresolvedVersionSpec::Version(version)); } if let Some(latest) = output.latest { resolver .aliases - .insert("latest".into(), VersionType::Version(latest)); + .insert("latest".into(), UnresolvedVersionSpec::Version(latest)); } // Sort from newest to oldest @@ -43,7 +43,7 @@ impl<'tool> VersionResolver<'tool> { Ok(()) } - pub fn resolve(&self, candidate: &VersionType) -> miette::Result { + pub fn resolve(&self, candidate: &UnresolvedVersionSpec) -> miette::Result { resolve_version(candidate, &self.versions, &self.aliases, self.manifest) } } @@ -66,24 +66,24 @@ where } // Filter out aliases because they cannot be matched against -fn extract_installed_versions(installed: &HashSet) -> Vec<&Version> { +fn extract_installed_versions(installed: &HashSet) -> Vec<&Version> { installed .iter() .filter_map(|item| match item { - AliasOrVersion::Alias(_) => None, - AliasOrVersion::Version(v) => Some(v), + VersionSpec::Alias(_) => None, + VersionSpec::Version(v) => Some(v), }) .collect() } pub fn resolve_version( - candidate: &VersionType, + candidate: &UnresolvedVersionSpec, versions: &[Version], - aliases: &BTreeMap, + aliases: &BTreeMap, manifest: Option<&ToolManifest>, ) -> miette::Result { match &candidate { - VersionType::Alias(alias) => { + UnresolvedVersionSpec::Alias(alias) => { if let Some(manifest) = manifest { if let Some(alias_type) = manifest.aliases.get(alias) { return resolve_version(alias_type, versions, aliases, Some(manifest)); @@ -94,7 +94,7 @@ pub fn resolve_version( return resolve_version(alias_type, versions, aliases, manifest); } } - VersionType::ReqAll(req) => { + UnresolvedVersionSpec::Req(req) => { if let Some(manifest) = manifest { if let Some(version) = match_highest_version( req, @@ -108,7 +108,7 @@ pub fn resolve_version( return Ok(version); } } - VersionType::ReqAny(reqs) => { + UnresolvedVersionSpec::ReqAny(reqs) => { for req in reqs { if let Some(manifest) = manifest { if let Some(version) = match_highest_version( @@ -124,7 +124,7 @@ pub fn resolve_version( } } } - VersionType::Version(ver) => { + UnresolvedVersionSpec::Version(ver) => { for version in versions { if ver == version { return Ok(ver.to_owned()); diff --git a/crates/core/tests/tools_config_test.rs b/crates/core/tests/tools_config_test.rs index 4ff71248c..d16702c23 100644 --- a/crates/core/tests/tools_config_test.rs +++ b/crates/core/tests/tools_config_test.rs @@ -1,4 +1,4 @@ -use proto_core::{ToolsConfig, VersionType}; +use proto_core::{ToolsConfig, UnresolvedVersionSpec}; use starbase_sandbox::create_empty_sandbox; use std::collections::BTreeMap; use std::str::FromStr; @@ -56,8 +56,14 @@ kebab-case = "source:./camel.toml" assert_eq!( config.tools, BTreeMap::from_iter([ - (Id::raw("node"), VersionType::from_str("12.0.0").unwrap()), - (Id::raw("rust"), VersionType::Alias("stable".into())), + ( + Id::raw("node"), + UnresolvedVersionSpec::from_str("12.0.0").unwrap() + ), + ( + Id::raw("rust"), + UnresolvedVersionSpec::Alias("stable".into()) + ), ]) ); @@ -87,12 +93,14 @@ kebab-case = "source:./camel.toml" let sandbox = create_empty_sandbox(); let mut config = ToolsConfig::load_from(sandbox.path()).unwrap(); - config - .tools - .insert(Id::raw("node"), VersionType::from_str("12.0.0").unwrap()); - config - .tools - .insert(Id::raw("rust"), VersionType::Alias("stable".into())); + config.tools.insert( + Id::raw("node"), + UnresolvedVersionSpec::from_str("12.0.0").unwrap(), + ); + config.tools.insert( + Id::raw("rust"), + UnresolvedVersionSpec::Alias("stable".into()), + ); config.plugins.insert( Id::raw("foo"), @@ -159,9 +167,18 @@ foo = "source:./test.toml" assert_eq!( config.tools, BTreeMap::from_iter([ - (Id::raw("node"), VersionType::parse("1.2.3").unwrap()), - (Id::raw("bun"), VersionType::parse("4.5.6").unwrap()), - (Id::raw("deno"), VersionType::parse("7.8.9").unwrap()), + ( + Id::raw("node"), + UnresolvedVersionSpec::parse("1.2.3").unwrap() + ), + ( + Id::raw("bun"), + UnresolvedVersionSpec::parse("4.5.6").unwrap() + ), + ( + Id::raw("deno"), + UnresolvedVersionSpec::parse("7.8.9").unwrap() + ), ]) ); diff --git a/crates/core/tests/version_resolver_test.rs b/crates/core/tests/version_resolver_test.rs index b0b49246f..979625a45 100644 --- a/crates/core/tests/version_resolver_test.rs +++ b/crates/core/tests/version_resolver_test.rs @@ -1,4 +1,4 @@ -use proto_core::{resolve_version, VersionType}; +use proto_core::{resolve_version, UnresolvedVersionSpec}; use semver::Version; use std::collections::BTreeMap; @@ -19,18 +19,24 @@ mod version_resolver { ] } - fn create_aliases() -> BTreeMap { + fn create_aliases() -> BTreeMap { BTreeMap::from_iter([ ( "latest".into(), - VersionType::Version(Version::new(10, 0, 0)), + UnresolvedVersionSpec::Version(Version::new(10, 0, 0)), + ), + ( + "stable".into(), + UnresolvedVersionSpec::Alias("latest".into()), ), - ("stable".into(), VersionType::Alias("latest".into())), ( "no-version".into(), - VersionType::Version(Version::new(20, 0, 0)), + UnresolvedVersionSpec::Version(Version::new(20, 0, 0)), + ), + ( + "no-alias".into(), + UnresolvedVersionSpec::Alias("missing".into()), ), - ("no-alias".into(), VersionType::Alias("missing".into())), ]) } @@ -41,7 +47,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::Alias("latest".into()), + &UnresolvedVersionSpec::Alias("latest".into()), &versions, &aliases, None, @@ -52,7 +58,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::Alias("stable".into()), + &UnresolvedVersionSpec::Alias("stable".into()), &versions, &aliases, None, @@ -69,7 +75,7 @@ mod version_resolver { let aliases = create_aliases(); resolve_version( - &VersionType::Alias("unknown".into()), + &UnresolvedVersionSpec::Alias("unknown".into()), &versions, &aliases, None, @@ -84,7 +90,7 @@ mod version_resolver { let aliases = create_aliases(); resolve_version( - &VersionType::Alias("no-alias".into()), + &UnresolvedVersionSpec::Alias("no-alias".into()), &versions, &aliases, None, @@ -99,7 +105,7 @@ mod version_resolver { let aliases = create_aliases(); resolve_version( - &VersionType::Alias("no-version".into()), + &UnresolvedVersionSpec::Alias("no-version".into()), &versions, &aliases, None, @@ -114,7 +120,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::Version(Version::new(1, 10, 5)), + &UnresolvedVersionSpec::Version(Version::new(1, 10, 5)), &versions, &aliases, None, @@ -125,7 +131,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::Version(Version::new(8, 0, 0)), + &UnresolvedVersionSpec::Version(Version::new(8, 0, 0)), &versions, &aliases, None, @@ -142,7 +148,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse("1.2").unwrap(), + &UnresolvedVersionSpec::parse("1.2").unwrap(), &versions, &aliases, None, @@ -153,7 +159,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse("1.0").unwrap(), + &UnresolvedVersionSpec::parse("1.0").unwrap(), &versions, &aliases, None, @@ -163,7 +169,13 @@ mod version_resolver { ); assert_eq!( - resolve_version(&VersionType::parse("1").unwrap(), &versions, &aliases, None,).unwrap(), + resolve_version( + &UnresolvedVersionSpec::parse("1").unwrap(), + &versions, + &aliases, + None, + ) + .unwrap(), Version::new(1, 10, 5) ); } @@ -175,7 +187,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse("v8.0.0").unwrap(), + &UnresolvedVersionSpec::parse("v8.0.0").unwrap(), &versions, &aliases, None, @@ -186,7 +198,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse("V8").unwrap(), + &UnresolvedVersionSpec::parse("V8").unwrap(), &versions, &aliases, None, @@ -203,7 +215,7 @@ mod version_resolver { let aliases = create_aliases(); resolve_version( - &VersionType::Version(Version::new(20, 0, 0)), + &UnresolvedVersionSpec::Version(Version::new(20, 0, 0)), &versions, &aliases, None, @@ -218,7 +230,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse("^8").unwrap(), + &UnresolvedVersionSpec::parse("^8").unwrap(), &versions, &aliases, None, @@ -229,7 +241,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse("~1.1").unwrap(), + &UnresolvedVersionSpec::parse("~1.1").unwrap(), &versions, &aliases, None, @@ -240,7 +252,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse(">1 <10").unwrap(), + &UnresolvedVersionSpec::parse(">1 <10").unwrap(), &versions, &aliases, None, @@ -251,7 +263,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse(">1, <10").unwrap(), + &UnresolvedVersionSpec::parse(">1, <10").unwrap(), &versions, &aliases, None, @@ -263,7 +275,7 @@ mod version_resolver { // Highest match assert_eq!( resolve_version( - &VersionType::parse("^1").unwrap(), + &UnresolvedVersionSpec::parse("^1").unwrap(), &versions, &aliases, None, @@ -274,7 +286,13 @@ mod version_resolver { // Star (latest) assert_eq!( - resolve_version(&VersionType::parse("*").unwrap(), &versions, &aliases, None,).unwrap(), + resolve_version( + &UnresolvedVersionSpec::parse("*").unwrap(), + &versions, + &aliases, + None, + ) + .unwrap(), Version::new(10, 0, 0) ); } @@ -286,7 +304,7 @@ mod version_resolver { let aliases = create_aliases(); resolve_version( - &VersionType::parse("^20").unwrap(), + &UnresolvedVersionSpec::parse("^20").unwrap(), &versions, &aliases, None, @@ -301,7 +319,7 @@ mod version_resolver { assert_eq!( resolve_version( - &VersionType::parse("^1 || ^6 || ^8").unwrap(), + &UnresolvedVersionSpec::parse("^1 || ^6 || ^8").unwrap(), &versions, &aliases, None, @@ -318,7 +336,7 @@ mod version_resolver { let aliases = create_aliases(); resolve_version( - &VersionType::parse("^3 || ^5 || ^9").unwrap(), + &UnresolvedVersionSpec::parse("^3 || ^5 || ^9").unwrap(), &versions, &aliases, None, @@ -332,7 +350,13 @@ mod version_resolver { let aliases = create_aliases(); for req in [">= 1.5.9", "> 1.5.0", ">= 1.2", "> 1.2", "< 1.2", "<= 1.2"] { - resolve_version(&VersionType::parse(req).unwrap(), &versions, &aliases, None).unwrap(); + resolve_version( + &UnresolvedVersionSpec::parse(req).unwrap(), + &versions, + &aliases, + None, + ) + .unwrap(); } } } diff --git a/crates/core/tests/version_test.rs b/crates/core/tests/version_test.rs index a3cf93530..2ff7cad0c 100644 --- a/crates/core/tests/version_test.rs +++ b/crates/core/tests/version_test.rs @@ -1,4 +1,4 @@ -use proto_core::VersionType; +use proto_core::UnresolvedVersionSpec; use semver::{Version, VersionReq}; use std::str::FromStr; @@ -8,16 +8,16 @@ mod version_type { #[test] fn parses_alias() { assert_eq!( - VersionType::from_str("stable").unwrap(), - VersionType::Alias("stable".to_owned()) + UnresolvedVersionSpec::from_str("stable").unwrap(), + UnresolvedVersionSpec::Alias("stable".to_owned()) ); assert_eq!( - VersionType::from_str("latest").unwrap(), - VersionType::Alias("latest".to_owned()) + UnresolvedVersionSpec::from_str("latest").unwrap(), + UnresolvedVersionSpec::Alias("latest".to_owned()) ); assert_eq!( - VersionType::from_str("lts-2014").unwrap(), - VersionType::Alias("lts-2014".to_owned()) + UnresolvedVersionSpec::from_str("lts-2014").unwrap(), + UnresolvedVersionSpec::Alias("lts-2014".to_owned()) ); } @@ -25,8 +25,8 @@ mod version_type { fn parses_req() { for req in ["=1.2.3", "^1.2", "~1", ">1.2.0", "<1", "*", ">1, <=1.5"] { assert_eq!( - VersionType::from_str(req).unwrap(), - VersionType::ReqAll(VersionReq::parse(req).unwrap()) + UnresolvedVersionSpec::from_str(req).unwrap(), + UnresolvedVersionSpec::Req(VersionReq::parse(req).unwrap()) ); } } @@ -34,24 +34,24 @@ mod version_type { #[test] fn parses_req_spaces() { assert_eq!( - VersionType::from_str("> 10").unwrap(), - VersionType::ReqAll(VersionReq::parse(">10").unwrap()) + UnresolvedVersionSpec::from_str("> 10").unwrap(), + UnresolvedVersionSpec::Req(VersionReq::parse(">10").unwrap()) ); assert_eq!( - VersionType::from_str("1.2 , 2").unwrap(), - VersionType::ReqAll(VersionReq::parse("1.2, 2").unwrap()) + UnresolvedVersionSpec::from_str("1.2 , 2").unwrap(), + UnresolvedVersionSpec::Req(VersionReq::parse("1.2, 2").unwrap()) ); assert_eq!( - VersionType::from_str(">= 1.2 < 2").unwrap(), - VersionType::ReqAll(VersionReq::parse(">=1.2, <2").unwrap()) + UnresolvedVersionSpec::from_str(">= 1.2 < 2").unwrap(), + UnresolvedVersionSpec::Req(VersionReq::parse(">=1.2, <2").unwrap()) ); } #[test] fn parses_req_any() { assert_eq!( - VersionType::from_str("^1 || ~2 || =3").unwrap(), - VersionType::ReqAny(vec![ + UnresolvedVersionSpec::from_str("^1 || ~2 || =3").unwrap(), + UnresolvedVersionSpec::ReqAny(vec![ VersionReq::parse("~2").unwrap(), VersionReq::parse("^1").unwrap(), VersionReq::parse("=3").unwrap(), @@ -62,16 +62,16 @@ mod version_type { #[test] fn sorts_any_req() { assert_eq!( - VersionType::from_str("^1 || ^2 || ^3").unwrap(), - VersionType::ReqAny(vec![ + UnresolvedVersionSpec::from_str("^1 || ^2 || ^3").unwrap(), + UnresolvedVersionSpec::ReqAny(vec![ VersionReq::parse("^3").unwrap(), VersionReq::parse("^2").unwrap(), VersionReq::parse("^1").unwrap(), ]) ); assert_eq!( - VersionType::from_str("^1.1 || ^1.10 || ^1.10.1 || ^1.2").unwrap(), - VersionType::ReqAny(vec![ + UnresolvedVersionSpec::from_str("^1.1 || ^1.10 || ^1.10.1 || ^1.2").unwrap(), + UnresolvedVersionSpec::ReqAny(vec![ VersionReq::parse("^1.10.1").unwrap(), VersionReq::parse("^1.10").unwrap(), VersionReq::parse("^1.2").unwrap(), @@ -84,8 +84,8 @@ mod version_type { fn parses_version() { for req in ["1.2.3", "4.5.6", "7.8.9-alpha", "10.11.12+build"] { assert_eq!( - VersionType::from_str(req).unwrap(), - VersionType::Version(Version::parse(req).unwrap()) + UnresolvedVersionSpec::from_str(req).unwrap(), + UnresolvedVersionSpec::Version(Version::parse(req).unwrap()) ); } } @@ -93,24 +93,24 @@ mod version_type { #[test] fn parses_version_with_v() { assert_eq!( - VersionType::from_str("v1.2.3").unwrap(), - VersionType::Version(Version::parse("1.2.3").unwrap()) + UnresolvedVersionSpec::from_str("v1.2.3").unwrap(), + UnresolvedVersionSpec::Version(Version::parse("1.2.3").unwrap()) ); } #[test] fn no_patch_becomes_req() { assert_eq!( - VersionType::from_str("1.2").unwrap(), - VersionType::ReqAll(VersionReq::parse("~1.2").unwrap()) + UnresolvedVersionSpec::from_str("1.2").unwrap(), + UnresolvedVersionSpec::Req(VersionReq::parse("~1.2").unwrap()) ); } #[test] fn no_minor_becomes_req() { assert_eq!( - VersionType::from_str("1").unwrap(), - VersionType::ReqAll(VersionReq::parse("~1").unwrap()) + UnresolvedVersionSpec::from_str("1").unwrap(), + UnresolvedVersionSpec::Req(VersionReq::parse("~1").unwrap()) ); } } diff --git a/crates/pdk-test-utils/src/lib.rs b/crates/pdk-test-utils/src/lib.rs index 4eeeb3945..4598b6254 100644 --- a/crates/pdk-test-utils/src/lib.rs +++ b/crates/pdk-test-utils/src/lib.rs @@ -4,8 +4,8 @@ mod wrapper; pub use macros::*; pub use proto_core as core; pub use proto_core::{ - AliasOrVersion, Id, ProtoEnvironment, Tool, ToolManifest, ToolsConfig, UserConfig, Version, - VersionReq, VersionType, + Id, ProtoEnvironment, Tool, ToolManifest, ToolsConfig, UnresolvedVersionSpec, UserConfig, + Version, VersionReq, VersionSpec, }; pub use proto_pdk_api::*; pub use wrapper::WasmTestWrapper;