-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from fitzgen/copy-fixtures-to-temp-dir
Copy fixtures to temp dir
- Loading branch information
Showing
11 changed files
with
288 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.