Skip to content

Commit bd3b517

Browse files
committed
Split utils into submods
1 parent 56f1896 commit bd3b517

File tree

4 files changed

+83
-86
lines changed

4 files changed

+83
-86
lines changed

tests/all/utils.rs

Lines changed: 0 additions & 86 deletions
This file was deleted.

tests/all/utils/manifest.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::fs::File;
2+
use std::io::prelude::*;
3+
use std::path::Path;
4+
5+
use failure::Error;
6+
use serde_json;
7+
8+
#[derive(Deserialize)]
9+
pub struct NpmPackage {
10+
pub name: String,
11+
pub description: String,
12+
pub version: String,
13+
pub license: String,
14+
pub repository: Repository,
15+
pub files: Vec<String>,
16+
pub main: String,
17+
pub types: Option<String>,
18+
}
19+
20+
#[derive(Deserialize)]
21+
pub struct Repository {
22+
#[serde(rename = "type")]
23+
pub ty: String,
24+
pub url: String,
25+
}
26+
27+
pub fn read_package_json(path: &Path) -> Result<NpmPackage, Error> {
28+
let manifest_path = path.join("pkg").join("package.json");
29+
let mut pkg_file = File::open(manifest_path)?;
30+
let mut pkg_contents = String::new();
31+
pkg_file.read_to_string(&mut pkg_contents)?;
32+
33+
Ok(serde_json::from_str(&pkg_contents)?)
34+
}

tests/all/utils/mod.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::path::{Path, PathBuf};
2+
3+
use copy_dir::copy_dir;
4+
use tempfile;
5+
6+
pub(crate) mod manifest;
7+
pub(crate) mod readme;
8+
9+
pub struct Fixture {
10+
pub dir: tempfile::TempDir,
11+
pub path: PathBuf,
12+
}
13+
14+
/// Copy the given fixture into a unique temporary directory. This allows the
15+
/// test to mutate the copied fixture without messing up other tests that are
16+
/// also trying to read from or write to that fixture. The given path should be
17+
/// relative from the root of the repository, eg
18+
/// "tests/fixtures/im-from-brooklyn-the-place-where-stars-are-born".
19+
pub fn fixture<P>(fixture: P) -> Fixture
20+
where
21+
P: AsRef<Path>,
22+
{
23+
let fixture = fixture
24+
.as_ref()
25+
.canonicalize()
26+
.expect("should canonicalize fixture path OK");
27+
let dir = tempfile::tempdir().expect("should create temporary directory OK");
28+
let path = dir.path().join("wasm-pack");
29+
println!(
30+
"wasm-pack: copying test fixture '{}' to temporary directory '{}'",
31+
fixture.display(),
32+
path.display()
33+
);
34+
copy_dir(fixture, &path).expect("should copy fixture directory into temporary directory OK");
35+
Fixture { dir, path }
36+
}

tests/all/utils/readme.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::fs::File;
2+
use std::io::Read;
3+
use std::path::Path;
4+
5+
use failure::Error;
6+
7+
pub fn read_file(path: &Path) -> Result<String, Error> {
8+
let mut file = File::open(path)?;
9+
let mut contents = String::new();
10+
file.read_to_string(&mut contents)?;
11+
12+
Ok(contents)
13+
}

0 commit comments

Comments
 (0)