From a01e60265e67545bf2d1378e8fb8f26415b74e89 Mon Sep 17 00:00:00 2001 From: Chuck Pierce Date: Tue, 20 Apr 2021 17:21:44 -0700 Subject: [PATCH] Switch Node filename builder to use Version object --- crates/volta-core/src/tool/node/fetch.rs | 23 ++++++++++------------- crates/volta-core/src/tool/node/mod.rs | 8 ++++---- tests/acceptance/corrupted_download.rs | 5 +++-- tests/acceptance/support/sandbox.rs | 3 ++- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/crates/volta-core/src/tool/node/fetch.rs b/crates/volta-core/src/tool/node/fetch.rs index 9630bc7f6..25fe013f4 100644 --- a/crates/volta-core/src/tool/node/fetch.rs +++ b/crates/volta-core/src/tool/node/fetch.rs @@ -30,7 +30,7 @@ cfg_if! { } } -fn npm_manifest_path(version: &str) -> PathBuf { +fn npm_manifest_path(version: &Version) -> PathBuf { let mut manifest = PathBuf::from(Node::archive_basename(version)); #[cfg(unix)] @@ -46,7 +46,7 @@ fn npm_manifest_path(version: &str) -> PathBuf { pub fn fetch(version: &Version, hooks: Option<&ToolHooks>) -> Fallible { let home = volta_home()?; let node_dir = home.node_inventory_dir(); - let cache_file = node_dir.join(Node::archive_filename(&version.to_string())); + let cache_file = node_dir.join(Node::archive_filename(version)); let (archive, staging) = match load_cached_distro(&cache_file) { Some(archive) => { @@ -107,7 +107,7 @@ fn unpack_archive(archive: Box, version: &Version) -> Fallible, version: &Version) -> Fallible Option> { /// Determine the remote URL to download from, using the hooks if available fn determine_remote_url(version: &Version, hooks: Option<&ToolHooks>) -> Fallible { - let version_str = version.to_string(); - let distro_file_name = Node::archive_filename(&version_str); + let distro_file_name = Node::archive_filename(version); match hooks { Some(&ToolHooks { distro: Some(ref hook), diff --git a/crates/volta-core/src/tool/node/mod.rs b/crates/volta-core/src/tool/node/mod.rs index 173cd611f..d93788bf8 100644 --- a/crates/volta-core/src/tool/node/mod.rs +++ b/crates/volta-core/src/tool/node/mod.rs @@ -129,11 +129,11 @@ impl Node { Node { version } } - pub fn archive_basename(version: &str) -> String { + pub fn archive_basename(version: &Version) -> String { format!("node-v{}-{}-{}", version, NODE_DISTRO_OS, NODE_DISTRO_ARCH) } - pub fn archive_filename(version: &str) -> String { + pub fn archive_filename(version: &Version) -> String { format!( "{}.{}", Node::archive_basename(version), @@ -242,7 +242,7 @@ mod tests { #[test] fn test_node_archive_basename() { assert_eq!( - Node::archive_basename("1.2.3"), + Node::archive_basename(&Version::new(1, 2, 3)), format!("node-v1.2.3-{}-{}", NODE_DISTRO_OS, NODE_DISTRO_ARCH) ); } @@ -250,7 +250,7 @@ mod tests { #[test] fn test_node_archive_filename() { assert_eq!( - Node::archive_filename("1.2.3"), + Node::archive_filename(&Version::new(1, 2, 3)), format!( "node-v1.2.3-{}-{}.{}", NODE_DISTRO_OS, NODE_DISTRO_ARCH, NODE_DISTRO_EXTENSION diff --git a/tests/acceptance/corrupted_download.rs b/tests/acceptance/corrupted_download.rs index 9b1aaa147..b9d442ed5 100644 --- a/tests/acceptance/corrupted_download.rs +++ b/tests/acceptance/corrupted_download.rs @@ -1,6 +1,7 @@ use crate::support::sandbox::{sandbox, DistroMetadata, NodeFixture, YarnFixture}; use hamcrest2::assert_that; use hamcrest2::prelude::*; +use semver::Version; use test_support::matchers::execs; use volta_core::error::ExitCode; @@ -58,7 +59,7 @@ fn install_corrupted_node_leaves_inventory_unchanged() { execs().with_status(ExitCode::UnknownError as i32) ); - assert!(!s.node_inventory_archive_exists("0.0.1")); + assert!(!s.node_inventory_archive_exists(&Version::new(0, 0, 1))); } #[test] @@ -73,7 +74,7 @@ fn install_valid_node_saves_to_inventory() { execs().with_status(ExitCode::Success as i32) ); - assert!(s.node_inventory_archive_exists("10.99.1040")); + assert!(s.node_inventory_archive_exists(&Version::new(10, 99, 1040))); } #[test] diff --git a/tests/acceptance/support/sandbox.rs b/tests/acceptance/support/sandbox.rs index bc704cba3..7baf135bf 100644 --- a/tests/acceptance/support/sandbox.rs +++ b/tests/acceptance/support/sandbox.rs @@ -7,6 +7,7 @@ use std::time::{Duration, SystemTime}; use hyperx::header::HttpDate; use mockito::{self, mock, Matcher}; +use semver::Version; use test_support::{self, ok_or_panic, paths, paths::PathExt, process::ProcessBuilder}; use volta_core::fs::symlink_file; use volta_core::tool::{Node, Yarn, NODE_DISTRO_ARCH, NODE_DISTRO_EXTENSION, NODE_DISTRO_OS}; @@ -631,7 +632,7 @@ impl Sandbox { // check that files in the sandbox exist - pub fn node_inventory_archive_exists(&self, version: &str) -> bool { + pub fn node_inventory_archive_exists(&self, version: &Version) -> bool { node_inventory_dir() .join(Node::archive_filename(version)) .exists()