Skip to content

Commit a01e602

Browse files
committed
Switch Node filename builder to use Version object
1 parent f6f6b59 commit a01e602

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

crates/volta-core/src/tool/node/fetch.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ cfg_if! {
3030
}
3131
}
3232

33-
fn npm_manifest_path(version: &str) -> PathBuf {
33+
fn npm_manifest_path(version: &Version) -> PathBuf {
3434
let mut manifest = PathBuf::from(Node::archive_basename(version));
3535

3636
#[cfg(unix)]
@@ -46,7 +46,7 @@ fn npm_manifest_path(version: &str) -> PathBuf {
4646
pub fn fetch(version: &Version, hooks: Option<&ToolHooks<Node>>) -> Fallible<NodeVersion> {
4747
let home = volta_home()?;
4848
let node_dir = home.node_inventory_dir();
49-
let cache_file = node_dir.join(Node::archive_filename(&version.to_string()));
49+
let cache_file = node_dir.join(Node::archive_filename(version));
5050

5151
let (archive, staging) = match load_cached_distro(&cache_file) {
5252
Some(archive) => {
@@ -107,22 +107,20 @@ fn unpack_archive(archive: Box<dyn Archive>, version: &Version) -> Fallible<Node
107107
})?;
108108

109109
// Save the npm version number in the npm version file for this distro
110-
let npm_package_json = temp.path().join(npm_manifest_path(&version_string));
110+
let npm_package_json = temp.path().join(npm_manifest_path(version));
111111
let npm = Manifest::version(&npm_package_json)?;
112112
save_default_npm_version(&version, &npm)?;
113113

114114
let dest = volta_home()?.node_image_dir(&version_string);
115115
ensure_containing_dir_exists(&dest)
116116
.with_context(|| ErrorKind::ContainingDirError { path: dest.clone() })?;
117117

118-
rename(
119-
temp.path().join(Node::archive_basename(&version_string)),
120-
&dest,
121-
)
122-
.with_context(|| ErrorKind::SetupToolImageError {
123-
tool: "Node".into(),
124-
version: version_string,
125-
dir: dest.clone(),
118+
rename(temp.path().join(Node::archive_basename(version)), &dest).with_context(|| {
119+
ErrorKind::SetupToolImageError {
120+
tool: "Node".into(),
121+
version: version_string,
122+
dir: dest.clone(),
123+
}
126124
})?;
127125

128126
progress.finish_and_clear();
@@ -151,8 +149,7 @@ fn load_cached_distro(file: &Path) -> Option<Box<dyn Archive>> {
151149

152150
/// Determine the remote URL to download from, using the hooks if available
153151
fn determine_remote_url(version: &Version, hooks: Option<&ToolHooks<Node>>) -> Fallible<String> {
154-
let version_str = version.to_string();
155-
let distro_file_name = Node::archive_filename(&version_str);
152+
let distro_file_name = Node::archive_filename(version);
156153
match hooks {
157154
Some(&ToolHooks {
158155
distro: Some(ref hook),

crates/volta-core/src/tool/node/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,11 @@ impl Node {
129129
Node { version }
130130
}
131131

132-
pub fn archive_basename(version: &str) -> String {
132+
pub fn archive_basename(version: &Version) -> String {
133133
format!("node-v{}-{}-{}", version, NODE_DISTRO_OS, NODE_DISTRO_ARCH)
134134
}
135135

136-
pub fn archive_filename(version: &str) -> String {
136+
pub fn archive_filename(version: &Version) -> String {
137137
format!(
138138
"{}.{}",
139139
Node::archive_basename(version),
@@ -242,15 +242,15 @@ mod tests {
242242
#[test]
243243
fn test_node_archive_basename() {
244244
assert_eq!(
245-
Node::archive_basename("1.2.3"),
245+
Node::archive_basename(&Version::new(1, 2, 3)),
246246
format!("node-v1.2.3-{}-{}", NODE_DISTRO_OS, NODE_DISTRO_ARCH)
247247
);
248248
}
249249

250250
#[test]
251251
fn test_node_archive_filename() {
252252
assert_eq!(
253-
Node::archive_filename("1.2.3"),
253+
Node::archive_filename(&Version::new(1, 2, 3)),
254254
format!(
255255
"node-v1.2.3-{}-{}.{}",
256256
NODE_DISTRO_OS, NODE_DISTRO_ARCH, NODE_DISTRO_EXTENSION

tests/acceptance/corrupted_download.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::support::sandbox::{sandbox, DistroMetadata, NodeFixture, YarnFixture};
22
use hamcrest2::assert_that;
33
use hamcrest2::prelude::*;
4+
use semver::Version;
45
use test_support::matchers::execs;
56

67
use volta_core::error::ExitCode;
@@ -58,7 +59,7 @@ fn install_corrupted_node_leaves_inventory_unchanged() {
5859
execs().with_status(ExitCode::UnknownError as i32)
5960
);
6061

61-
assert!(!s.node_inventory_archive_exists("0.0.1"));
62+
assert!(!s.node_inventory_archive_exists(&Version::new(0, 0, 1)));
6263
}
6364

6465
#[test]
@@ -73,7 +74,7 @@ fn install_valid_node_saves_to_inventory() {
7374
execs().with_status(ExitCode::Success as i32)
7475
);
7576

76-
assert!(s.node_inventory_archive_exists("10.99.1040"));
77+
assert!(s.node_inventory_archive_exists(&Version::new(10, 99, 1040)));
7778
}
7879

7980
#[test]

tests/acceptance/support/sandbox.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::time::{Duration, SystemTime};
77

88
use hyperx::header::HttpDate;
99
use mockito::{self, mock, Matcher};
10+
use semver::Version;
1011
use test_support::{self, ok_or_panic, paths, paths::PathExt, process::ProcessBuilder};
1112
use volta_core::fs::symlink_file;
1213
use volta_core::tool::{Node, Yarn, NODE_DISTRO_ARCH, NODE_DISTRO_EXTENSION, NODE_DISTRO_OS};
@@ -631,7 +632,7 @@ impl Sandbox {
631632

632633
// check that files in the sandbox exist
633634

634-
pub fn node_inventory_archive_exists(&self, version: &str) -> bool {
635+
pub fn node_inventory_archive_exists(&self, version: &Version) -> bool {
635636
node_inventory_dir()
636637
.join(Node::archive_filename(version))
637638
.exists()

0 commit comments

Comments
 (0)