| •1 |
#![cfg(feature = "oci")] |
| 2 |
|
| 3 |
use std::{str::FromStr, vec}; |
| 4 |
|
| 5 |
use auditable_serde::VersionInfo; |
| 6 |
use wasm_encoder::Module; |
| 7 |
use wasm_metadata::*; |
| 8 |
|
| 9 |
#[test] |
| 10 |
fn add_to_empty_module() { |
| 11 |
let mut add = AddMetadata::default(); |
| 12 |
add.name = AddMetadataField::Set("foo".to_owned()); |
| 13 |
add.language = vec![("bar".to_owned(), "1.0".to_owned())]; |
| 14 |
add.processed_by = vec![("baz".to_owned(), "1.0".to_owned())]; |
| 15 |
add.sdk = vec![]; |
| 16 |
add.authors = AddMetadataField::Set(Authors::new("Chashu Cat")); |
| 17 |
add.description = AddMetadataField::Set(Description::new("Chashu likes tuna")); |
| 18 |
add.licenses = AddMetadataField::Set( |
| 19 |
Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap(), |
| 20 |
); |
| 21 |
add.source = AddMetadataField::Set( |
| 22 |
Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(), |
| 23 |
); |
| 24 |
add.homepage = AddMetadataField::Set( |
| 25 |
Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(), |
| 26 |
); |
| 27 |
add.revision = AddMetadataField::Set(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad")); |
| 28 |
add.version = AddMetadataField::Set(Version::new("1.0.0")); |
| 29 |
|
| 30 |
let json_str = r#"{"packages":[{"name":"adler","version":"0.2.3","source":"registry"}]}"#; |
| 31 |
let info = VersionInfo::from_str(json_str).unwrap(); |
| 32 |
let mut component = Module::new(); |
| 33 |
component.section(&Dependencies::new(info.clone())); |
| 34 |
let module = component.finish(); |
| 35 |
let module = add.to_wasm(&module).unwrap(); |
| 36 |
|
| 37 |
match Payload::from_binary(&module).unwrap() { |
| 38 |
Payload::Module(Metadata { |
| 39 |
name, |
| 40 |
producers, |
| 41 |
authors: author, |
| 42 |
licenses, |
| 43 |
source, |
| 44 |
range, |
| 45 |
description, |
| 46 |
homepage, |
| 47 |
revision, |
| 48 |
version, |
| 49 |
dependencies, |
| 50 |
}) => { |
| 51 |
assert_eq!(name, Some("foo".to_owned())); |
| 52 |
let producers = producers.expect("some producers"); |
| 53 |
assert_eq!( |
| 54 |
producers.get("language").unwrap().get("bar").unwrap(), |
| 55 |
"1.0" |
| 56 |
); |
| 57 |
assert_eq!( |
| 58 |
producers.get("processed-by").unwrap().get("baz").unwrap(), |
| 59 |
"1.0" |
| 60 |
); |
| 61 |
|
| 62 |
assert_eq!(author.unwrap(), Authors::new("Chashu Cat")); |
| 63 |
assert_eq!(description.unwrap(), Description::new("Chashu likes tuna")); |
| 64 |
assert_eq!( |
| 65 |
licenses.unwrap(), |
| 66 |
Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap() |
| 67 |
); |
| 68 |
assert_eq!( |
| 69 |
source.unwrap(), |
| 70 |
Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(), |
| 71 |
); |
| 72 |
assert_eq!( |
| 73 |
homepage.unwrap(), |
| 74 |
Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(), |
| 75 |
); |
| 76 |
assert_eq!( |
| 77 |
revision.unwrap(), |
| 78 |
Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad") |
| 79 |
); |
| 80 |
assert_eq!(version.unwrap(), Version::new("1.0.0")); |
| 81 |
assert_eq!(dependencies.unwrap().version_info(), &info,); |
| 82 |
|
| 83 |
assert_eq!(range.start, 0); |
| 84 |
assert_eq!(range.end, 455); |
| 85 |
} |
| 86 |
_ => panic!("metadata should be module"), |
| 87 |
} |
| 88 |
} |