Refactor: Create parameter-ised test_has_release_artifact

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2024-05-30 00:50:10 +10:00
parent 345255a65f
commit 4ffaf8c805
No known key found for this signature in database
GPG key ID: 76D1E687CA3C4928

View file

@ -326,11 +326,6 @@ mod test {
} }
); );
} }
#[tokio::test]
async fn test_gh_api_client_cargo_audit_v_0_17_6() {
test_specific_release(&RELEASE, ARTIFACTS).await
}
} }
fn try_extract_artifact_from_str(s: &str) -> Option<GhReleaseArtifact> { fn try_extract_artifact_from_str(s: &str) -> Option<GhReleaseArtifact> {
@ -490,81 +485,69 @@ mod test {
} }
} }
async fn test_specific_release(release: &GhRelease, artifacts: &[&str]) { #[tokio::test]
async fn test_has_release_artifact() {
const RELEASES: [(GhRelease, &[&str]); 2] = [
(
cargo_audit_v_0_17_6::RELEASE,
cargo_audit_v_0_17_6::ARTIFACTS,
),
(
cargo_audit_v_0_17_6::RELEASE,
cargo_audit_v_0_17_6::ARTIFACTS,
),
];
const NON_EXISTENT_RELEASES: [GhRelease; 1] = [GhRelease {
repo: GhRepo {
owner: CompactString::new_inline("cargo-bins"),
repo: CompactString::new_inline("cargo-binstall"),
},
// We are currently at v0.20.1 and we would never release
// anything older than v0.20.1
tag: CompactString::new_inline("v0.18.2"),
}];
init_logger(); init_logger();
for client in create_client().await { for client in create_client().await {
eprintln!("In client {client:?}"); eprintln!("In client {client:?}");
for (release, artifacts) in RELEASES {
for artifact_name in artifacts { for artifact_name in artifacts {
let res = client client
.has_release_artifact(GhReleaseArtifact { .has_release_artifact(GhReleaseArtifact {
release: release.clone(), release: release.clone(),
artifact_name: artifact_name.to_compact_string(), artifact_name: artifact_name.to_compact_string(),
}) })
.await; .await
.unwrap()
assert!( .unwrap();
matches!(res, Ok(Some(_)) | Err(GhApiError::RateLimit { .. })),
"for '{artifact_name}': answer is {:#?}",
res
);
} }
let res = client assert_eq!(
client
.has_release_artifact(GhReleaseArtifact { .has_release_artifact(GhReleaseArtifact {
release: release.clone(), release: release.clone(),
artifact_name: "123z".to_compact_string(), artifact_name: "123z".to_compact_string(),
}) })
.await; .await
.unwrap(),
assert!( None
matches!(res, Ok(None) | Err(GhApiError::RateLimit { .. })),
"res = {:#?}",
res
); );
} }
}
#[tokio::test] for release in NON_EXISTENT_RELEASES {
async fn test_gh_api_client_cargo_binstall_v0_20_1() { assert_eq!(
test_specific_release( client
&cargo_binstall_v0_20_1::RELEASE,
cargo_binstall_v0_20_1::ARTIFACTS,
)
.await
}
#[tokio::test]
async fn test_gh_api_client_cargo_binstall_no_such_release() {
init_logger();
for client in create_client().await {
let release = GhRelease {
repo: GhRepo {
owner: "cargo-bins".to_compact_string(),
repo: "cargo-binstall".to_compact_string(),
},
// We are currently at v0.20.1 and we would never release
// anything older than v0.20.1
tag: "v0.18.2".to_compact_string(),
};
let err = client
.has_release_artifact(GhReleaseArtifact { .has_release_artifact(GhReleaseArtifact {
release, release,
artifact_name: "1234".to_compact_string(), artifact_name: "1234".to_compact_string(),
}) })
.await; .await
.unwrap(),
assert!( None
matches!(
err,
Ok(None) | Err(GhApiError::NotFound | GhApiError::RateLimit { .. })
),
"err = {:#?}",
err
); );
} }
} }
}
} }