Browse Source

fix: --install-if-missing when using version alias (#484)

remotes/origin/add-with-shims
Alex Munoz 4 years ago committed by GitHub
parent
commit
79ded8f6be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 64
      src/commands/use.rs
  2. 18
      tests/feature_tests/mod.rs
  3. 11
      tests/feature_tests/snapshots/e2e__feature_tests__use_alias_install_if_missing__Bash.snap
  4. 8
      tests/feature_tests/snapshots/e2e__feature_tests__use_alias_install_if_missing__Fish.snap
  5. 9
      tests/feature_tests/snapshots/e2e__feature_tests__use_alias_install_if_missing__PowerShell.snap
  6. 9
      tests/feature_tests/snapshots/e2e__feature_tests__use_alias_install_if_missing__Zsh.snap
  7. 10
      tests/feature_tests/snapshots/e2e__feature_tests__use_alias_not_installed__Bash.snap
  8. 7
      tests/feature_tests/snapshots/e2e__feature_tests__use_alias_not_installed__Fish.snap
  9. 8
      tests/feature_tests/snapshots/e2e__feature_tests__use_alias_not_installed__PowerShell.snap
  10. 8
      tests/feature_tests/snapshots/e2e__feature_tests__use_alias_not_installed__Zsh.snap

64
src/commands/use.rs

@ -42,14 +42,13 @@ impl Command for Use { @@ -42,14 +42,13 @@ impl Command for Use {
system_version::path()
} else if let Some(alias_name) = requested_version.alias_name() {
let alias_path = config.aliases_dir().join(&alias_name);
ensure!(
alias_path.exists(),
CantFindVersion {
version: requested_version
}
);
outln!(config#Info, "Using Node for alias {}", alias_name.cyan());
alias_path
if alias_path.exists() {
outln!(config#Info, "Using Node for alias {}", alias_name.cyan());
alias_path
} else {
install_new_version(requested_version, config, self.install_if_missing)?;
return Ok(());
}
} else {
let current_version = requested_version.to_version(&all_versions, &config);
match current_version {
@ -61,26 +60,7 @@ impl Command for Use { @@ -61,26 +60,7 @@ impl Command for Use {
.join("installation")
}
None => {
ensure!(
self.install_if_missing || should_install_interactively(&requested_version),
CantFindVersion {
version: requested_version
}
);
Install {
version: Some(requested_version.clone()),
..Default::default()
}
.apply(config)
.context(InstallError)?;
Self {
version: Some(UserVersionReader::Direct(requested_version)),
install_if_missing: self.install_if_missing,
}
.apply(config)?;
install_new_version(requested_version, config, self.install_if_missing)?;
return Ok(());
}
}
@ -92,6 +72,34 @@ impl Command for Use { @@ -92,6 +72,34 @@ impl Command for Use {
}
}
fn install_new_version(
requested_version: UserVersion,
config: &FnmConfig,
install_if_missing: bool,
) -> Result<(), Error> {
ensure!(
install_if_missing || should_install_interactively(&requested_version),
CantFindVersion {
version: requested_version
}
);
Install {
version: Some(requested_version.clone()),
..Default::default()
}
.apply(config)
.context(InstallError)?;
Use {
version: Some(UserVersionReader::Direct(requested_version)),
install_if_missing: true,
}
.apply(config)?;
return Ok(());
}
/// Tries to delete `from`, and then tries to symlink `from` to `to` anyway.
/// If the symlinking fails, it will return the errors in the following order:
/// * The deletion error (if exists)

18
tests/feature_tests/mod.rs

@ -200,3 +200,21 @@ mod matching_dotfiles { @@ -200,3 +200,21 @@ mod matching_dotfiles {
.then(test_node_version("v11.10.0"))
});
}
mod use_alias_install_if_missing {
test_shell!(Bash, Zsh, Fish, PowerShell; {
EvalFnmEnv::default()
.then(WriteFile::new(".node-version", "lts/*"))
.then(Call::new("fnm", vec!["use", "--install-if-missing"]))
.then(OutputContains::new(Call::new("fnm", vec!["ls"]), "lts-latest"))
});
}
mod use_alias_not_installed {
test_shell!(Bash, Zsh, Fish, PowerShell; {
EvalFnmEnv::default()
.log_level(Some("error"))
.then(WriteFile::new(".node-version", "lts/*"))
.then(OutputContains::new(IgnoreErrors::new(GetStderr::new(Call::new("fnm", vec!["use"]))), "Requested version lts-latest is not currently installed"))
});
}

11
tests/feature_tests/snapshots/e2e__feature_tests__use_alias_install_if_missing__Bash.snap

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
---
source: tests/feature_tests/mod.rs
expression: "&source.trim()"
---
set -e
shopt -s expand_aliases
eval "$(fnm env)"
echo 'lts/*' > .node-version
fnm use --install-if-missing
fnm ls | grep lts-latest

8
tests/feature_tests/snapshots/e2e__feature_tests__use_alias_install_if_missing__Fish.snap

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
---
source: tests/feature_tests/mod.rs
expression: "&source.trim()"
---
fnm env | source
echo 'lts/*' > .node-version
fnm use --install-if-missing
fnm ls | grep lts-latest

9
tests/feature_tests/snapshots/e2e__feature_tests__use_alias_install_if_missing__PowerShell.snap

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
---
source: tests/feature_tests/mod.rs
expression: "&source.trim()"
---
$ErrorActionPreference = "Stop"
fnm env | Out-String | Invoke-Expression
echo 'lts/*' > '.node-version'
fnm use --install-if-missing
$($__out__ = $(fnm ls | Select-String 'lts-latest'); echo $__out__; if ($__out__ -eq $null){ exit 1 } else { $__out__ })

9
tests/feature_tests/snapshots/e2e__feature_tests__use_alias_install_if_missing__Zsh.snap

@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
---
source: tests/feature_tests/mod.rs
expression: "&source.trim()"
---
set -e
eval "$(fnm env)"
echo 'lts/*' > .node-version
fnm use --install-if-missing
fnm ls | grep lts-latest

10
tests/feature_tests/snapshots/e2e__feature_tests__use_alias_not_installed__Bash.snap

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
---
source: tests/feature_tests/mod.rs
expression: "&source.trim()"
---
set -e
shopt -s expand_aliases
eval "$(fnm --log-level='error' env)"
echo 'lts/*' > .node-version
fnm use 2>&1 | grep 'Requested version lts-latest is not currently installed'

7
tests/feature_tests/snapshots/e2e__feature_tests__use_alias_not_installed__Fish.snap

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
---
source: tests/feature_tests/mod.rs
expression: "&source.trim()"
---
fnm --log-level='error' env | source
echo 'lts/*' > .node-version
fnm use 2>&1 | grep 'Requested version lts-latest is not currently installed'

8
tests/feature_tests/snapshots/e2e__feature_tests__use_alias_not_installed__PowerShell.snap

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
---
source: tests/feature_tests/mod.rs
expression: "&source.trim()"
---
$ErrorActionPreference = "Stop"
fnm --log-level='error' env | Out-String | Invoke-Expression
echo 'lts/*' > '.node-version'
$($__out__ = $($($_tmp_err_action = $ErrorActionPreference;$ErrorActionPreference = "Continue";fnm use 2>&1;$ErrorActionPreference = $_tmp_err_action) | Select-String 'Requested version lts-latest is not currently installed'); echo $__out__; if ($__out__ -eq $null){ exit 1 } else { $__out__ })

8
tests/feature_tests/snapshots/e2e__feature_tests__use_alias_not_installed__Zsh.snap

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
---
source: tests/feature_tests/mod.rs
expression: "&source.trim()"
---
set -e
eval "$(fnm --log-level='error' env)"
echo 'lts/*' > .node-version
fnm use 2>&1 | grep 'Requested version lts-latest is not currently installed'
Loading…
Cancel
Save