fune/third_party/rust/semver/tests/node/mod.rs
Mike Hommey 7ce663eb08 Bug 1772048 - Update rustc_version and semver crates. r=emilio,webdriver-reviewers,kinetik,whimboo
semver 1.0 doesn't and won't support Clone on semver::Error[1], so we
convert the mozversion error type to store the string version of the
error, which is an incompatible change requiring a version bump on the
crate.

1. https://github.com/dtolnay/semver/pull/280

Differential Revision: https://phabricator.services.mozilla.com/D147825
2022-06-07 10:01:32 +00:00

43 lines
1.2 KiB
Rust

#![cfg(test_node_semver)]
use semver::Version;
use std::fmt::{self, Display};
use std::process::Command;
#[derive(Default, Eq, PartialEq, Hash, Debug)]
pub(super) struct VersionReq(semver::VersionReq);
impl VersionReq {
pub(super) const STAR: Self = VersionReq(semver::VersionReq::STAR);
pub(super) fn matches(&self, version: &Version) -> bool {
let out = Command::new("node")
.arg("-e")
.arg(format!(
"console.log(require('semver').satisfies('{}', '{}'))",
version,
self.to_string().replace(',', ""),
))
.output()
.unwrap();
if out.stdout == b"true\n" {
true
} else if out.stdout == b"false\n" {
false
} else {
let s = String::from_utf8_lossy(&out.stdout) + String::from_utf8_lossy(&out.stderr);
panic!("unexpected output: {}", s);
}
}
}
impl Display for VersionReq {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, formatter)
}
}
#[cfg_attr(not(no_track_caller), track_caller)]
pub(super) fn req(text: &str) -> VersionReq {
VersionReq(crate::util::req(text))
}