Skip to content

Commit bd29039

Browse files
committed
add unit test
1 parent 8b8670f commit bd29039

File tree

1 file changed

+128
-30
lines changed

1 file changed

+128
-30
lines changed

tests/build.rs

Lines changed: 128 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,6 @@ fn it_supports_wit_keywords() -> Result<()> {
128128
Ok(())
129129
}
130130

131-
#[test]
132-
fn it_adds_a_producers_field() -> Result<()> {
133-
let project = Project::new("foo", true)?;
134-
135-
project
136-
.cargo_component(["build", "--release"])
137-
.assert()
138-
.stderr(contains("Finished `release` profile [optimized] target(s)"))
139-
.success();
140-
141-
let path = project.release_wasm("foo");
142-
143-
validate_component(&path)?;
144-
145-
let wasm = fs::read(&path)
146-
.with_context(|| format!("failed to read wasm file `{path}`", path = path.display()))?;
147-
let section = wasm_metadata::Producers::from_wasm(&wasm)?.expect("missing producers section");
148-
149-
assert_eq!(
150-
section
151-
.get("processed-by")
152-
.expect("missing processed-by field")
153-
.get(env!("CARGO_PKG_NAME"))
154-
.expect("missing cargo-component field"),
155-
option_env!("CARGO_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
156-
);
157-
158-
Ok(())
159-
}
160-
161131
#[test]
162132
fn it_builds_wasm32_unknown_unknown_from_cli() -> Result<()> {
163133
let project = Project::new("foo", true)?;
@@ -1008,3 +978,131 @@ edition = "2021"
1008978

1009979
Ok(())
1010980
}
981+
982+
fn it_adds_a_producers_field() -> Result<()> {
983+
let project = Project::new("foo", true)?;
984+
985+
project
986+
.cargo_component(["build", "--release"])
987+
.assert()
988+
.stderr(contains("Finished `release` profile [optimized] target(s)"))
989+
.success();
990+
991+
let path = project.release_wasm("foo");
992+
993+
validate_component(&path)?;
994+
995+
let wasm = fs::read(&path)
996+
.with_context(|| format!("failed to read wasm file `{path}`", path = path.display()))?;
997+
let section = wasm_metadata::Producers::from_wasm(&wasm)?.expect("missing producers section");
998+
999+
assert_eq!(
1000+
section
1001+
.get("processed-by")
1002+
.expect("missing processed-by field")
1003+
.get(env!("CARGO_PKG_NAME"))
1004+
.expect("missing cargo-component field"),
1005+
option_env!("CARGO_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
1006+
);
1007+
1008+
Ok(())
1009+
}
1010+
1011+
#[test]
1012+
fn it_adds_metadata_from_cargo_toml() -> Result<()> {
1013+
let name = "foo";
1014+
let authors = "Jane Doe <jane@example.com>";
1015+
let description = "A test package";
1016+
let license = "Apache-2.0";
1017+
let version = "1.0.0";
1018+
let documentation = "https://example.com/docs";
1019+
let homepage = "https://example.com/home";
1020+
let repository = "https://example.com/repo";
1021+
1022+
let project = Project::new(name, true)?;
1023+
project.update_manifest(|mut doc| {
1024+
let package = &mut doc["package"];
1025+
package["name"] = value(name);
1026+
package["version"] = value(version);
1027+
package["authors"] = value(Array::from_iter([authors]));
1028+
package["description"] = value(description);
1029+
package["license"] = value(license);
1030+
package["documentation"] = value(documentation);
1031+
package["homepage"] = value(homepage);
1032+
package["repository"] = value(repository);
1033+
Ok(doc)
1034+
})?;
1035+
1036+
project
1037+
.cargo_component(["build", "--release"])
1038+
.assert()
1039+
.stderr(contains("Finished `release` profile [optimized] target(s)"))
1040+
.success();
1041+
1042+
let path = project.release_wasm("foo");
1043+
1044+
validate_component(&path)?;
1045+
1046+
let wasm = fs::read(&path)
1047+
.with_context(|| format!("failed to read wasm file `{path}`", path = path.display()))?;
1048+
1049+
let metadata = match wasm_metadata::Payload::from_binary(&wasm)? {
1050+
wasm_metadata::Payload::Component { metadata, .. } => metadata,
1051+
wasm_metadata::Payload::Module(_) => unreachable!("found a wasm module"),
1052+
};
1053+
1054+
assert_eq!(
1055+
&metadata.name.as_ref().expect("missing name").to_string(),
1056+
name
1057+
);
1058+
assert_eq!(
1059+
&metadata
1060+
.author
1061+
.as_ref()
1062+
.expect("missing authors")
1063+
.to_string(),
1064+
authors
1065+
);
1066+
assert_eq!(
1067+
&metadata
1068+
.description
1069+
.as_ref()
1070+
.expect("missing description")
1071+
.to_string(),
1072+
description
1073+
);
1074+
assert_eq!(
1075+
&metadata
1076+
.licenses
1077+
.as_ref()
1078+
.expect("missing licenses")
1079+
.to_string(),
1080+
license
1081+
);
1082+
assert_eq!(
1083+
&metadata
1084+
.source
1085+
.as_ref()
1086+
.expect("missing source")
1087+
.to_string(),
1088+
repository
1089+
);
1090+
assert_eq!(
1091+
&metadata
1092+
.homepage
1093+
.as_ref()
1094+
.expect("missing homepage")
1095+
.to_string(),
1096+
homepage
1097+
);
1098+
assert_eq!(
1099+
&metadata
1100+
.version
1101+
.as_ref()
1102+
.expect("missing version")
1103+
.to_string(),
1104+
version
1105+
);
1106+
1107+
Ok(())
1108+
}

0 commit comments

Comments
 (0)