forked from bytecodealliance/wasm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.rs
More file actions
310 lines (289 loc) · 12 KB
/
Copy pathcomponent.rs
File metadata and controls
310 lines (289 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use std::{str::FromStr, vec};
use auditable_serde::VersionInfo;
use wasm_encoder::{Component, Module};
use wasm_metadata::*;
#[test]
fn add_to_empty_component() {
let mut add = AddMetadata::default();
add.name = AddMetadataField::Set("foo".to_owned());
add.language = vec![("bar".to_owned(), "1.0".to_owned())];
add.processed_by = vec![("baz".to_owned(), "1.0".to_owned())];
add.sdk = vec![];
add.authors = AddMetadataField::Set(Authors::new("Chashu Cat"));
add.description = AddMetadataField::Set(Description::new("Chashu likes tuna"));
add.licenses = AddMetadataField::Set(
Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap(),
);
add.source = AddMetadataField::Set(
Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
add.homepage = AddMetadataField::Set(
Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
add.revision = AddMetadataField::Set(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"));
add.version = AddMetadataField::Set(Version::new("1.0.0"));
let json_str = r#"{"packages":[{"name":"adler","version":"0.2.3","source":"registry"}]}"#;
let info = VersionInfo::from_str(json_str).unwrap();
let mut component = Component::new();
component.section(&Dependencies::new(info.clone()));
let component = component.finish();
let component = add.to_wasm(&component).unwrap();
match Payload::from_binary(&component).unwrap() {
Payload::Component {
children,
metadata:
Metadata {
name,
producers,
authors,
description,
licenses,
source,
range,
homepage,
revision,
version,
dependencies,
},
} => {
assert!(children.is_empty());
assert_eq!(name, Some("foo".to_owned()));
let producers = producers.expect("some producers");
assert_eq!(
producers.get("language").unwrap().get("bar").unwrap(),
"1.0"
);
assert_eq!(
producers.get("processed-by").unwrap().get("baz").unwrap(),
"1.0"
);
assert_eq!(authors.unwrap(), Authors::new("Chashu Cat"));
assert_eq!(description.unwrap(), Description::new("Chashu likes tuna"));
assert_eq!(
licenses.unwrap(),
Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap()
);
assert_eq!(
source.unwrap(),
Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
assert_eq!(
homepage.unwrap(),
Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
assert_eq!(
revision.unwrap(),
Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad")
);
assert_eq!(version.unwrap(), Version::new("1.0.0"));
assert_eq!(dependencies.unwrap().version_info(), &info,);
assert_eq!(range.start, 0);
assert_eq!(range.end, 465);
}
_ => panic!("metadata should be component"),
}
}
#[test]
fn add_to_nested_component() {
// Create the same old module, stick some metadata into it
let mut add = AddMetadata::default();
add.name = AddMetadataField::Set("foo".to_owned());
add.language = vec![("bar".to_owned(), "1.0".to_owned())];
add.processed_by = vec![("baz".to_owned(), "1.0".to_owned())];
add.sdk = vec![];
add.authors = AddMetadataField::Set(Authors::new("Chashu Cat"));
add.description = AddMetadataField::Set(Description::new("Chashu likes tuna"));
add.licenses = AddMetadataField::Set(
Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap(),
);
add.source = AddMetadataField::Set(
Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
add.homepage = AddMetadataField::Set(
Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
add.revision = AddMetadataField::Set(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"));
add.version = AddMetadataField::Set(Version::new("1.0.0"));
let json_str = r#"{"packages":[{"name":"adler","version":"0.2.3","source":"registry"}]}"#;
let info = VersionInfo::from_str(json_str).unwrap();
let mut component = Module::new();
component.section(&Dependencies::new(info.clone()));
let module = component.finish();
let module = add.to_wasm(&module).unwrap();
// Stick that module inside a component.
let mut component = wasm_encoder::Component::new();
component.section(&wasm_encoder::RawSection {
id: wasm_encoder::ComponentSectionId::CoreModule.into(),
data: &module,
});
let component = component.finish();
// Add some different metadata to the component.
let mut add = AddMetadata::default();
add.name = AddMetadataField::Set("gussie".to_owned());
add.sdk = vec![("willa".to_owned(), "sparky".to_owned())];
let component = add.to_wasm(&component).unwrap();
match Payload::from_binary(&component).unwrap() {
Payload::Component {
children,
metadata: Metadata {
name, producers, ..
},
} => {
// Check that the component metadata is in the component
assert_eq!(name, Some("gussie".to_owned()));
let producers = producers.as_ref().expect("some producers");
assert_eq!(
producers.get("sdk").unwrap().get("willa").unwrap(),
&"sparky".to_owned()
);
// Check that there is a single child with the metadata set for the module
assert_eq!(children.len(), 1);
match children.get(0).unwrap() {
Payload::Module(Metadata {
name,
producers,
authors,
licenses,
source,
range,
description,
homepage,
revision,
version,
dependencies,
}) => {
assert_eq!(name, &Some("foo".to_owned()));
let producers = producers.as_ref().expect("some producers");
assert_eq!(
producers.get("language").unwrap().get("bar").unwrap(),
"1.0"
);
assert_eq!(
producers.get("processed-by").unwrap().get("baz").unwrap(),
"1.0"
);
assert_eq!(authors, &Some(Authors::new("Chashu Cat")));
assert_eq!(description, &Some(Description::new("Chashu likes tuna")));
assert_eq!(
licenses,
&Some(
Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT")
.unwrap()
)
);
assert_eq!(
source,
&Some(
Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap()
),
);
assert_eq!(
homepage,
&Some(
Homepage::new("https://github.com/bytecodealliance/wasm-tools")
.unwrap()
),
);
assert_eq!(
revision,
&Some(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"))
);
assert_eq!(version, &Some(Version::new("1.0.0")));
assert_eq!(dependencies.as_ref().unwrap().version_info(), &info);
assert_eq!(range.start, 11);
assert_eq!(range.end, 466);
}
_ => panic!("child is a module"),
}
}
_ => panic!("root should be component"),
}
}
#[test]
fn add_then_clear_fields() {
let mut add1 = AddMetadata::default();
add1.name = AddMetadataField::Set("foo".to_owned());
add1.language = vec![("bar".to_owned(), "1.0".to_owned())];
add1.processed_by = vec![("baz".to_owned(), "1.0".to_owned())];
add1.sdk = vec![];
add1.authors = AddMetadataField::Set(Authors::new("Chashu Cat"));
add1.description = AddMetadataField::Set(Description::new("Chashu likes tuna"));
add1.licenses = AddMetadataField::Set(
Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap(),
);
add1.source = AddMetadataField::Set(
Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
add1.homepage = AddMetadataField::Set(
Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
add1.revision =
AddMetadataField::Set(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"));
add1.version = AddMetadataField::Set(Version::new("1.0.0"));
let json_str = r#"{"packages":[{"name":"adler","version":"0.2.3","source":"registry"}]}"#;
let info = VersionInfo::from_str(json_str).unwrap();
let mut component1 = Component::new();
component1.section(&Dependencies::new(info.clone()));
let component1 = component1.finish();
let component1 = add1.to_wasm(&component1).unwrap();
let mut add2 = AddMetadata::default();
add2.name = AddMetadataField::Clear;
add2.version = AddMetadataField::Clear;
add2.description = AddMetadataField::Set(Description::new("Chashu likes something else"));
add2.revision = AddMetadataField::Clear;
let component2 = add2.to_wasm(&component1).unwrap();
match Payload::from_binary(&component2).unwrap() {
Payload::Component {
children,
metadata:
Metadata {
name,
producers,
authors,
description,
licenses,
source,
range,
homepage,
revision,
version,
dependencies,
},
} => {
assert!(children.is_empty());
assert_eq!(name, None);
let producers = producers.expect("some producers");
assert_eq!(
producers.get("language").unwrap().get("bar").unwrap(),
"1.0"
);
assert_eq!(
producers.get("processed-by").unwrap().get("baz").unwrap(),
"1.0"
);
assert_eq!(authors.unwrap(), Authors::new("Chashu Cat"));
assert_eq!(
description.unwrap(),
Description::new("Chashu likes something else")
);
assert_eq!(
licenses.unwrap(),
Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap()
);
assert_eq!(
source.unwrap(),
Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
assert_eq!(
homepage.unwrap(),
Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
);
assert_eq!(revision, None);
assert_eq!(version, None);
assert_eq!(dependencies.unwrap().version_info(), &info,);
assert_eq!(range.start, 0);
assert_eq!(range.end, 434);
}
_ => panic!("metadata should be component"),
}
}