1 #![cfg(test_node_semver)]
2 
3 use semver::Version;
4 use std::fmt::{self, Display};
5 use std::process::Command;
6 
7 #[derive(Default, Eq, PartialEq, Hash, Debug)]
8 pub(super) struct VersionReq(semver::VersionReq);
9 
10 impl VersionReq {
11     pub(super) const STAR: Self = VersionReq(semver::VersionReq::STAR);
12 
matches(&self, version: &Version) -> bool13     pub(super) fn matches(&self, version: &Version) -> bool {
14         let out = Command::new("node")
15             .arg("-e")
16             .arg(format!(
17                 "console.log(require('semver').satisfies('{}', '{}'))",
18                 version,
19                 self.to_string().replace(',', ""),
20             ))
21             .output()
22             .unwrap();
23         if out.stdout == b"true\n" {
24             true
25         } else if out.stdout == b"false\n" {
26             false
27         } else {
28             let s = String::from_utf8_lossy(&out.stdout) + String::from_utf8_lossy(&out.stderr);
29             panic!("unexpected output: {}", s);
30         }
31     }
32 }
33 
34 impl Display for VersionReq {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result35     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
36         Display::fmt(&self.0, formatter)
37     }
38 }
39 
40 #[cfg_attr(not(no_track_caller), track_caller)]
req(text: &str) -> VersionReq41 pub(super) fn req(text: &str) -> VersionReq {
42     VersionReq(crate::util::req(text))
43 }
44