summaryrefslogtreecommitdiffstats
path: root/vendor/semver/tests/node/mod.rs
blob: eb50673d5ff1b533de2dff3a4459ffb92d1fda94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#![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))
}