prefetch-npm-deps: move tests to separate file

This commit is contained in:
Winter 2022-11-13 21:10:27 -05:00
parent 3d5f77f496
commit b117b359db
2 changed files with 92 additions and 92 deletions

View file

@ -14,6 +14,8 @@ use tempfile::tempdir;
use url::Url;
mod cacache;
#[cfg(test)]
mod tests;
#[derive(Deserialize)]
struct PackageLock {
@ -343,95 +345,3 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
#[cfg(test)]
mod tests {
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(())
}
}

View file

@ -0,0 +1,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(())
}