Skip to content

Commit

Permalink
Merge pull request #211 from fitzgen/copy-fixtures-to-temp-dir
Browse files Browse the repository at this point in the history
Copy fixtures to temp dir
  • Loading branch information
ashleygwilliams authored Jul 26, 2018
2 parents cd572f9 + abfe66c commit 56f1896
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 152 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ matrix:
# tests pass
- rust: nightly
script:
# XXX: `--test-threads 1` is a hack until we have a proper fix for
# synchronizing access to the fixtures.
- cargo test --locked -- --test-threads 1
- cargo test --locked
- rustup component add rustfmt-preview
- cargo fmt --all -- --check
env: RUST_BACKTRACE=1
Expand Down
108 changes: 85 additions & 23 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ slog-term = "2.4"
slog-async = "2.3"
structopt = "0.2"
toml = "0.4"

[dev-dependencies]
copy_dir = "0.1.2"
tempfile = "3"
11 changes: 11 additions & 0 deletions src/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ use PBAR;

/// Copy the crate's README into the `pkg` directory.
pub fn copy_from_crate(path: &Path, step: &Step) -> Result<(), Error> {
assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist"
);
assert!(
fs::metadata(path.join("pkg"))
.ok()
.map_or(false, |m| m.is_dir()),
"crate's pkg directory should exist"
);

let msg = format!("{}Copying over your README...", emoji::DANCERS);
PBAR.step(step, &msg);
let crate_readme_path = path.join("README.md");
Expand Down
11 changes: 11 additions & 0 deletions tests/all/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
extern crate copy_dir;
extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate tempfile;
extern crate wasm_pack;

mod manifest;
mod readme;
mod utils;
82 changes: 37 additions & 45 deletions tests/manifest/main.rs → tests/all/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate wasm_pack;

mod utils;

use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;

use wasm_pack::manifest;
use utils;
use wasm_pack::{self, manifest};

#[test]
fn it_gets_the_crate_name_default_path() {
Expand Down Expand Up @@ -60,14 +53,14 @@ fn it_recognizes_a_map_during_depcheck() {

#[test]
fn it_creates_a_package_json_default_path() {
let fixture = utils::fixture(".");
let step = wasm_pack::progressbar::Step::new(1);
let path = PathBuf::from(".");
wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap();
assert!(manifest::write_package_json(&path, &None, false, "", &step).is_ok());
let package_json_path = &path.join("pkg").join("package.json");
wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &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::read_package_json(&path).is_ok());
let pkg = utils::read_package_json(&path).unwrap();
assert!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
Expand All @@ -88,14 +81,14 @@ fn it_creates_a_package_json_default_path() {

#[test]
fn it_creates_a_package_json_provided_path() {
let fixture = utils::fixture("tests/fixtures/js-hello-world");
let step = wasm_pack::progressbar::Step::new(1);
let path = PathBuf::from("tests/fixtures/js-hello-world");
wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap();
assert!(manifest::write_package_json(&path, &None, false, "", &step).is_ok());
let package_json_path = &path.join("pkg").join("package.json");
wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &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::read_package_json(&path).is_ok());
let pkg = utils::read_package_json(&path).unwrap();
assert!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.main, "js_hello_world.js");

Expand All @@ -109,16 +102,17 @@ fn it_creates_a_package_json_provided_path() {

#[test]
fn it_creates_a_package_json_provided_path_with_scope() {
let fixture = utils::fixture("tests/fixtures/scopes");
let step = wasm_pack::progressbar::Step::new(1);
let path = PathBuf::from("tests/fixtures/scopes");
wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap();
wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(
manifest::write_package_json(&path, &Some("test".to_string()), false, "", &step).is_ok()
manifest::write_package_json(&fixture.path, &Some("test".to_string()), false, "", &step)
.is_ok()
);
let package_json_path = &path.join("pkg").join("package.json");
let package_json_path = &fixture.path.join("pkg").join("package.json");
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!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
assert_eq!(pkg.name, "@test/scopes-hello-world");
assert_eq!(pkg.main, "scopes_hello_world.js");

Expand All @@ -132,14 +126,14 @@ fn it_creates_a_package_json_provided_path_with_scope() {

#[test]
fn it_creates_a_pkg_json_with_correct_files_on_node() {
let fixture = utils::fixture(".");
let step = wasm_pack::progressbar::Step::new(1);
let path = PathBuf::from(".");
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 = &path.join("pkg").join("package.json");
wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, false, "nodejs", &step).is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
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!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
Expand All @@ -161,14 +155,14 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {

#[test]
fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
let fixture = utils::fixture(".");
let step = wasm_pack::progressbar::Step::new(1);
let path = PathBuf::from(".");
wasm_pack::command::init::create_pkg_dir(&path, &step).unwrap();
assert!(manifest::write_package_json(&path, &None, true, "", &step).is_ok());
let package_json_path = &path.join("pkg").join("package.json");
wasm_pack::command::init::create_pkg_dir(&fixture.path, &step).unwrap();
assert!(manifest::write_package_json(&fixture.path, &None, true, "", &step).is_ok());
let package_json_path = &fixture.path.join("pkg").join("package.json");
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!(utils::manifest::read_package_json(&fixture.path).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path).unwrap();
assert_eq!(pkg.name, "wasm-pack");
assert_eq!(pkg.repository.ty, "git");
assert_eq!(
Expand All @@ -187,16 +181,14 @@ 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 = utils::fixture("tests/fixtures/bad-cargo-toml");
let step = wasm_pack::progressbar::Step::new(1);
assert!(
manifest::check_crate_config(&PathBuf::from("tests/fixtures/bad-cargo-toml"), &step)
.is_err()
);
assert!(manifest::check_crate_config(&fixture.path, &step).is_err());
}

#[test]
fn it_does_not_error_when_wasm_bindgen_is_declared() {
let fixture = utils::fixture("tests/fixtures/js-hello-world");
let step = wasm_pack::progressbar::Step::new(1);
let path = PathBuf::from("tests/fixtures/js-hello-world");
assert!(manifest::check_crate_config(&path, &step).is_ok());
assert!(manifest::check_crate_config(&fixture.path, &step).is_ok());
}
53 changes: 53 additions & 0 deletions tests/all/readme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
extern crate failure;
extern crate wasm_pack;

use std::fs;

use utils;
use wasm_pack::readme;

#[test]
fn it_copies_a_readme_default_path() {
let fixture = utils::fixture(".");
fs::create_dir(fixture.path.join("pkg")).expect("should create pkg directory OK");

let step = wasm_pack::progressbar::Step::new(1);
assert!(readme::copy_from_crate(&fixture.path, &step).is_ok());

let crate_readme_path = fixture.path.join("README.md");
let pkg_readme_path = fixture.path.join("pkg").join("README.md");
println!(
"wasm-pack: should have copied README.md from '{}' to '{}'",
crate_readme_path.display(),
pkg_readme_path.display()
);
assert!(fs::metadata(&crate_readme_path).is_ok());

assert!(fs::metadata(&pkg_readme_path).is_ok());

let crate_readme = utils::readme::read_file(&crate_readme_path).unwrap();
let pkg_readme = utils::readme::read_file(&pkg_readme_path).unwrap();
assert_eq!(crate_readme, pkg_readme);
}

#[test]
fn it_creates_a_package_json_provided_path() {
let fixture = utils::fixture("tests/fixtures/js-hello-world");
fs::create_dir(fixture.path.join("pkg")).expect("should create pkg directory OK");

let step = wasm_pack::progressbar::Step::new(1);
assert!(readme::copy_from_crate(&fixture.path, &step).is_ok());
let crate_readme_path = fixture.path.join("README.md");
let pkg_readme_path = fixture.path.join("pkg").join("README.md");
println!(
"wasm-pack: should have copied README.md from '{}' to '{}'",
crate_readme_path.display(),
pkg_readme_path.display()
);
assert!(fs::metadata(&crate_readme_path).is_ok());
assert!(fs::metadata(&pkg_readme_path).is_ok());

let crate_readme = utils::readme::read_file(&crate_readme_path).unwrap();
let pkg_readme = utils::readme::read_file(&pkg_readme_path).unwrap();
assert_eq!(crate_readme, pkg_readme);
}
86 changes: 86 additions & 0 deletions tests/all/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::path::{Path, PathBuf};

use copy_dir::copy_dir;
use tempfile;

/// Copy the given fixture into a unique temporary directory. This allows the
/// test to mutate the copied fixture without messing up other tests that are
/// also trying to read from or write to that fixture. The given path should be
/// relative from the root of the repository, eg
/// "tests/fixtures/im-from-brooklyn-the-place-where-stars-are-born".
pub fn fixture<P>(fixture: P) -> Fixture
where
P: AsRef<Path>,
{
let fixture = fixture
.as_ref()
.canonicalize()
.expect("should canonicalize fixture path OK");
let dir = tempfile::tempdir().expect("should create temporary directory OK");
let path = dir.path().join("wasm-pack");
println!(
"wasm-pack: copying test fixture '{}' to temporary directory '{}'",
fixture.display(),
path.display()
);
copy_dir(fixture, &path).expect("should copy fixture directory into temporary directory OK");
Fixture { dir, path }
}

pub struct Fixture {
pub dir: tempfile::TempDir,
pub path: PathBuf,
}

pub mod manifest {
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

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>,
pub main: String,
pub types: Option<String>,
}

#[derive(Deserialize)]
pub struct Repository {
#[serde(rename = "type")]
pub ty: String,
pub url: String,
}

pub fn read_package_json(path: &Path) -> Result<NpmPackage, Error> {
let manifest_path = path.join("pkg").join("package.json");
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)?)
}
}

pub mod readme {
use std::fs::File;
use std::io::Read;
use std::path::Path;

use failure::Error;

pub fn read_file(path: &Path) -> Result<String, Error> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;

Ok(contents)
}
}
34 changes: 0 additions & 34 deletions tests/manifest/utils.rs

This file was deleted.

Loading

0 comments on commit 56f1896

Please sign in to comment.