Skip to content

Commit

Permalink
added additional tests for the backward compatibile preferred protoco…
Browse files Browse the repository at this point in the history
…l selection and fixed incorrect implicit selection
  • Loading branch information
calvinrp committed Jun 22, 2024
1 parent 2467602 commit 37223a9
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion crates/wasm-pkg-common/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@ impl RegistryMetadata {
/// The preferred protocol is:
/// - the `preferredProtocol` metadata field, if given
/// - the protocol configuration key, if only one configuration is given
/// - the protocol backward-compatible aliases configuration, if only one configuration is given
pub fn preferred_protocol(&self) -> Option<&str> {
if let Some(protocol) = self.preferred_protocol.as_deref() {
return Some(protocol);
}
if self.protocol_configs.len() == 1 {
return self.protocol_configs.keys().next().map(|x| x.as_str());
} else if self.protocol_configs.is_empty() {
match (self.oci_registry.is_some(), self.warg_url.is_some()) {
(true, false) => return Some(OCI_PROTOCOL),
(false, true) => return Some(WARG_PROTOCOL),
_ => {}
}
}
None
}
Expand Down Expand Up @@ -197,14 +204,42 @@ mod tests {
}

#[test]
fn preferred_protocol_implicit() {
fn preferred_protocol_implicit_oci() {
let meta: RegistryMetadata = serde_json::from_value(json!({
"oci": {"registry": "oci.example.com"},
}))
.unwrap();
assert_eq!(meta.preferred_protocol(), Some("oci"));
}

#[test]
fn preferred_protocol_implicit_warg() {
let meta: RegistryMetadata = serde_json::from_value(json!({
"warg": {"url": "https://warg.example.com"},
}))
.unwrap();
assert_eq!(meta.preferred_protocol(), Some("warg"));
}

#[test]
fn backward_compat_preferred_protocol_implicit_oci() {
let meta: RegistryMetadata = serde_json::from_value(json!({
"ociRegistry": "oci.example.com",
"ociNamespacePrefix": "prefix/",
}))
.unwrap();
assert_eq!(meta.preferred_protocol(), Some("oci"));
}

#[test]
fn backward_compat_preferred_protocol_implicit_warg() {
let meta: RegistryMetadata = serde_json::from_value(json!({
"wargUrl": "https://warg.example.com",
}))
.unwrap();
assert_eq!(meta.preferred_protocol(), Some("warg"));
}

#[test]
fn basic_backward_compat_test() {
let meta: RegistryMetadata = serde_json::from_value(json!({
Expand Down

0 comments on commit 37223a9

Please sign in to comment.