Skip to content

Commit

Permalink
use absolutize crate instead of std canonicalize for crate absolute path
Browse files Browse the repository at this point in the history
add two tests from use cases:
- transitive dependencies
- crc crate
  • Loading branch information
xmclark committed Oct 30, 2018
1 parent 9218c65 commit a2a3520
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 4 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -21,6 +21,7 @@ indicatif = "0.9.0"
lazy_static = "1.1.0"
openssl = { version = '0.10.11', optional = true }
parking_lot = "0.6"
path-absolutize = "1.1.1"
serde = "1.0.74"
serde_derive = "1.0.74"
serde_json = "1.0.26"
Expand Down
8 changes: 4 additions & 4 deletions src/command/utils.rs
Expand Up @@ -2,21 +2,21 @@

use emoji;
use failure;
use path_absolutize::*;
use progressbar::Step;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use PBAR;

/// If an explicit path is given, then use it, otherwise assume the current
/// directory is the crate path.
pub fn set_crate_path(path: Option<PathBuf>) -> io::Result<PathBuf> {
pub fn set_crate_path(path: Option<PathBuf>) -> Result<PathBuf, failure::Error> {
let crate_path = match path {
Some(p) => p,
None => PathBuf::from("."),
};

crate_path.canonicalize()
let absolute_crate_path = crate_path.absolutize()?;
Ok(absolute_crate_path)
}

/// Construct our `pkg` directory in the crate.
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -12,6 +12,7 @@ extern crate indicatif;
#[macro_use]
extern crate lazy_static;
extern crate parking_lot;
extern crate path_absolutize;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
Expand Down
30 changes: 30 additions & 0 deletions tests/all/build.rs
Expand Up @@ -35,3 +35,33 @@ fn it_should_build_js_hello_world_example() {
command::run_wasm_pack(cli.cmd, &logger)
.expect("running wasm-pack in a js-hello-world directory should succeed.");
}

#[test]
fn it_should_build_nested_project_with_transitive_dependencies() {
let fixture = utils::fixture::transitive_dependencies();
fixture.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
&fixture.path.join("main").display().to_string(),
])
.unwrap();
let logger = logger::new(&cli.cmd, cli.verbosity).unwrap();
command::run_wasm_pack(cli.cmd, &logger)
.expect("running wasm-pack in a project with transitive dependencies should succeed.");
}

#[test]
fn it_should_build_project_with_dependency_on_crc_crate() {
let fixture = utils::fixture::project_with_crc();
fixture.install_local_wasm_bindgen();
let cli = Cli::from_iter_safe(vec![
"wasm-pack",
"build",
&fixture.path.display().to_string(),
])
.unwrap();
let logger = logger::new(&cli.cmd, cli.verbosity).unwrap();
command::run_wasm_pack(cli.cmd, &logger)
.expect("running wasm-pack in a project with dependency on crc crate should succeed.");
}
204 changes: 204 additions & 0 deletions tests/all/utils/fixture.rs
Expand Up @@ -474,3 +474,207 @@ pub fn wbg_test_node() -> Fixture {
);
fixture
}

pub fn transitive_dependencies() -> Fixture {
fn project_main_fixture(fixture: &mut Fixture) {
fixture.file(PathBuf::from("main/README"), "# Main Fixture\n");
fixture.file(
PathBuf::from("main/Cargo.toml"),
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "main_project"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
project_a = { path = "../project_a" }
project_b = { path = "../project_b" }
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
"#,
);
fixture.file(
PathBuf::from("main/src/lib.rs"),
r#"
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
"#,
);
}

fn project_a_fixture(fixture: &mut Fixture) {
fixture.file(
PathBuf::from("project_a/README"),
"# Project Alpha Fixture\n",
);
fixture.file(
PathBuf::from("project_a/Cargo.toml"),
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "project_a"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
project_b = { path = "../project_b" }
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
"#,
);
fixture.file(
PathBuf::from("project_a/src/lib.rs"),
r#"
extern crate wasm_bindgen;
// extern crate project_b;
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
"#,
);
}

fn project_b_fixture(fixture: &mut Fixture) {
fixture.file(
PathBuf::from("project_b/README"),
"# Project Beta Fixture\n",
);
fixture.file(
PathBuf::from("project_b/Cargo.toml"),
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "project_b"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
"#,
);
fixture.file(
PathBuf::from("project_b/src/lib.rs"),
r#"
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello, {}!", name));
}
"#,
);
}

let mut fixture = Fixture::new();
project_b_fixture(&mut fixture);
project_a_fixture(&mut fixture);
project_main_fixture(&mut fixture);
fixture
}

pub fn project_with_crc() -> Fixture {
let fixture = Fixture::new();
fixture.file(PathBuf::from("README"), "# Project Beta Fixture\n");
fixture.file(
PathBuf::from("Cargo.toml"),
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "proj"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "=0.2.21"
crc = "*"
[dev-dependencies]
wasm-bindgen-test = "=0.2.21"
"#,
);
fixture.file(
PathBuf::from("src/lib.rs"),
r#"
extern crate crc;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use crc::crc16;
// Import the `window.alert` function from the Web.
#[wasm_bindgen]
extern {
fn alert(s: &str);
}
// Export a `greet` function from Rust to JavaScript, that alerts a
// hello message.
#[wasm_bindgen]
pub fn greet(name: &str) {
assert_eq!(crc16::checksum_x25(b"123456789"), 0x906e);
alert(&format!("Hello, {}!", name));
}
"#,
);
fixture
}

0 comments on commit a2a3520

Please sign in to comment.