Merge pull request #53 from ashleygwilliams/test-test-test

feat(test): test package json values
master
ashley williams 7 years ago committed by GitHub
commit 29499f445a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      tests/manifest.rs
  2. 31
      tests/utils/mod.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"]);
}

@ -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<String>,
}
#[derive(Deserialize)]
pub struct Repository {
#[serde(rename = "type")]
pub ty: String,
pub url: String,
}
pub fn read_package_json(path: &str) -> Result<NpmPackage, Error> {
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)?)
}
Loading…
Cancel
Save