From e88484d5674b7c038346ff1c89089e535d2e2d6d Mon Sep 17 00:00:00 2001 From: Andre Ribeiro <33375224+Andree37@users.noreply.github.com> Date: Tue, 10 Jan 2023 22:00:09 +0000 Subject: [PATCH] fix(nodejs): apply `style` even if node version is unavailable (#4713) * correct nodejs color * Update src/modules/nodejs.rs Co-authored-by: David Knaack * Update src/modules/nodejs.rs Co-authored-by: David Knaack * fix: removed unecessary unwraps * test: no node installed * test: no node installed Co-authored-by: David Knaack --- src/modules/nodejs.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/modules/nodejs.rs b/src/modules/nodejs.rs index 635cfb3f..0a4e3c5b 100644 --- a/src/modules/nodejs.rs +++ b/src/modules/nodejs.rs @@ -45,7 +45,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { "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 { Some(raw_version.to_string()) } -fn check_engines_version(nodejs_version: &str, engines_version: Option) -> 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) -> 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() + } }