From 4e6abcfd1d4474aa1c5ed232eb9211412a88362c Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 5 Nov 2018 14:14:23 -0800 Subject: [PATCH 1/6] Only read the `Cargo.toml` manifest once And then keep reusing what we read, instead of re-reading it again. Fixes #25 --- src/command/build.rs | 11 +++-- src/command/test.rs | 12 ++--- src/manifest/mod.rs | 38 ++++++++------- tests/all/bindgen.rs | 8 +-- tests/all/build.rs | 7 +-- tests/all/manifest.rs | 111 ++++++++++++++++++++++-------------------- 6 files changed, 99 insertions(+), 88 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index 12381fe..7d32b27 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -21,6 +21,7 @@ use PBAR; #[allow(missing_docs)] pub struct Build { pub crate_path: PathBuf, + pub crate_data: manifest::CargoManifest, pub scope: Option, pub disable_dts: bool, pub target: String, @@ -104,11 +105,13 @@ impl Build { /// Construct a build command from the given options. pub fn try_from_opts(build_opts: BuildOptions) -> Result { let crate_path = set_crate_path(build_opts.path)?; - let crate_name = manifest::get_crate_name(&crate_path)?; + let crate_data = manifest::read_cargo_toml(&crate_path)?; + let crate_name = manifest::get_crate_name(&crate_data).to_string(); let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir)); // let build_config = manifest::xxx(&crate_path).xxx(); Ok(Build { crate_path, + crate_data, scope: build_opts.scope, disable_dts: build_opts.disable_dts, target: build_opts.target, @@ -213,7 +216,7 @@ impl Build { fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { info!(&log, "Checking crate configuration..."); - manifest::check_crate_config(&self.crate_path, step)?; + manifest::check_crate_config(&self.crate_data, step)?; info!(&log, "Crate is correctly configured."); Ok(()) } @@ -251,7 +254,7 @@ impl Build { fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { info!(&log, "Writing a package.json..."); manifest::write_package_json( - &self.crate_path, + &self.crate_data, &self.out_dir, &self.scope, self.disable_dts, @@ -298,7 +301,7 @@ impl Build { info!(&log, "Installing wasm-bindgen-cli was successful."); info!(&log, "Getting the crate name from the manifest..."); - self.crate_name = manifest::get_crate_name(&self.crate_path)?; + self.crate_name = manifest::get_crate_name(&self.crate_data).to_string(); info!( &log, "Got crate name {:#?} from the manifest at {:#?}.", diff --git a/src/command/test.rs b/src/command/test.rs index 0c31dac..37a2321 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -82,6 +82,7 @@ pub struct TestOptions { /// A configured `wasm-pack test` command. pub struct Test { crate_path: PathBuf, + crate_data: manifest::CargoManifest, cache: Cache, node: bool, mode: BuildMode, @@ -116,13 +117,7 @@ impl Test { } = test_opts; let crate_path = set_crate_path(path)?; - - // let geckodriver = get_web_driver("geckodriver", test_opts.geckodriver, test_opts.firefox)?; - // let chromedriver = - // get_web_driver("chromedriver", test_opts.chromedriver, test_opts.chrome)?; - // let safaridriver = - // get_web_driver("safaridriver", test_opts.safaridriver, test_opts.safari)?; - + let crate_data = manifest::read_cargo_toml(&crate_path)?; let any_browser = chrome || firefox || safari; if !node && !any_browser { @@ -143,6 +138,7 @@ impl Test { Ok(Test { cache: Cache::new()?, crate_path, + crate_data, node, mode, chrome, @@ -248,7 +244,7 @@ impl Test { fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { info!(log, "Checking crate configuration..."); - manifest::check_crate_config(&self.crate_path, step)?; + manifest::check_crate_config(&self.crate_data, step)?; info!(log, "Crate is correctly configured."); Ok(()) } diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index f253b15..7d6d5e2 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -18,8 +18,9 @@ use serde_json; use toml; use PBAR; -#[derive(Debug, Deserialize)] -struct CargoManifest { +/// A parsed `Cargo.toml` manifest. +#[derive(Clone, Debug, Deserialize)] +pub struct CargoManifest { package: CargoPackage, dependencies: Option>, #[serde(rename = "dev-dependencies")] @@ -27,7 +28,7 @@ struct CargoManifest { lib: Option, } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize)] struct CargoPackage { name: String, authors: Vec, @@ -59,25 +60,26 @@ impl CargoPackage { } } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize)] #[serde(untagged)] enum CargoDependency { Simple(String), Detailed(DetailedCargoDependency), } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize)] struct DetailedCargoDependency { version: Option, } -#[derive(Debug, Deserialize)] +#[derive(Clone, Debug, Deserialize)] struct CargoLib { #[serde(rename = "crate-type")] crate_type: Option>, } -fn read_cargo_toml(path: &Path) -> Result { +/// Read the `Cargo.toml` inside the crate at the given `path`. +pub fn read_cargo_toml(path: &Path) -> Result { let manifest_path = path.join("Cargo.toml"); if !manifest_path.is_file() { return Err(Error::crate_config(&format!( @@ -210,7 +212,7 @@ impl CargoManifest { /// Generate a package.json file inside in `./pkg`. pub fn write_package_json( - path: &Path, + crate_data: &CargoManifest, out_dir: &Path, scope: &Option, disable_dts: bool, @@ -222,13 +224,12 @@ pub fn write_package_json( PBAR.step(step, &msg); let pkg_file_path = out_dir.join("package.json"); let mut pkg_file = File::create(pkg_file_path)?; - let crate_data = read_cargo_toml(path)?; let npm_data = if target == "nodejs" { - crate_data.into_commonjs(scope, disable_dts) + crate_data.clone().into_commonjs(scope, disable_dts) } else if target == "no-modules" { - crate_data.into_nomodules(scope, disable_dts) + crate_data.clone().into_nomodules(scope, disable_dts) } else { - crate_data.into_esmodules(scope, disable_dts) + crate_data.clone().into_esmodules(scope, disable_dts) }; let npm_json = serde_json::to_string_pretty(&npm_data)?; @@ -237,21 +238,22 @@ pub fn write_package_json( } /// Get the crate name for the crate at the given path. -pub fn get_crate_name(path: &Path) -> Result { - Ok(read_cargo_toml(path)?.package.name) +pub fn get_crate_name(crate_data: &CargoManifest) -> &str { + &crate_data.package.name } /// Check that the crate the given path is properly configured. -pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), failure::Error> { +pub fn check_crate_config(crate_data: &CargoManifest, step: &Step) -> Result<(), failure::Error> { let msg = format!("{}Checking crate configuration...", emoji::WRENCH); PBAR.step(&step, &msg); - check_crate_type(path)?; + check_crate_type(crate_data)?; Ok(()) } -fn check_crate_type(path: &Path) -> Result<(), failure::Error> { - if read_cargo_toml(path)?.lib.map_or(false, |lib| { +fn check_crate_type(crate_data: &CargoManifest) -> Result<(), failure::Error> { + if crate_data.lib.as_ref().map_or(false, |lib| { lib.crate_type + .as_ref() .map_or(false, |types| types.iter().any(|s| s == "cdylib")) }) { return Ok(()); diff --git a/tests/all/bindgen.rs b/tests/all/bindgen.rs index 985a4d0..ae836a8 100644 --- a/tests/all/bindgen.rs +++ b/tests/all/bindgen.rs @@ -31,7 +31,9 @@ fn downloading_prebuilt_wasm_bindgen_handles_http_errors() { let error = result.err().unwrap(); assert!(error.iter_chain().any(|e| e.to_string().contains("404"))); - assert!(error - .iter_chain() - .any(|e| e.to_string().contains(bad_version))); + assert!( + error + .iter_chain() + .any(|e| e.to_string().contains(bad_version)) + ); } diff --git a/tests/all/build.rs b/tests/all/build.rs index 2d8d56d..4f9c273 100644 --- a/tests/all/build.rs +++ b/tests/all/build.rs @@ -17,9 +17,10 @@ fn build_in_non_crate_directory_doesnt_panic() { "running wasm-pack in a non-crate directory should fail, but it should not panic" ); let err = result.unwrap_err(); - assert!(err - .iter_chain() - .any(|e| e.to_string().contains("missing a `Cargo.toml`"))); + assert!( + err.iter_chain() + .any(|e| e.to_string().contains("missing a `Cargo.toml`")) + ); } #[test] diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 459b8ab..bbb99bc 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -8,18 +8,16 @@ use wasm_pack::{self, manifest}; #[test] fn it_gets_the_crate_name_default_path() { let path = &PathBuf::from("."); - assert!(manifest::get_crate_name(path).is_ok()); - assert_eq!(manifest::get_crate_name(path).unwrap(), "wasm-pack"); + let crate_data = manifest::read_cargo_toml(&path).unwrap(); + let name = manifest::get_crate_name(&crate_data); + assert_eq!(name, "wasm-pack"); } #[test] fn it_gets_the_crate_name_provided_path() { let fixture = fixture::js_hello_world(); - assert!(manifest::get_crate_name(&fixture.path).is_ok()); - assert_eq!( - manifest::get_crate_name(&fixture.path).unwrap(), - "js-hello-world" - ); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + assert_eq!(manifest::get_crate_name(&crate_data), "js-hello-world"); } #[test] @@ -27,8 +25,9 @@ fn it_checks_has_cdylib_default_path() { let fixture = fixture::no_cdylib(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&fixture.path, &step).is_err()); + assert!(manifest::check_crate_config(&crate_data, &step).is_err()); } #[test] @@ -36,15 +35,17 @@ fn it_checks_has_cdylib_provided_path() { let fixture = fixture::js_hello_world(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&fixture.path, &step).is_ok()); + assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); } #[test] fn it_checks_has_cdylib_wrong_crate_type() { let fixture = fixture::bad_cargo_toml(); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&fixture.path, &step).is_err()); + assert!(manifest::check_crate_config(&crate_data, &step).is_err()); } #[test] @@ -52,17 +53,19 @@ fn it_recognizes_a_map_during_depcheck() { let fixture = fixture::serde_feature(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&fixture.path, &step).is_ok()); + assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); } #[test] fn it_creates_a_package_json_default_path() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok()); + assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); @@ -83,9 +86,9 @@ fn it_creates_a_package_json_default_path() { "js_hello_world.d.ts", "js_hello_world.js", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -93,9 +96,10 @@ fn it_creates_a_package_json_default_path() { fn it_creates_a_package_json_provided_path() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok()); + assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); @@ -109,9 +113,9 @@ fn it_creates_a_package_json_provided_path() { "js_hello_world.d.ts", "js_hello_world.js", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -119,17 +123,20 @@ fn it_creates_a_package_json_provided_path() { fn it_creates_a_package_json_provided_path_with_scope() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json( - &fixture.path, - &out_dir, - &Some("test".to_string()), - false, - "", - &step - ) - .is_ok()); + assert!( + manifest::write_package_json( + &crate_data, + &out_dir, + &Some("test".to_string()), + false, + "", + &step + ) + .is_ok() + ); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); @@ -143,9 +150,9 @@ fn it_creates_a_package_json_provided_path_with_scope() { "js_hello_world.d.ts", "js_hello_world.js", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -153,11 +160,11 @@ fn it_creates_a_package_json_provided_path_with_scope() { fn it_creates_a_pkg_json_with_correct_files_on_node() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!( - manifest::write_package_json(&fixture.path, &out_dir, &None, false, "nodejs", &step) - .is_ok() + manifest::write_package_json(&crate_data, &out_dir, &None, false, "nodejs", &step).is_ok() ); let package_json_path = &out_dir.join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -178,9 +185,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { "js_hello_world_bg.js", "js_hello_world.d.ts", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -188,17 +195,13 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json( - &fixture.path, - &out_dir, - &None, - false, - "no-modules", - &step - ) - .is_ok()); + assert!( + manifest::write_package_json(&crate_data, &out_dir, &None, false, "no-modules", &step) + .is_ok() + ); let package_json_path = &out_dir.join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); @@ -218,9 +221,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { "js_hello_world.js", "js_hello_world.d.ts", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -228,9 +231,10 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { fn it_creates_a_pkg_json_in_out_dir() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("./custom/out"); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, false, "", &step).is_ok()); + assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); let package_json_path = &fixture.path.join(&out_dir).join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -241,9 +245,10 @@ fn it_creates_a_pkg_json_in_out_dir() { fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json(&fixture.path, &out_dir, &None, true, "", &step).is_ok()); + assert!(manifest::write_package_json(&crate_data, &out_dir, &None, true, "", &step).is_ok()); let package_json_path = &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()); @@ -267,8 +272,9 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { #[test] fn it_errors_when_wasm_bindgen_is_not_declared() { let fixture = fixture::bad_cargo_toml(); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&fixture.path, &step).is_err()); + assert!(manifest::check_crate_config(&crate_data, &step).is_err()); } #[test] @@ -276,6 +282,7 @@ fn it_does_not_error_when_wasm_bindgen_is_declared() { let fixture = fixture::js_hello_world(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); + let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&fixture.path, &step).is_ok()); + assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); } From 511e6c29a056fcb9d8ae1753babc02975a4ccf8a Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 5 Nov 2018 14:18:56 -0800 Subject: [PATCH 2/6] Refactor: replace `manifest::read_cargo_toml` with a constructor --- src/command/build.rs | 2 +- src/command/test.rs | 2 +- src/manifest/mod.rs | 34 +++++++++++++++++----------------- tests/all/manifest.rs | 30 +++++++++++++++--------------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index 7d32b27..dc2e5e9 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -105,7 +105,7 @@ impl Build { /// Construct a build command from the given options. pub fn try_from_opts(build_opts: BuildOptions) -> Result { let crate_path = set_crate_path(build_opts.path)?; - let crate_data = manifest::read_cargo_toml(&crate_path)?; + let crate_data = manifest::CargoManifest::read(&crate_path)?; let crate_name = manifest::get_crate_name(&crate_data).to_string(); let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir)); // let build_config = manifest::xxx(&crate_path).xxx(); diff --git a/src/command/test.rs b/src/command/test.rs index 37a2321..ff67ef1 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -117,7 +117,7 @@ impl Test { } = test_opts; let crate_path = set_crate_path(path)?; - let crate_data = manifest::read_cargo_toml(&crate_path)?; + let crate_data = manifest::CargoManifest::read(&crate_path)?; let any_browser = chrome || firefox || safari; if !node && !any_browser { diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 7d6d5e2..d7d3fdf 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -78,25 +78,25 @@ struct CargoLib { crate_type: Option>, } -/// Read the `Cargo.toml` inside the crate at the given `path`. -pub fn read_cargo_toml(path: &Path) -> Result { - let manifest_path = path.join("Cargo.toml"); - if !manifest_path.is_file() { - return Err(Error::crate_config(&format!( - "Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?", - path.display() - )) - .into()); - } - let mut cargo_file = File::open(manifest_path)?; - let mut cargo_contents = String::new(); - cargo_file.read_to_string(&mut cargo_contents)?; +impl CargoManifest { + /// Read the `Cargo.toml` inside the crate at the given `crate_path`. + pub fn read(crate_path: &Path) -> Result { + let manifest_path = crate_path.join("Cargo.toml"); + if !manifest_path.is_file() { + return Err(Error::crate_config(&format!( + "Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?", + crate_path.display() + )) + .into()); + } + let mut cargo_file = File::open(manifest_path)?; + let mut cargo_contents = String::new(); + cargo_file.read_to_string(&mut cargo_contents)?; - let manifest: CargoManifest = toml::from_str(&cargo_contents)?; - Ok(manifest) -} + let manifest: CargoManifest = toml::from_str(&cargo_contents)?; + Ok(manifest) + } -impl CargoManifest { fn into_commonjs(mut self, scope: &Option, disable_dts: bool) -> NpmPackage { let filename = self.package.name.replace("-", "_"); let wasm_file = format!("{}_bg.wasm", filename); diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index bbb99bc..a407418 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -8,7 +8,7 @@ use wasm_pack::{self, manifest}; #[test] fn it_gets_the_crate_name_default_path() { let path = &PathBuf::from("."); - let crate_data = manifest::read_cargo_toml(&path).unwrap(); + let crate_data = manifest::CargoManifest::read(&path).unwrap(); let name = manifest::get_crate_name(&crate_data); assert_eq!(name, "wasm-pack"); } @@ -16,7 +16,7 @@ fn it_gets_the_crate_name_default_path() { #[test] fn it_gets_the_crate_name_provided_path() { let fixture = fixture::js_hello_world(); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); assert_eq!(manifest::get_crate_name(&crate_data), "js-hello-world"); } @@ -25,7 +25,7 @@ fn it_checks_has_cdylib_default_path() { let fixture = fixture::no_cdylib(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); assert!(manifest::check_crate_config(&crate_data, &step).is_err()); } @@ -35,7 +35,7 @@ fn it_checks_has_cdylib_provided_path() { let fixture = fixture::js_hello_world(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); } @@ -43,7 +43,7 @@ fn it_checks_has_cdylib_provided_path() { #[test] fn it_checks_has_cdylib_wrong_crate_type() { let fixture = fixture::bad_cargo_toml(); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); assert!(manifest::check_crate_config(&crate_data, &step).is_err()); } @@ -53,7 +53,7 @@ fn it_recognizes_a_map_during_depcheck() { let fixture = fixture::serde_feature(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); } @@ -62,7 +62,7 @@ fn it_recognizes_a_map_during_depcheck() { fn it_creates_a_package_json_default_path() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); @@ -96,7 +96,7 @@ fn it_creates_a_package_json_default_path() { fn it_creates_a_package_json_provided_path() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); @@ -123,7 +123,7 @@ fn it_creates_a_package_json_provided_path() { fn it_creates_a_package_json_provided_path_with_scope() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!( @@ -160,7 +160,7 @@ fn it_creates_a_package_json_provided_path_with_scope() { fn it_creates_a_pkg_json_with_correct_files_on_node() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!( @@ -195,7 +195,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!( @@ -231,7 +231,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { fn it_creates_a_pkg_json_in_out_dir() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("./custom/out"); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); @@ -245,7 +245,7 @@ fn it_creates_a_pkg_json_in_out_dir() { fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { let fixture = fixture::js_hello_world(); let out_dir = fixture.path.join("pkg"); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!(manifest::write_package_json(&crate_data, &out_dir, &None, true, "", &step).is_ok()); @@ -272,7 +272,7 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { #[test] fn it_errors_when_wasm_bindgen_is_not_declared() { let fixture = fixture::bad_cargo_toml(); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); assert!(manifest::check_crate_config(&crate_data, &step).is_err()); } @@ -282,7 +282,7 @@ fn it_does_not_error_when_wasm_bindgen_is_declared() { let fixture = fixture::js_hello_world(); // Ensure that there is a `Cargo.lock`. fixture.cargo_check(); - let crate_data = manifest::read_cargo_toml(&fixture.path).unwrap(); + let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); } From 0e09b3fd3c8bb0c7816aaf53d6dbf65c3cec690f Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 5 Nov 2018 14:23:18 -0800 Subject: [PATCH 3/6] Refactor: make `write_package_json` a method on `CargoManifest` --- src/command/build.rs | 3 +-- src/manifest/mod.rs | 54 +++++++++++++++++++++---------------------- tests/all/manifest.rs | 43 ++++++++++++++++++++++------------ 3 files changed, 56 insertions(+), 44 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index dc2e5e9..b3cc665 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -253,8 +253,7 @@ impl Build { fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { info!(&log, "Writing a package.json..."); - manifest::write_package_json( - &self.crate_data, + self.crate_data.write_package_json( &self.out_dir, &self.scope, self.disable_dts, diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index d7d3fdf..8581d22 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -97,6 +97,33 @@ impl CargoManifest { Ok(manifest) } + /// Generate a package.json file inside in `./pkg`. + pub fn write_package_json( + &self, + out_dir: &Path, + scope: &Option, + disable_dts: bool, + target: &str, + step: &Step, + ) -> Result<(), failure::Error> { + let msg = format!("{}Writing a package.json...", emoji::MEMO); + + PBAR.step(step, &msg); + let pkg_file_path = out_dir.join("package.json"); + let mut pkg_file = File::create(pkg_file_path)?; + let npm_data = if target == "nodejs" { + self.clone().into_commonjs(scope, disable_dts) + } else if target == "no-modules" { + self.clone().into_nomodules(scope, disable_dts) + } else { + self.clone().into_esmodules(scope, disable_dts) + }; + + let npm_json = serde_json::to_string_pretty(&npm_data)?; + pkg_file.write_all(npm_json.as_bytes())?; + Ok(()) + } + fn into_commonjs(mut self, scope: &Option, disable_dts: bool) -> NpmPackage { let filename = self.package.name.replace("-", "_"); let wasm_file = format!("{}_bg.wasm", filename); @@ -210,33 +237,6 @@ impl CargoManifest { } } -/// Generate a package.json file inside in `./pkg`. -pub fn write_package_json( - crate_data: &CargoManifest, - out_dir: &Path, - scope: &Option, - disable_dts: bool, - target: &str, - step: &Step, -) -> Result<(), failure::Error> { - let msg = format!("{}Writing a package.json...", emoji::MEMO); - - PBAR.step(step, &msg); - let pkg_file_path = out_dir.join("package.json"); - let mut pkg_file = File::create(pkg_file_path)?; - let npm_data = if target == "nodejs" { - crate_data.clone().into_commonjs(scope, disable_dts) - } else if target == "no-modules" { - crate_data.clone().into_nomodules(scope, disable_dts) - } else { - crate_data.clone().into_esmodules(scope, disable_dts) - }; - - let npm_json = serde_json::to_string_pretty(&npm_data)?; - pkg_file.write_all(npm_json.as_bytes())?; - Ok(()) -} - /// Get the crate name for the crate at the given path. pub fn get_crate_name(crate_data: &CargoManifest) -> &str { &crate_data.package.name diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index a407418..0fef60a 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -65,7 +65,11 @@ fn it_creates_a_package_json_default_path() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); + assert!( + crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .is_ok() + ); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); @@ -99,7 +103,11 @@ fn it_creates_a_package_json_provided_path() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); + assert!( + crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .is_ok() + ); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); @@ -127,15 +135,9 @@ fn it_creates_a_package_json_provided_path_with_scope() { let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!( - manifest::write_package_json( - &crate_data, - &out_dir, - &Some("test".to_string()), - false, - "", - &step - ) - .is_ok() + crate_data + .write_package_json(&out_dir, &Some("test".to_string()), false, "", &step) + .is_ok() ); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -164,7 +166,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!( - manifest::write_package_json(&crate_data, &out_dir, &None, false, "nodejs", &step).is_ok() + crate_data + .write_package_json(&out_dir, &None, false, "nodejs", &step) + .is_ok() ); let package_json_path = &out_dir.join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -199,7 +203,8 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); assert!( - manifest::write_package_json(&crate_data, &out_dir, &None, false, "no-modules", &step) + crate_data + .write_package_json(&out_dir, &None, false, "no-modules", &step) .is_ok() ); let package_json_path = &out_dir.join("package.json"); @@ -234,7 +239,11 @@ fn it_creates_a_pkg_json_in_out_dir() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json(&crate_data, &out_dir, &None, false, "", &step).is_ok()); + assert!( + crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .is_ok() + ); let package_json_path = &fixture.path.join(&out_dir).join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -248,7 +257,11 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!(manifest::write_package_json(&crate_data, &out_dir, &None, true, "", &step).is_ok()); + assert!( + crate_data + .write_package_json(&out_dir, &None, true, "", &step) + .is_ok() + ); let package_json_path = &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()); From 25490301e003274772e9d0a3496ac4783c28589f Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 5 Nov 2018 14:25:55 -0800 Subject: [PATCH 4/6] Refactor: make `get_crate_name` a method of `CargoManifest` --- src/command/build.rs | 4 ++-- src/manifest/mod.rs | 10 +++++----- tests/all/manifest.rs | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index b3cc665..43eb04b 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -106,7 +106,7 @@ impl Build { pub fn try_from_opts(build_opts: BuildOptions) -> Result { let crate_path = set_crate_path(build_opts.path)?; let crate_data = manifest::CargoManifest::read(&crate_path)?; - let crate_name = manifest::get_crate_name(&crate_data).to_string(); + let crate_name = crate_data.get_crate_name().to_string(); let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir)); // let build_config = manifest::xxx(&crate_path).xxx(); Ok(Build { @@ -300,7 +300,7 @@ impl Build { info!(&log, "Installing wasm-bindgen-cli was successful."); info!(&log, "Getting the crate name from the manifest..."); - self.crate_name = manifest::get_crate_name(&self.crate_data).to_string(); + self.crate_name = self.crate_data.get_crate_name().to_string(); info!( &log, "Got crate name {:#?} from the manifest at {:#?}.", diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 8581d22..174519f 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -124,6 +124,11 @@ impl CargoManifest { Ok(()) } + /// Get the crate name for the crate at the given path. + pub fn get_crate_name(&self) -> &str { + &self.package.name + } + fn into_commonjs(mut self, scope: &Option, disable_dts: bool) -> NpmPackage { let filename = self.package.name.replace("-", "_"); let wasm_file = format!("{}_bg.wasm", filename); @@ -237,11 +242,6 @@ impl CargoManifest { } } -/// Get the crate name for the crate at the given path. -pub fn get_crate_name(crate_data: &CargoManifest) -> &str { - &crate_data.package.name -} - /// Check that the crate the given path is properly configured. pub fn check_crate_config(crate_data: &CargoManifest, step: &Step) -> Result<(), failure::Error> { let msg = format!("{}Checking crate configuration...", emoji::WRENCH); diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 0fef60a..fbc5327 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -9,7 +9,7 @@ use wasm_pack::{self, manifest}; fn it_gets_the_crate_name_default_path() { let path = &PathBuf::from("."); let crate_data = manifest::CargoManifest::read(&path).unwrap(); - let name = manifest::get_crate_name(&crate_data); + let name = crate_data.get_crate_name(); assert_eq!(name, "wasm-pack"); } @@ -17,7 +17,7 @@ fn it_gets_the_crate_name_default_path() { fn it_gets_the_crate_name_provided_path() { let fixture = fixture::js_hello_world(); let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); - assert_eq!(manifest::get_crate_name(&crate_data), "js-hello-world"); + assert_eq!(crate_data.get_crate_name(), "js-hello-world"); } #[test] From 6d3e9dfebb1c2a3b97f85838f1bec482aaa90d9c Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 5 Nov 2018 14:29:28 -0800 Subject: [PATCH 5/6] Refactor: make `check_crate_config` a method of `CargoManifest` --- src/command/build.rs | 2 +- src/command/test.rs | 2 +- src/manifest/mod.rs | 48 +++++++++++++++++++++---------------------- tests/all/manifest.rs | 12 +++++------ 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/command/build.rs b/src/command/build.rs index 43eb04b..e15603b 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -216,7 +216,7 @@ impl Build { fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { info!(&log, "Checking crate configuration..."); - manifest::check_crate_config(&self.crate_data, step)?; + self.crate_data.check_crate_config(step)?; info!(&log, "Crate is correctly configured."); Ok(()) } diff --git a/src/command/test.rs b/src/command/test.rs index ff67ef1..a78cfc2 100644 --- a/src/command/test.rs +++ b/src/command/test.rs @@ -244,7 +244,7 @@ impl Test { fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> { info!(log, "Checking crate configuration..."); - manifest::check_crate_config(&self.crate_data, step)?; + self.crate_data.check_crate_config(step)?; info!(log, "Crate is correctly configured."); Ok(()) } diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index 174519f..0ef8c31 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -129,6 +129,30 @@ impl CargoManifest { &self.package.name } + /// Check that the crate the given path is properly configured. + pub fn check_crate_config(&self, step: &Step) -> Result<(), failure::Error> { + let msg = format!("{}Checking crate configuration...", emoji::WRENCH); + PBAR.step(&step, &msg); + self.check_crate_type()?; + Ok(()) + } + + fn check_crate_type(&self) -> Result<(), failure::Error> { + if self.lib.as_ref().map_or(false, |lib| { + lib.crate_type + .as_ref() + .map_or(false, |types| types.iter().any(|s| s == "cdylib")) + }) { + return Ok(()); + } + Err(Error::crate_config( + "crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your \ + Cargo.toml file:\n\n\ + [lib]\n\ + crate-type = [\"cdylib\", \"rlib\"]" + ).into()) + } + fn into_commonjs(mut self, scope: &Option, disable_dts: bool) -> NpmPackage { let filename = self.package.name.replace("-", "_"); let wasm_file = format!("{}_bg.wasm", filename); @@ -241,27 +265,3 @@ impl CargoManifest { }) } } - -/// Check that the crate the given path is properly configured. -pub fn check_crate_config(crate_data: &CargoManifest, step: &Step) -> Result<(), failure::Error> { - let msg = format!("{}Checking crate configuration...", emoji::WRENCH); - PBAR.step(&step, &msg); - check_crate_type(crate_data)?; - Ok(()) -} - -fn check_crate_type(crate_data: &CargoManifest) -> Result<(), failure::Error> { - if crate_data.lib.as_ref().map_or(false, |lib| { - lib.crate_type - .as_ref() - .map_or(false, |types| types.iter().any(|s| s == "cdylib")) - }) { - return Ok(()); - } - Err(Error::crate_config( - "crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your \ - Cargo.toml file:\n\n\ - [lib]\n\ - crate-type = [\"cdylib\", \"rlib\"]" - ).into()) -} diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index fbc5327..0474846 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -27,7 +27,7 @@ fn it_checks_has_cdylib_default_path() { fixture.cargo_check(); let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&crate_data, &step).is_err()); + assert!(crate_data.check_crate_config(&step).is_err()); } #[test] @@ -37,7 +37,7 @@ fn it_checks_has_cdylib_provided_path() { fixture.cargo_check(); let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); + assert!(crate_data.check_crate_config(&step).is_ok()); } #[test] @@ -45,7 +45,7 @@ fn it_checks_has_cdylib_wrong_crate_type() { let fixture = fixture::bad_cargo_toml(); let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&crate_data, &step).is_err()); + assert!(crate_data.check_crate_config(&step).is_err()); } #[test] @@ -55,7 +55,7 @@ fn it_recognizes_a_map_during_depcheck() { fixture.cargo_check(); let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); + assert!(crate_data.check_crate_config(&step).is_ok()); } #[test] @@ -287,7 +287,7 @@ fn it_errors_when_wasm_bindgen_is_not_declared() { let fixture = fixture::bad_cargo_toml(); let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&crate_data, &step).is_err()); + assert!(crate_data.check_crate_config(&step).is_err()); } #[test] @@ -297,5 +297,5 @@ fn it_does_not_error_when_wasm_bindgen_is_declared() { fixture.cargo_check(); let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); - assert!(manifest::check_crate_config(&crate_data, &step).is_ok()); + assert!(crate_data.check_crate_config(&step).is_ok()); } From 098eb33cea1a98099f5dc97d87a1c985ea94ebb8 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 5 Nov 2018 15:00:15 -0800 Subject: [PATCH 6/6] Run cargo fmt --- tests/all/bindgen.rs | 8 ++-- tests/all/build.rs | 7 ++-- tests/all/manifest.rs | 86 ++++++++++++++++++------------------------- 3 files changed, 42 insertions(+), 59 deletions(-) diff --git a/tests/all/bindgen.rs b/tests/all/bindgen.rs index ae836a8..985a4d0 100644 --- a/tests/all/bindgen.rs +++ b/tests/all/bindgen.rs @@ -31,9 +31,7 @@ fn downloading_prebuilt_wasm_bindgen_handles_http_errors() { let error = result.err().unwrap(); assert!(error.iter_chain().any(|e| e.to_string().contains("404"))); - assert!( - error - .iter_chain() - .any(|e| e.to_string().contains(bad_version)) - ); + assert!(error + .iter_chain() + .any(|e| e.to_string().contains(bad_version))); } diff --git a/tests/all/build.rs b/tests/all/build.rs index 4f9c273..2d8d56d 100644 --- a/tests/all/build.rs +++ b/tests/all/build.rs @@ -17,10 +17,9 @@ fn build_in_non_crate_directory_doesnt_panic() { "running wasm-pack in a non-crate directory should fail, but it should not panic" ); let err = result.unwrap_err(); - assert!( - err.iter_chain() - .any(|e| e.to_string().contains("missing a `Cargo.toml`")) - ); + assert!(err + .iter_chain() + .any(|e| e.to_string().contains("missing a `Cargo.toml`"))); } #[test] diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index 0474846..400950b 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -65,11 +65,9 @@ fn it_creates_a_package_json_default_path() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!( - crate_data - .write_package_json(&out_dir, &None, false, "", &step) - .is_ok() - ); + assert!(crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); @@ -90,9 +88,9 @@ fn it_creates_a_package_json_default_path() { "js_hello_world.d.ts", "js_hello_world.js", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -103,11 +101,9 @@ fn it_creates_a_package_json_provided_path() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!( - crate_data - .write_package_json(&out_dir, &None, false, "", &step) - .is_ok() - ); + assert!(crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); @@ -121,9 +117,9 @@ fn it_creates_a_package_json_provided_path() { "js_hello_world.d.ts", "js_hello_world.js", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -134,11 +130,9 @@ fn it_creates_a_package_json_provided_path_with_scope() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!( - crate_data - .write_package_json(&out_dir, &Some("test".to_string()), false, "", &step) - .is_ok() - ); + assert!(crate_data + .write_package_json(&out_dir, &Some("test".to_string()), false, "", &step) + .is_ok()); let package_json_path = &fixture.path.join("pkg").join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok()); @@ -152,9 +146,9 @@ fn it_creates_a_package_json_provided_path_with_scope() { "js_hello_world.d.ts", "js_hello_world.js", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -165,11 +159,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!( - crate_data - .write_package_json(&out_dir, &None, false, "nodejs", &step) - .is_ok() - ); + assert!(crate_data + .write_package_json(&out_dir, &None, false, "nodejs", &step) + .is_ok()); let package_json_path = &out_dir.join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); @@ -189,9 +181,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() { "js_hello_world_bg.js", "js_hello_world.d.ts", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -202,11 +194,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!( - crate_data - .write_package_json(&out_dir, &None, false, "no-modules", &step) - .is_ok() - ); + assert!(crate_data + .write_package_json(&out_dir, &None, false, "no-modules", &step) + .is_ok()); let package_json_path = &out_dir.join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); @@ -226,9 +216,9 @@ fn it_creates_a_pkg_json_with_correct_files_on_nomodules() { "js_hello_world.js", "js_hello_world.d.ts", ] - .iter() - .map(|&s| String::from(s)) - .collect(); + .iter() + .map(|&s| String::from(s)) + .collect(); assert_eq!(actual_files, expected_files); } @@ -239,11 +229,9 @@ fn it_creates_a_pkg_json_in_out_dir() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!( - crate_data - .write_package_json(&out_dir, &None, false, "", &step) - .is_ok() - ); + assert!(crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .is_ok()); let package_json_path = &fixture.path.join(&out_dir).join("package.json"); assert!(fs::metadata(package_json_path).is_ok()); @@ -257,11 +245,9 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() { let crate_data = manifest::CargoManifest::read(&fixture.path).unwrap(); let step = wasm_pack::progressbar::Step::new(1); wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); - assert!( - crate_data - .write_package_json(&out_dir, &None, true, "", &step) - .is_ok() - ); + assert!(crate_data + .write_package_json(&out_dir, &None, true, "", &step) + .is_ok()); let package_json_path = &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());