1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 07:46:28 +00:00

fix(nodejs): apply style even if node version is unavailable (#4713)

* correct nodejs color

* Update src/modules/nodejs.rs

Co-authored-by: David Knaack <davidkna@users.noreply.github.com>

* Update src/modules/nodejs.rs

Co-authored-by: David Knaack <davidkna@users.noreply.github.com>

* fix: removed unecessary unwraps

* test: no node installed

* test: no node installed

Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
Andre Ribeiro 2023-01-10 22:00:09 +00:00 committed by GitHub
parent af5e506192
commit e88484d567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -45,7 +45,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
"style" => {
let engines_version = get_engines_version(context);
let in_engines_range =
check_engines_version(nodejs_version.deref().as_ref()?, engines_version);
check_engines_version(nodejs_version.as_deref(), engines_version);
if in_engines_range {
Some(Ok(config.style))
} else {
@ -92,11 +92,12 @@ fn get_engines_version(context: &Context) -> Option<String> {
Some(raw_version.to_string())
}
fn check_engines_version(nodejs_version: &str, engines_version: Option<String>) -> bool {
if engines_version.is_none() {
return true;
}
let r = match VersionReq::parse(&engines_version.unwrap()) {
fn check_engines_version(nodejs_version: Option<&str>, engines_version: Option<String>) -> bool {
let (nodejs_version, engines_version) = match (nodejs_version, engines_version) {
(Some(nv), Some(ev)) => (nv, ev),
_ => return true,
};
let r = match VersionReq::parse(&engines_version) {
Ok(r) => r,
Err(_e) => return true,
};
@ -270,4 +271,16 @@ mod tests {
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn no_node_installed() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("index.js"))?.sync_all()?;
let actual = ModuleRenderer::new("nodejs")
.path(dir.path())
.cmd("node --version", None)
.collect();
let expected = Some(format!("via {}", Color::Green.bold().paint("")));
assert_eq!(expected, actual);
dir.close()
}
}