Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nodejs): Add expected_version variable #5081

Merged
merged 10 commits into from
Jun 1, 2023
11 changes: 6 additions & 5 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2829,11 +2829,12 @@ By default the module will be shown if any of the following conditions are met:

### Variables

| Variable | Example | Description |
| -------- | ---------- | ------------------------------------ |
| version | `v13.12.0` | The version of `node` |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |
| Variable | Example | Description |
| ---------------- | ---------- | ------------------------------------------------------------------------ |
| version | `v13.12.0` | The version of `node` |
| expected_version | `>=12.0.0` | Optional `node` version as set in the engines property of `package.json` |
marcybelardo marked this conversation as resolved.
Show resolved Hide resolved
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |

*: This variable can only be used as a part of a style string

Expand Down
88 changes: 88 additions & 0 deletions src/modules/nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
_ => None,
})
.map(|variable| match variable {
"expected_version" => {
let version = nodejs_version.as_deref();
let expected_version = get_engines_version(context);
let versions_in_range =
check_engines_version(version, get_engines_version(context));

match (expected_version, versions_in_range) {
(Some(ver), false) => Some(Ok(ver)),
_ => None,
}
}
_ => None,
})
marcybelardo marked this conversation as resolved.
Show resolved Hide resolved
.parse(None, Some(context))
});

Expand Down Expand Up @@ -269,6 +283,80 @@ mod tests {
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn show_expected_version_when_engines_does_not_match() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let mut file = File::create(dir.path().join("package.json"))?;
file.write_all(
b"{
\"engines\":{
\"node\":\"<=11.0.0\"
}
}",
)?;
file.sync_all()?;

let actual = ModuleRenderer::new("nodejs")
.path(dir.path())
.config(toml::toml! {
[nodejs]
format = "via [$symbol($version )($expected_version )]($style)"
})
.collect();
let expected = Some(format!(
"via {}",
Color::Red.bold().paint(" v12.0.0 <=11.0.0 ")
));

assert_eq!(expected, actual);
dir.close()
}

#[test]
fn do_not_show_expected_version_if_engines_match() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let mut file = File::create(dir.path().join("package.json"))?;
file.write_all(
b"{
\"engines\":{
\"node\":\">=12.0.0\"
}
}",
)?;
file.sync_all()?;

let actual = ModuleRenderer::new("nodejs")
.path(dir.path())
.config(toml::toml! [
[nodejs]
format = "via [$symbol($version )($expected_version )]($style)"
])
.collect();
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));

assert_eq!(expected, actual);
dir.close()
}

#[test]
fn do_not_show_expected_version_if_no_set_engines_version() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("package.json"))?.sync_all()?;

let actual = ModuleRenderer::new("nodejs")
.path(dir.path())
.config(toml::toml! {
[nodejs]
format = "via [$symbol($version )($expected_version )]($style)"
})
.collect();
let expected = Some(format!("via {}", Color::Green.bold().paint(" v12.0.0 ")));

assert_eq!(expected, actual);
dir.close()
}

#[test]
fn no_node_installed() -> io::Result<()> {
let dir = tempfile::tempdir()?;
Expand Down