Merge pull request #243 from fitzgen/running-in-non-crate-dir

Running in non crate dir
master
ashley williams 7 years ago committed by GitHub
commit 969797c0ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/command/build.rs
  2. 2
      src/command/mod.rs
  3. 5
      src/logger.rs
  4. 6
      src/manifest.rs
  5. 21
      tests/all/build.rs
  6. 2
      tests/all/main.rs
  7. 1
      tests/fixtures/not-a-crate/README.md

@ -64,12 +64,15 @@ pub struct BuildOptions {
// build_config: Option<BuildConfig>,
}
impl From<BuildOptions> for Build {
fn from(build_opts: BuildOptions) -> Self {
type BuildStep = fn(&mut Build, &Step, &Logger) -> 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_name = manifest::get_crate_name(&crate_path).unwrap();
let crate_name = manifest::get_crate_name(&crate_path)?;
// let build_config = manifest::xxx(&crate_path).xxx();
Build {
Ok(Build {
crate_path,
scope: build_opts.scope,
disable_dts: build_opts.disable_dts,
@ -77,13 +80,9 @@ impl From<BuildOptions> for Build {
debug: build_opts.debug,
// build_config,
crate_name,
}
})
}
}
type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>;
impl Build {
/// Execute this `Build` command.
pub fn run(&mut self, log: &Logger, mode: &BuildMode) -> Result<(), Error> {
let process_steps = Build::get_process_steps(mode);

@ -84,7 +84,7 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
"normal" => BuildMode::Normal,
_ => BuildMode::Normal,
};
Build::from(build_opts).run(&log, &build_mode)
Build::try_from_opts(build_opts).and_then(|mut b| b.run(&log, &build_mode))
}
Command::Pack { path } => {
info!(&log, "Running pack command...");

@ -11,7 +11,10 @@ use std::path::PathBuf;
/// Create the logger for wasm-pack that will output any info warning or errors we encounter
pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, Error> {
let log_path = log_file_path(&cmd);
let file = OpenOptions::new().create(true).append(true).open(log_path)?;
let file = OpenOptions::new()
.create(true)
.append(true)
.open(log_path)?;
let decorator = PlainDecorator::new(file);
let drain = FullFormat::new(decorator).build().fuse();

@ -68,6 +68,12 @@ struct Repository {
fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
let manifest_path = path.join("Cargo.toml");
if !manifest_path.is_file() {
return Error::crate_config(&format!(
"Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?",
path.display()
)).map(|_| unreachable!());
}
let mut cargo_file = File::open(manifest_path)?;
let mut cargo_contents = String::new();
cargo_file.read_to_string(&mut cargo_contents)?;

@ -0,0 +1,21 @@
use structopt::StructOpt;
use utils;
use wasm_pack::{command, logger, Cli};
#[test]
fn build_in_non_crate_directory_doesnt_panic() {
let fixture = utils::fixture::fixture("tests/fixtures/not-a-crate");
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();
let result = command::run_wasm_pack(cli.cmd, &logger);
assert!(
result.is_err(),
"running wasm-pack in a non-crate directory should fail, but it should not panic"
);
let err_msg = result.unwrap_err().to_string();
assert!(err_msg.contains("missing a `Cargo.toml`"));
}

@ -3,9 +3,11 @@ extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate structopt;
extern crate tempfile;
extern crate wasm_pack;
mod build;
mod manifest;
mod readme;
mod utils;

@ -0,0 +1 @@
This is not a Rust crate!
Loading…
Cancel
Save