Skip to content

Commit 969797c

Browse files
Merge pull request #243 from fitzgen/running-in-non-crate-dir
Running in non crate dir
2 parents 883cae2 + a32b03d commit 969797c

File tree

7 files changed

+43
-11
lines changed

7 files changed

+43
-11
lines changed

src/command/build.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,25 @@ pub struct BuildOptions {
6464
// build_config: Option<BuildConfig>,
6565
}
6666

67-
impl From<BuildOptions> for Build {
68-
fn from(build_opts: BuildOptions) -> Self {
67+
type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>;
68+
69+
impl Build {
70+
/// Construct a build command from the given options.
71+
pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, Error> {
6972
let crate_path = set_crate_path(build_opts.path);
70-
let crate_name = manifest::get_crate_name(&crate_path).unwrap();
73+
let crate_name = manifest::get_crate_name(&crate_path)?;
7174
// let build_config = manifest::xxx(&crate_path).xxx();
72-
Build {
75+
Ok(Build {
7376
crate_path,
7477
scope: build_opts.scope,
7578
disable_dts: build_opts.disable_dts,
7679
target: build_opts.target,
7780
debug: build_opts.debug,
7881
// build_config,
7982
crate_name,
80-
}
83+
})
8184
}
82-
}
83-
84-
type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>;
8585

86-
impl Build {
8786
/// Execute this `Build` command.
8887
pub fn run(&mut self, log: &Logger, mode: &BuildMode) -> Result<(), Error> {
8988
let process_steps = Build::get_process_steps(mode);

src/command/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
8484
"normal" => BuildMode::Normal,
8585
_ => BuildMode::Normal,
8686
};
87-
Build::from(build_opts).run(&log, &build_mode)
87+
Build::try_from_opts(build_opts).and_then(|mut b| b.run(&log, &build_mode))
8888
}
8989
Command::Pack { path } => {
9090
info!(&log, "Running pack command...");

src/logger.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use std::path::PathBuf;
1111
/// Create the logger for wasm-pack that will output any info warning or errors we encounter
1212
pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, Error> {
1313
let log_path = log_file_path(&cmd);
14-
let file = OpenOptions::new().create(true).append(true).open(log_path)?;
14+
let file = OpenOptions::new()
15+
.create(true)
16+
.append(true)
17+
.open(log_path)?;
1518

1619
let decorator = PlainDecorator::new(file);
1720
let drain = FullFormat::new(decorator).build().fuse();

src/manifest.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ struct Repository {
6868

6969
fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
7070
let manifest_path = path.join("Cargo.toml");
71+
if !manifest_path.is_file() {
72+
return Error::crate_config(&format!(
73+
"Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?",
74+
path.display()
75+
)).map(|_| unreachable!());
76+
}
7177
let mut cargo_file = File::open(manifest_path)?;
7278
let mut cargo_contents = String::new();
7379
cargo_file.read_to_string(&mut cargo_contents)?;

tests/all/build.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use structopt::StructOpt;
2+
use utils;
3+
use wasm_pack::{command, logger, Cli};
4+
5+
#[test]
6+
fn build_in_non_crate_directory_doesnt_panic() {
7+
let fixture = utils::fixture::fixture("tests/fixtures/not-a-crate");
8+
let cli = Cli::from_iter_safe(vec![
9+
"wasm-pack",
10+
"build",
11+
&fixture.path.display().to_string(),
12+
]).unwrap();
13+
let logger = logger::new(&cli.cmd, cli.verbosity).unwrap();
14+
let result = command::run_wasm_pack(cli.cmd, &logger);
15+
assert!(
16+
result.is_err(),
17+
"running wasm-pack in a non-crate directory should fail, but it should not panic"
18+
);
19+
let err_msg = result.unwrap_err().to_string();
20+
assert!(err_msg.contains("missing a `Cargo.toml`"));
21+
}

tests/all/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ extern crate failure;
33
#[macro_use]
44
extern crate serde_derive;
55
extern crate serde_json;
6+
extern crate structopt;
67
extern crate tempfile;
78
extern crate wasm_pack;
89

10+
mod build;
911
mod manifest;
1012
mod readme;
1113
mod utils;

tests/fixtures/not-a-crate/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is not a Rust crate!

0 commit comments

Comments
 (0)