Skip to content

Commit 62edcd5

Browse files
committed
test: add tests for extension matchers
1 parent a04a087 commit 62edcd5

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

postgresql_extensions/src/matcher.rs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,32 @@ use std::collections::HashMap;
55
use std::env::consts;
66
use url::Url;
77

8-
/// .zip asset matcher that matches the asset name to the postgresql major version, target triple or
9-
/// OS/CPU architecture.
8+
/// .tar.gz asset matcher that matches the asset name to the postgresql major version, target triple
9+
/// or OS/CPU architecture.
1010
///
1111
/// # Errors
1212
/// * If the asset matcher fails.
1313
#[allow(clippy::case_sensitive_file_extension_comparisons)]
14-
pub fn zip_matcher(url: &str, name: &str, version: &Version) -> Result<bool> {
14+
pub fn tar_gz_matcher(url: &str, name: &str, version: &Version) -> Result<bool> {
1515
if !matcher(url, name, version)? {
1616
return Ok(false);
1717
}
1818

19-
Ok(name.ends_with(".zip"))
19+
Ok(name.ends_with(".tar.gz"))
2020
}
2121

22-
/// .tar.gz asset matcher that matches the asset name to the postgresql major version, target triple
23-
/// or OS/CPU architecture.
22+
/// .zip asset matcher that matches the asset name to the postgresql major version, target triple or
23+
/// OS/CPU architecture.
2424
///
2525
/// # Errors
2626
/// * If the asset matcher fails.
2727
#[allow(clippy::case_sensitive_file_extension_comparisons)]
28-
pub fn tar_gz_matcher(url: &str, name: &str, version: &Version) -> Result<bool> {
28+
pub fn zip_matcher(url: &str, name: &str, version: &Version) -> Result<bool> {
2929
if !matcher(url, name, version)? {
3030
return Ok(false);
3131
}
3232

33-
Ok(name.ends_with(".tar.gz"))
33+
Ok(name.ends_with(".zip"))
3434
}
3535

3636
/// Default asset matcher that matches the asset name to the postgresql major version, target triple
@@ -136,22 +136,66 @@ mod tests {
136136
}
137137

138138
#[test]
139-
fn test_asset_match_success() -> Result<()> {
139+
fn test_tar_gz_matcher() -> Result<()> {
140+
let postgresql_major_version = 16;
141+
let url = format!("https://foo?postgresql_version={postgresql_major_version}.3");
142+
let version = Version::parse("1.2.3")?;
143+
let target = target_triple::TARGET;
144+
145+
let valid_name = format!("postgresql-pg{postgresql_major_version}-{target}.tar.gz");
146+
let invalid_name = format!("postgresql-pg{postgresql_major_version}-{target}.zip");
147+
assert!(
148+
tar_gz_matcher(url.as_str(), valid_name.as_str(), &version)?,
149+
"{}",
150+
valid_name
151+
);
152+
assert!(
153+
!tar_gz_matcher(url.as_str(), invalid_name.as_str(), &version)?,
154+
"{}",
155+
invalid_name
156+
);
157+
Ok(())
158+
}
159+
160+
#[test]
161+
fn test_zip_matcher() -> Result<()> {
162+
let postgresql_major_version = 16;
163+
let url = format!("https://foo?postgresql_version={postgresql_major_version}.3");
164+
let version = Version::parse("1.2.3")?;
165+
let target = target_triple::TARGET;
166+
167+
let valid_name = format!("postgresql-pg{postgresql_major_version}-{target}.zip");
168+
let invalid_name = format!("postgresql-pg{postgresql_major_version}-{target}.tar.gz");
169+
assert!(
170+
zip_matcher(url.as_str(), valid_name.as_str(), &version)?,
171+
"{}",
172+
valid_name
173+
);
174+
assert!(
175+
!zip_matcher(url.as_str(), invalid_name.as_str(), &version)?,
176+
"{}",
177+
invalid_name
178+
);
179+
Ok(())
180+
}
181+
182+
#[test]
183+
fn test_matcher_success() -> Result<()> {
140184
let postgresql_major_version = 16;
141185
let url = format!("https://foo?postgresql_version={postgresql_major_version}.3");
142186
let version = Version::parse("1.2.3")?;
143187
let target = target_triple::TARGET;
144188
let os = consts::OS;
145189
let arch = consts::ARCH;
146190
let names = vec![
147-
format!("postgresql-pg16-{target}.zip"),
148-
format!("postgresql-pg16-{os}-{arch}.zip"),
149-
format!("postgresql-pg16-{target}.tar.gz"),
150-
format!("postgresql-pg16-{os}-{arch}.tar.gz"),
151-
format!("foo.{target}.pg16.tar.gz"),
152-
format!("foo.{os}.{arch}.pg16.tar.gz"),
153-
format!("foo-{arch}-{os}-pg16.tar.gz"),
154-
format!("foo_{arch}_{os}_pg16.tar.gz"),
191+
format!("postgresql-pg{postgresql_major_version}-{target}.zip"),
192+
format!("postgresql-pg{postgresql_major_version}-{os}-{arch}.zip"),
193+
format!("postgresql-pg{postgresql_major_version}-{target}.tar.gz"),
194+
format!("postgresql-pg{postgresql_major_version}-{os}-{arch}.tar.gz"),
195+
format!("foo.{target}.pg{postgresql_major_version}.tar.gz"),
196+
format!("foo.{os}.{arch}.pg{postgresql_major_version}.tar.gz"),
197+
format!("foo-{arch}-{os}-pg{postgresql_major_version}.tar.gz"),
198+
format!("foo_{arch}_{os}_pg{postgresql_major_version}.tar.gz"),
155199
];
156200

157201
for name in names {
@@ -161,7 +205,7 @@ mod tests {
161205
}
162206

163207
#[test]
164-
fn test_asset_match_errors() -> Result<()> {
208+
fn test_matcher_errors() -> Result<()> {
165209
let postgresql_major_version = 16;
166210
let url = format!("https://foo?postgresql_version={postgresql_major_version}.3");
167211
let version = Version::parse("1.2.3")?;

0 commit comments

Comments
 (0)