test(manifest): update tests to reflect new pkgjsons

master
Ashley Williams 7 years ago
parent c26a834772
commit fc7c2d1593
  1. 35
      tests/all/manifest.rs
  2. 13
      tests/all/utils/manifest.rs

@ -68,12 +68,11 @@ fn it_creates_a_package_json_default_path() {
pkg.repository.url,
"https://github.com/ashleygwilliams/wasm-pack.git"
);
assert_eq!(pkg.main, "wasm_pack.js");
let types = pkg.types.unwrap_or_default();
assert_eq!(types, "wasm_pack.d.ts");
assert_eq!(pkg.module, "wasm_pack.js");
assert_eq!(pkg.types, "wasm_pack.d.ts");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm", "wasm_pack.d.ts"]
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm", "wasm_pack.d.ts", "wasm_pack.js"]
.iter()
.map(|&s| String::from(s))
.collect();
@ -92,10 +91,10 @@ fn it_creates_a_package_json_provided_path() {
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.main, "js_hello_world.js");
assert_eq!(pkg.module, "js_hello_world.js");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["js_hello_world_bg.wasm", "js_hello_world.d.ts"]
let expected_files: HashSet<String> = ["js_hello_world_bg.wasm", "js_hello_world.d.ts", "js_hello_world.js"]
.iter()
.map(|&s| String::from(s))
.collect();
@ -123,10 +122,10 @@ fn it_creates_a_package_json_provided_path_with_scope() {
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "@test/scopes-hello-world");
assert_eq!(pkg.main, "scopes_hello_world.js");
assert_eq!(pkg.module, "scopes_hello_world.js");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["scopes_hello_world_bg.wasm", "scopes_hello_world.d.ts"]
let expected_files: HashSet<String> = ["scopes_hello_world_bg.wasm", "scopes_hello_world.d.ts", "scopes_hello_world.js"]
.iter()
.map(|&s| String::from(s))
.collect();
@ -154,8 +153,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
"https://github.com/ashleygwilliams/wasm-pack.git"
);
assert_eq!(pkg.main, "wasm_pack.js");
let types = pkg.types.unwrap_or_default();
assert_eq!(types, "wasm_pack.d.ts");
assert_eq!(pkg.types, "wasm_pack.d.ts");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> =
@ -177,19 +175,6 @@ fn it_creates_a_pkg_json_in_out_dir() {
let package_json_path = &fixture.path.join(&out_dir).join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.main, "js_hello_world.js");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["js_hello_world_bg.wasm", "js_hello_world.d.ts"]
.iter()
.map(|&s| String::from(s))
.collect();
assert_eq!(actual_files, expected_files);
}
#[test]
@ -209,10 +194,10 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
pkg.repository.url,
"https://github.com/ashleygwilliams/wasm-pack.git"
);
assert_eq!(pkg.main, "wasm_pack.js");
assert_eq!(pkg.module, "wasm_pack.js");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm"]
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm", "wasm_pack.js"]
.iter()
.map(|&s| String::from(s))
.collect();

@ -13,8 +13,16 @@ pub struct NpmPackage {
pub license: String,
pub repository: Repository,
pub files: Vec<String>,
#[serde(default = "default_none")]
pub main: String,
pub types: Option<String>,
#[serde(default = "default_none")]
pub module: String,
#[serde(default = "default_none")]
pub types: String,
}
fn default_none() -> String {
"".to_string()
}
#[derive(Deserialize)]
@ -29,6 +37,7 @@ pub fn read_package_json(path: &Path, out_dir: &Path) -> Result<NpmPackage, Erro
let mut pkg_file = File::open(manifest_path)?;
let mut pkg_contents = String::new();
pkg_file.read_to_string(&mut pkg_contents)?;
println!("pkg_file: {}", pkg_contents);
Ok(serde_json::from_str(&pkg_contents)?)
}

Loading…
Cancel
Save