Skip to content

Commit

Permalink
Merge branch 'master' into wasm-pack-test
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleygwilliams authored and fitzgen committed Sep 7, 2018
2 parents 041438e + d0fc382 commit ccc2158
Show file tree
Hide file tree
Showing 21 changed files with 298 additions and 122 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
target/
**/target
**/*.rs.bk
**/pkg
tests/.crates.toml
tests/bin
wasm-pack.log
8 changes: 7 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ INSTALL_NODE_VIA_NVM: &INSTALL_NODE_VIA_NVM
rustup target add wasm32-unknown-unknown
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
source ~/.nvm/nvm.sh
nvm install v10.5
nvm install lts/carbon

# Cache `cargo install`ed tools, but don't cache the project's `target`
# directory (which ends up over-caching and filling all disk space!)
cache:
directories:
- /home/travis/.cargo

DEPLOY_TO_GITHUB: &DEPLOY_TO_GITHUB
before_deploy:
Expand Down
52 changes: 29 additions & 23 deletions src/binaries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,40 +62,46 @@ pub fn bin_path(log: &Logger, crate_path: &Path, bin: &str) -> Option<PathBuf> {
})
}

fn with_url_context<T, E>(url: &str, r: Result<T, E>) -> Result<T, impl failure::Fail>
where
Result<T, E>: failure::ResultExt<T, E>,
{
use failure::ResultExt;
r.with_context(|_| format!("when requesting {}", url))
}

fn transfer(
url: &str,
easy: &mut curl::easy::Easy,
data: &mut Vec<u8>,
) -> Result<(), failure::Error> {
let mut transfer = easy.transfer();
with_url_context(
url,
transfer.write_function(|part| {
data.extend_from_slice(part);
Ok(part.len())
}),
)?;
with_url_context(url, transfer.perform())?;
Ok(())
}

fn curl(url: &str) -> Result<Vec<u8>, failure::Error> {
let mut data = Vec::new();

fn with_url_context<T, E>(url: &str, r: Result<T, E>) -> Result<T, impl failure::Fail>
where
Result<T, E>: failure::ResultExt<T, E>,
{
use failure::ResultExt;
r.with_context(|_| format!("when requesting {}", url))
}

let mut easy = curl::easy::Easy::new();
with_url_context(url, easy.follow_location(true))?;
with_url_context(url, easy.url(url))?;
transfer(url, &mut easy, &mut data)?;

{
let mut transfer = easy.transfer();
with_url_context(
url,
transfer.write_function(|part| {
data.extend_from_slice(part);
Ok(part.len())
}),
)?;
with_url_context(url, transfer.perform())?;
}

let code = with_url_context(url, easy.response_code())?;
if 200 <= code && code < 300 {
let status_code = with_url_context(url, easy.response_code())?;
if 200 <= status_code && status_code < 300 {
Ok(data)
} else {
Err(Error::http(&format!(
"received a bad HTTP status code ({}) when requesting {}",
code, url
status_code, url
)).into())
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use progressbar::Step;
use slog::Logger;
use std::path::PathBuf;
use std::time::Instant;
use test;
use webdriver;
use test::{self, webdriver};
use PBAR;

#[derive(Debug, Default, StructOpt)]
Expand Down Expand Up @@ -129,8 +128,10 @@ impl Test {
}

if headless && !any_browser {
return Error::crate_config("The `--headless` flag only applies to browser tests")
.map(|_| unreachable!());
return Error::crate_config(
"The `--headless` flag only applies to browser tests. Node does not provide a UI, \
so it doesn't make sense to talk about a headless version of Node tests.",
).map(|_| unreachable!());
}

Ok(Test {
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ pub mod progressbar;
pub mod readme;
pub mod target;
pub mod test;
pub mod webdriver;

use progressbar::ProgressOutput;

Expand Down
108 changes: 94 additions & 14 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,36 @@ use PBAR;
struct CargoManifest {
package: CargoPackage,
dependencies: Option<HashMap<String, CargoDependency>>,
#[serde(rename = "dev-dependencies")]
dev_dependencies: Option<HashMap<String, CargoDependency>>,
lib: Option<CargoLib>,
}

fn normalize_dependency_name(dep: &str) -> String {
dep.replace("-", "_")
}

fn normalize_dependencies(
deps: HashMap<String, CargoDependency>,
) -> HashMap<String, CargoDependency> {
let mut new_deps = HashMap::with_capacity(deps.len());
for (key, val) in deps {
new_deps.insert(normalize_dependency_name(&key), val);
}
new_deps
}

impl CargoManifest {
fn normalize_dependencies(&mut self) {
if let Some(deps) = self.dependencies.take() {
self.dependencies = Some(normalize_dependencies(deps));
}
if let Some(dev_deps) = self.dev_dependencies.take() {
self.dev_dependencies = Some(normalize_dependencies(dev_deps));
}
}
}

#[derive(Debug, Deserialize)]
struct CargoPackage {
name: String,
Expand Down Expand Up @@ -86,7 +113,10 @@ fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
let mut cargo_contents = String::new();
cargo_file.read_to_string(&mut cargo_contents)?;

Ok(toml::from_str(&cargo_contents)?)
let mut manifest: CargoManifest = toml::from_str(&cargo_contents)?;
manifest.normalize_dependencies();

Ok(manifest)
}

impl CargoManifest {
Expand Down Expand Up @@ -192,8 +222,7 @@ pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), Error> {
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg);
check_wasm_bindgen(path)?;
// TODO: check that if there is a `wasm-bindgen-test` dependency, then it
// matches `wasm-bindgen`.
check_wasm_bindgen_test(path)?;
check_crate_type(path)?;
Ok(())
}
Expand All @@ -203,6 +232,24 @@ fn check_wasm_bindgen(path: &Path) -> Result<(), Error> {
Ok(())
}

fn check_wasm_bindgen_test(path: &Path) -> Result<(), Error> {
let expected_version = get_wasm_bindgen_version(path)?;

// Only do the version check if `wasm-bindgen-test` is actually a
// dependency. Not every crate needs to have tests!
if let Ok(actual_version) = get_wasm_bindgen_test_version(path) {
if expected_version != actual_version {
return Error::crate_config(&format!(
"The `wasm-bindgen-test` dependency version ({}) must match \
the `wasm-bindgen` dependency version ({}), but it does not.",
actual_version, expected_version
));
}
}

Ok(())
}

fn check_crate_type(path: &Path) -> Result<(), Error> {
if read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type
Expand All @@ -211,32 +258,43 @@ fn check_crate_type(path: &Path) -> Result<(), Error> {
return Ok(());
}
Error::crate_config(
"crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your Cargo.toml file:\n\n[lib]\ncrate-type = [\"cdylib\"]"
"crate-type must be cdylib to compile to wasm32-unknown-unknown. Add the following to your \
Cargo.toml file:\n\n\
[lib]\n\
crate-type = [\"cdylib\"]"
)
}

/// Get the version of `wasm-bindgen` specified as a dependency.
pub fn get_wasm_bindgen_version(path: &Path) -> Result<String, Error> {
if let Some(deps) = read_cargo_toml(path)?.dependencies {
match deps
.get("wasm-bindgen")
.or_else(|| deps.get("wasm_bindgen"))
{
fn get_dependency_version(
dependencies: Option<&HashMap<String, CargoDependency>>,
dependency: &str,
dependencies_section_name: &str,
version_suggestion: &str,
) -> Result<String, Error> {
if let Some(deps) = dependencies {
let dependency = normalize_dependency_name(dependency);
match deps.get(&dependency) {
Some(CargoDependency::Simple(version))
| Some(CargoDependency::Detailed(DetailedCargoDependency {
version: Some(version),
})) => Ok(version.clone()),
Some(CargoDependency::Detailed(DetailedCargoDependency { version: None })) => {
let msg = format!(
"\"{}\" dependency is missing its version number",
style("wasm-bindgen").bold().dim()
style(&dependency).bold().dim()
);
Err(Error::CrateConfig { message: msg })
}
None => {
let message = format!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n[dependencies]\nwasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim());
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
[{}]\n\
{} = \"{}\"",
style(&dependency).bold().dim(),
dependencies_section_name,
dependency,
version_suggestion
);
Err(Error::CrateConfig { message })
}
}
Expand All @@ -245,3 +303,25 @@ pub fn get_wasm_bindgen_version(path: &Path) -> Result<String, Error> {
Err(Error::CrateConfig { message })
}
}

/// Get the version of `wasm-bindgen` specified as a dependency.
pub fn get_wasm_bindgen_version(path: &Path) -> Result<String, Error> {
let toml = read_cargo_toml(path)?;
get_dependency_version(
toml.dependencies.as_ref(),
"wasm-bindgen",
"dependencies",
"0.2",
)
}

/// Get the version of `wasm-bindgen-test` specified as a dependency.
pub fn get_wasm_bindgen_test_version(path: &Path) -> Result<String, Error> {
let toml = read_cargo_toml(path)?;
get_dependency_version(
toml.dev_dependencies.as_ref(),
"wasm-bindgen-test",
"dev-dependencies",
"0.2",
)
}
5 changes: 4 additions & 1 deletion src/target.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Information about the target.
//! Information about the target wasm-pack is currently being compiled for.
//!
//! That is, whether we are building wasm-pack for windows vs linux, and x86 vs
//! x86-64, etc.

#![allow(missing_docs)]

Expand Down
2 changes: 2 additions & 0 deletions src/test.rs → src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Testing a Rust crate compiled to wasm.

pub mod webdriver;

use error::Error;
use slog::Logger;
use std::ffi::OsStr;
Expand Down
Loading

0 comments on commit ccc2158

Please sign in to comment.