summary refs log tree commit diff
path: root/pkgs/build-support/node/fetch-npm-deps/src/tests.rs
blob: 99e091cbc2c2e383ed40548a08897ebcbaea206d (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use super::{
    get_hosted_git_url, get_ideal_hash, get_initial_url, to_new_packages, OldPackage, Package,
    UrlOrString,
};
use std::collections::HashMap;
use url::Url;

#[test]
fn hosted_git_urls() {
    for (input, expected) in [
        (
            "git+ssh://git@github.com/castlabs/electron-releases.git#fc5f78d046e8d7cdeb66345a2633c383ab41f525",
            Some("https://codeload.github.com/castlabs/electron-releases/tar.gz/fc5f78d046e8d7cdeb66345a2633c383ab41f525"),
        ),
        (
            "https://user@github.com/foo/bar#fix/bug",
            Some("https://codeload.github.com/foo/bar/tar.gz/fix/bug")
        ),
        (
            "https://github.com/eligrey/classList.js/archive/1.2.20180112.tar.gz",
            None
        ),
        (
            "git+ssh://bitbucket.org/foo/bar#branch",
            Some("https://bitbucket.org/foo/bar/get/branch.tar.gz")
        ),
        (
            "ssh://git@gitlab.com/foo/bar.git#fix/bug",
            Some("https://gitlab.com/foo/bar/repository/archive.tar.gz?ref=fix/bug")
        ),
        (
            "git+ssh://git.sr.ht/~foo/bar#branch",
            Some("https://git.sr.ht/~foo/bar/archive/branch.tar.gz")
        ),
    ] {
        assert_eq!(
            get_hosted_git_url(&Url::parse(input).unwrap()),
            expected.map(|u| Url::parse(u).unwrap())
        );
    }
}

#[test]
fn ideal_hashes() {
    for (input, expected) in [
        ("sha512-foo sha1-bar", Some("sha512-foo")),
        ("sha1-bar md5-foo", Some("sha1-bar")),
        ("sha1-bar", Some("sha1-bar")),
        ("sha512-foo", Some("sha512-foo")),
        ("foo-bar sha1-bar", Some("sha1-bar")),
        ("foo-bar baz-foo", None),
    ] {
        assert_eq!(get_ideal_hash(input).ok(), expected);
    }
}

#[test]
fn git_shorthand_v1() -> anyhow::Result<()> {
    let old = {
        let mut o = HashMap::new();
        o.insert(
            String::from("sqlite3"),
            OldPackage {
                version: UrlOrString::Url(
                    Url::parse(
                        "github:mapbox/node-sqlite3#593c9d498be2510d286349134537e3bf89401c4a",
                    )
                    .unwrap(),
                ),
                bundled: false,
                resolved: None,
                integrity: None,
                dependencies: None,
            },
        );
        o
    };

    let initial_url = get_initial_url()?;

    let new = to_new_packages(old, &initial_url)?;

    assert_eq!(new.len(), 1, "new packages map should contain 1 value");
    assert_eq!(new.into_values().next().unwrap(), Package {
        resolved: Some(UrlOrString::Url(Url::parse("git+ssh://git@github.com/mapbox/node-sqlite3.git#593c9d498be2510d286349134537e3bf89401c4a").unwrap())),
        integrity: None
    });

    Ok(())
}