diff --git a/src/commands/install.rs b/src/commands/install.rs index a3f9c74..93a3c2d 100644 --- a/src/commands/install.rs +++ b/src/commands/install.rs @@ -112,6 +112,11 @@ impl super::command::Command for Install { create_alias(&config, &alias_name, &version).context(IoError)?; } + if !config.default_version_dir().exists() { + debug!("Tagging {} as the default version", version.v_str().cyan()); + create_alias(&config, "default", &version).context(IoError)?; + } + Ok(()) } } @@ -151,3 +156,36 @@ pub enum Error { #[snafu(display("Too many versions provided. Please don't use --lts with a version string."))] TooManyVersionsProvided, } + +#[cfg(test)] +mod tests { + use super::super::command::Command; + use super::*; + use pretty_assertions::assert_eq; + use std::str::FromStr; + + #[test] + fn test_set_default_on_new_installation() { + let base_dir = tempfile::tempdir().unwrap(); + let config = FnmConfig::default().with_base_dir(Some(base_dir.path().to_path_buf())); + assert!(!config.default_version_dir().exists()); + + Install { + version: UserVersion::from_str("12.0.0").ok(), + lts: false, + } + .apply(&config) + .expect("Can't install"); + + assert!(config.default_version_dir().exists()); + assert_eq!( + config.default_version_dir().canonicalize().ok(), + config + .installations_dir() + .join("v12.0.0") + .join("installation") + .canonicalize() + .ok() + ); + } +} diff --git a/src/config.rs b/src/config.rs index 0e6fdb6..1a78d02 100644 --- a/src/config.rs +++ b/src/config.rs @@ -66,6 +66,12 @@ impl FnmConfig { pub fn aliases_dir(&self) -> std::path::PathBuf { ensure_exists_silently(self.base_dir_with_default().join("aliases")) } + + #[cfg(test)] + pub fn with_base_dir(mut self, base_dir: Option) -> Self { + self.base_dir = base_dir; + self + } } fn ensure_exists_silently>(path: T) -> T {