From b9df6d60ae556fa4d4074ae0e634b8c38b7cd3fa Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Wed, 14 Mar 2018 10:06:03 +0100 Subject: [PATCH] feat(test): test package json values --- tests/manifest.rs | 19 +++++++++++++++++++ tests/utils/mod.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 tests/utils/mod.rs diff --git a/tests/manifest.rs b/tests/manifest.rs index 8db99eb..d4ddce2 100644 --- a/tests/manifest.rs +++ b/tests/manifest.rs @@ -1,5 +1,11 @@ +extern crate failure; +#[macro_use] +extern crate serde_derive; +extern crate serde_json; extern crate wasm_pack; +mod utils; + use std::fs; use wasm_pack::manifest; @@ -26,6 +32,15 @@ fn it_creates_a_package_json_default_path() { assert!(manifest::write_package_json(&path).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.files, ["wasm_pack.js", "wasm_pack_bg.wasm"]); } #[test] @@ -35,4 +50,8 @@ fn it_creates_a_package_json_provided_path() { assert!(manifest::write_package_json(&path).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, "js-hello-world"); + assert_eq!(pkg.files, ["js_hello_world.js", "js_hello_world_bg.wasm"]); } diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs new file mode 100644 index 0000000..5b83909 --- /dev/null +++ b/tests/utils/mod.rs @@ -0,0 +1,31 @@ +use std::fs::File; +use std::io::prelude::*; + +use failure::Error; +use serde_json; + +#[derive(Deserialize)] +pub struct NpmPackage { + pub name: String, + pub description: String, + pub version: String, + pub license: String, + pub repository: Repository, + pub files: Vec, +} + +#[derive(Deserialize)] +pub struct Repository { + #[serde(rename = "type")] + pub ty: String, + pub url: String, +} + +pub fn read_package_json(path: &str) -> Result { + let manifest_path = format!("{}/pkg/package.json", path); + let mut pkg_file = File::open(manifest_path)?; + let mut pkg_contents = String::new(); + pkg_file.read_to_string(&mut pkg_contents)?; + + Ok(serde_json::from_str(&pkg_contents)?) +}