fix(files): set files correctly for node and cleanup tests

master
Ashley Williams 7 years ago
parent e4d7048c10
commit 5004517bf0
  1. 2
      src/command/init.rs
  2. 21
      src/manifest.rs
  3. 39
      tests/manifest/main.rs

@ -161,7 +161,7 @@ impl Init {
fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Writing a package.json...");
manifest::write_package_json(&self.crate_path, &self.scope, self.disable_dts, step)?;
manifest::write_package_json(&self.crate_path, &self.scope, self.disable_dts, &self.target, step)?;
#[cfg(not(target_os = "windows"))]
info!(
&log,

@ -68,11 +68,10 @@ fn read_cargo_toml(path: &str) -> Result<CargoManifest, Error> {
}
impl CargoManifest {
fn into_npm(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
fn into_npm(mut self, scope: &Option<String>, disable_dts: bool, target: &str) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);
let bindgen_js_file = format!("{}_bg.js", filename);
let dts_file = if disable_dts == true {
None
@ -80,10 +79,16 @@ impl CargoManifest {
Some(format!("{}.d.ts", filename))
};
let js_bg_file = if target == "nodejs" {
Some(format!("{}_bg.js", filename))
} else {
None
};
if let Some(s) = scope {
self.package.name = format!("@{}/{}", s, self.package.name);
}
let mut files = vec![wasm_file, bindgen_js_file];
let mut files = vec![wasm_file];
match dts_file {
Some(ref dts_file) => {
@ -92,6 +97,13 @@ impl CargoManifest {
None => {}
}
match js_bg_file {
Some(ref js_bg_file) => {
files.push(js_bg_file.to_string());
}
None => {}
}
NpmPackage {
name: self.package.name,
collaborators: self.package.authors,
@ -114,6 +126,7 @@ pub fn write_package_json(
path: &str,
scope: &Option<String>,
disable_dts: bool,
target: &str,
step: &Step,
) -> Result<(), Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO);
@ -129,7 +142,7 @@ pub fn write_package_json(
let pkg_file_path = format!("{}/pkg/package.json", path);
let mut pkg_file = File::create(pkg_file_path)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm(scope, disable_dts);
let npm_data = crate_data.into_npm(scope, disable_dts, target);
if npm_data.description.is_none() {
PBAR.warn(&warn_fmt("description"));

@ -49,7 +49,7 @@ fn it_creates_a_package_json_default_path() {
let step = wasm_pack::progressbar::Step::new(1);
let path = ".".to_string();
wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap();
assert!(manifest::write_package_json(&path, &None, false, &step).is_ok());
assert!(manifest::write_package_json(&path, &None, false, "", &step).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::read_package_json(&path).is_ok());
@ -66,7 +66,7 @@ fn it_creates_a_package_json_default_path() {
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> =
["wasm_pack_bg.wasm", "wasm_pack_bg.js", "wasm_pack.d.ts"]
["wasm_pack_bg.wasm", "wasm_pack.d.ts"]
.iter()
.map(|&s| String::from(s))
.collect();
@ -78,7 +78,7 @@ fn it_creates_a_package_json_provided_path() {
let step = wasm_pack::progressbar::Step::new(1);
let path = "tests/fixtures/js-hello-world".to_string();
wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap();
assert!(manifest::write_package_json(&path, &None, false, &step).is_ok());
assert!(manifest::write_package_json(&path, &None, false, "", &step).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::read_package_json(&path).is_ok());
@ -89,7 +89,6 @@ fn it_creates_a_package_json_provided_path() {
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world_bg.js",
"js_hello_world.d.ts",
].iter()
.map(|&s| String::from(s))
@ -102,7 +101,7 @@ fn it_creates_a_package_json_provided_path_with_scope() {
let step = wasm_pack::progressbar::Step::new(1);
let path = "tests/fixtures/scopes".to_string();
wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap();
assert!(manifest::write_package_json(&path, &Some("test".to_string()), false, &step).is_ok());
assert!(manifest::write_package_json(&path, &Some("test".to_string()), false, "", &step).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::read_package_json(&path).is_ok());
@ -113,7 +112,6 @@ fn it_creates_a_package_json_provided_path_with_scope() {
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = [
"scopes_hello_world_bg.wasm",
"scopes_hello_world_bg.js",
"scopes_hello_world.d.ts",
].iter()
.map(|&s| String::from(s))
@ -121,6 +119,35 @@ fn it_creates_a_package_json_provided_path_with_scope() {
assert_eq!(actual_files, expected_files);
}
#[test]
fn it_creates_a_pkg_json_with_correct_files_on_node() {
let step = wasm_pack::progressbar::Step::new(1);
let path = ".".to_string();
wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap();
assert!(manifest::write_package_json(&path, &None, false, "nodejs", &step).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::read_package_json(&path).is_ok());
let pkg = utils::read_package_json(&path).unwrap();
assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
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");
let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> =
["wasm_pack_bg.wasm", "wasm_pack_bg.js", "wasm_pack.d.ts"]
.iter()
.map(|&s| String::from(s))
.collect();
assert_eq!(actual_files, expected_files);
}
#[test]
fn it_errors_when_wasm_bindgen_is_not_declared() {
let step = wasm_pack::progressbar::Step::new(1);

Loading…
Cancel
Save