Skip to content

Commit

Permalink
fix(nodejs): apply style even if node version is unavailable (#4713)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Andree37 and davidkna committed Jan 10, 2023
1 parent af5e506 commit e88484d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/modules/nodejs.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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()
}
}

0 comments on commit e88484d

Please sign in to comment.