Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recurse up the path looking for the Cargo.toml #624

Merged
merged 1 commit into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use binary_install::{Cache, Download};
use bindgen;
use build;
use cache;
use command::utils::{create_pkg_dir, set_crate_path};
use command::utils::{create_pkg_dir, get_crate_path};
use emoji;
use failure::Error;
use license;
Expand Down Expand Up @@ -118,7 +118,7 @@ pub enum BuildProfile {
/// Everything required to configure and run the `wasm-pack build` command.
#[derive(Debug, StructOpt)]
pub struct BuildOptions {
/// The path to the Rust crate.
/// The path to the Rust crate. If not set, searches up the path from the current directory.
#[structopt(parse(from_os_str))]
pub path: Option<PathBuf>,

Expand Down Expand Up @@ -193,7 +193,7 @@ type BuildStep = fn(&mut Build) -> Result<(), Error>;
impl Build {
/// Construct a build command from the given options.
pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, Error> {
let crate_path = set_crate_path(build_opts.path)?;
let crate_path = get_crate_path(build_opts.path)?;
let crate_data = manifest::CrateData::new(&crate_path, build_opts.out_name.clone())?;
let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir));

Expand Down
4 changes: 2 additions & 2 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub enum Command {
#[structopt(name = "pack")]
/// 🍱 create a tar of your npm package but don't publish!
Pack {
/// The path to the Rust crate.
/// The path to the Rust crate. If not set, searches up the path from the current dirctory.
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,
},
Expand All @@ -44,7 +44,7 @@ pub enum Command {
#[structopt(long = "access", short = "a")]
access: Option<Access>,

/// The path to the Rust crate.
/// The path to the Rust crate. If not set, searches up the path from the current dirctory.
#[structopt(parse(from_os_str))]
path: Option<PathBuf>,
},
Expand Down
4 changes: 2 additions & 2 deletions src/command/pack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use command::utils::{find_pkg_directory, set_crate_path};
use command::utils::{find_pkg_directory, get_crate_path};
use failure::Error;
use log::info;
use npm;
Expand All @@ -9,7 +9,7 @@ use PBAR;
/// Executes the 'npm pack' command on the 'pkg' directory
/// which creates a tarball that can be published to the NPM registry
pub fn pack(path: Option<PathBuf>) -> result::Result<(), Error> {
let crate_path = set_crate_path(path)?;
let crate_path = get_crate_path(path)?;

info!("Packing up the npm package...");
let pkg_directory = find_pkg_directory(&crate_path).ok_or_else(|| {
Expand Down
4 changes: 2 additions & 2 deletions src/command/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod access;

use self::access::Access;
use command::build::{Build, BuildOptions, Target};
use command::utils::{find_pkg_directory, set_crate_path};
use command::utils::{find_pkg_directory, get_crate_path};
use dialoguer::{Confirmation, Input, Select};
use failure::Error;
use log::info;
Expand All @@ -20,7 +20,7 @@ pub fn publish(
path: Option<PathBuf>,
access: Option<Access>,
) -> result::Result<(), Error> {
let crate_path = set_crate_path(path)?;
let crate_path = get_crate_path(path)?;

info!("Publishing the npm package...");
info!("npm info located in the npm debug log");
Expand Down
6 changes: 3 additions & 3 deletions src/command/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use binary_install::Cache;
use bindgen;
use build;
use cache;
use command::utils::set_crate_path;
use command::utils::get_crate_path;
use console::style;
use failure::Error;
use lockfile::Lockfile;
Expand All @@ -19,7 +19,7 @@ use test::{self, webdriver};
/// Everything required to configure the `wasm-pack test` command.
pub struct TestOptions {
#[structopt(parse(from_os_str))]
/// The path to the Rust crate.
/// The path to the Rust crate. If not set, searches up the path from the current dirctory.
pub path: Option<PathBuf>,

#[structopt(long = "node")]
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Test {
extra_options,
} = test_opts;

let crate_path = set_crate_path(path)?;
let crate_path = get_crate_path(path)?;
let crate_data = manifest::CrateData::new(&crate_path, None)?;
let any_browser = chrome || firefox || safari;

Expand Down
26 changes: 24 additions & 2 deletions src/command/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,30 @@ use walkdir::WalkDir;

/// 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>) -> Result<PathBuf, failure::Error> {
Ok(path.unwrap_or_else(|| PathBuf::from(".")))
pub fn get_crate_path(path: Option<PathBuf>) -> Result<PathBuf, failure::Error> {
match path {
Some(p) => Ok(p),
None => find_manifest_from_cwd(),
}
}

/// Search up the path for the manifest file from the current working directory
/// If we don't find the manifest file then return back the current working directory
/// to provide the appropriate error
fn find_manifest_from_cwd() -> Result<PathBuf, failure::Error> {
let mut parent_path = std::env::current_dir()?;
let mut manifest_path = parent_path.join("Cargo.toml");
loop {
if !manifest_path.is_file() {
if parent_path.pop() {
manifest_path = parent_path.join("Cargo.toml");
} else {
return Ok(PathBuf::from("."));
}
} else {
return Ok(parent_path.to_owned());
}
}
}

/// Construct our `pkg` directory in the crate.
Expand Down
1 change: 1 addition & 0 deletions tests/all/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn build_in_non_crate_directory_doesnt_panic() {
fixture
.wasm_pack()
.arg("build")
.arg(".")
.assert()
.failure()
.stderr(predicates::str::contains("missing a `Cargo.toml`"));
Expand Down
44 changes: 44 additions & 0 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fs;
use std::path::PathBuf;
use utils::{self, fixture};
use wasm_pack::command::build::Target;
use wasm_pack::command::utils::get_crate_path;
use wasm_pack::{self, license, manifest};

#[test]
Expand Down Expand Up @@ -481,3 +482,46 @@ fn it_lists_license_files_in_files_field_of_package_json() {
pkg.files,
);
}

#[test]
fn it_recurses_up_the_path_to_find_cargo_toml() {
let fixture = utils::fixture::Fixture::new();
fixture.hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "recurse-for-manifest-test"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
homepage = "https://rustwasm.github.io/wasm-pack/"
"#,
);
let path = get_crate_path(None).unwrap();
let crate_data = manifest::CrateData::new(&path, None).unwrap();
let name = crate_data.crate_name();
assert_eq!(name, "wasm_pack");
}

#[test]
fn it_doesnt_recurse_up_the_path_to_find_cargo_toml_when_default() {
let fixture = utils::fixture::Fixture::new();
fixture.hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "recurse-for-manifest-test"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
homepage = "https://rustwasm.github.io/wasm-pack/"
"#,
);
let path = get_crate_path(Some(PathBuf::from("src"))).unwrap();
let crate_data = manifest::CrateData::new(&path, None);
assert!(crate_data.is_err());
}