Skip to content

Commit

Permalink
return an error when global command receives not installed version (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
TaKO8Ki authored Jul 30, 2021
1 parent b5b7a8a commit f2a4074
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/commands/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum FrumError {
HttpError(#[from] reqwest::Error),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("Requested version {version} is not currently installed")]
VersionNotFound { version: InputVersion },
}

pub struct Global {
Expand All @@ -23,8 +25,17 @@ impl crate::command::Command for Global {
debug!("Use {} as the default version", &self.version);
let version = match self.version.clone() {
InputVersion::Full(Version::Semver(v)) => Version::Semver(v),
_ => return Ok(()),
version => return Err(FrumError::VersionNotFound { version }),
};
if !&config
.versions_dir()
.join(self.version.to_string())
.exists()
{
return Err(FrumError::VersionNotFound {
version: self.version.clone(),
});
}
create_alias(&config, "default", &version).map_err(FrumError::IoError)?;
Ok(())
}
Expand All @@ -41,7 +52,7 @@ mod tests {
use tempfile::tempdir;

#[test]
fn test_global_specified_version() {
fn test_global_specified_version_success() {
let config = FrumConfig {
base_dir: Some(tempdir().unwrap().path().to_path_buf()),
frum_path: Some(std::env::temp_dir().join(format!(
Expand All @@ -67,4 +78,38 @@ mod tests {
.join("ruby")
.exists());
}

#[test]
fn test_global_specified_version_failure() {
let config = FrumConfig {
base_dir: Some(tempdir().unwrap().path().to_path_buf()),
frum_path: Some(std::env::temp_dir().join(format!(
"frum_{}_{}",
std::process::id(),
chrono::Utc::now().timestamp_millis(),
))),
..Default::default()
};
let dir_path = config.versions_dir().join("2.6.4").join("bin");
std::fs::create_dir_all(&dir_path).unwrap();
File::create(dir_path.join("ruby")).unwrap();

let result = Global {
version: InputVersion::Full(Version::Semver(semver::Version::parse("2.7.0").unwrap())),
}
.apply(&config);
match result {
Ok(()) => assert!(false, "global must return an error"),
Err(e) => assert_eq!(
e.to_string(),
"Requested version 2.7.0 is not currently installed"
),
}

assert!(!config
.default_version_dir()
.join("bin")
.join("ruby")
.exists());
}
}

0 comments on commit f2a4074

Please sign in to comment.