Skip to content

Commit

Permalink
Merge pull request #70 from iqlusioninc/testing/simplify-error-handling
Browse files Browse the repository at this point in the history
testing: Simplify error handling
  • Loading branch information
tony-iqlusion authored Jun 29, 2019
2 parents 5e1b4ea + 0021252 commit 56ab3cc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
10 changes: 2 additions & 8 deletions abscissa_generator/template/tests/acceptance.rs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ use abscissa::testing::CmdRunner;

#[test]
fn start_no_args() {
let mut cmd = CmdRunner::default()
.arg("start")
.capture_stdout()
.run()
.unwrap();

let mut cmd = CmdRunner::default().arg("start").capture_stdout().run();
cmd.stdout().expect_line("Hello, world!");
cmd.wait().unwrap().expect_success();
}
Expand All @@ -23,8 +18,7 @@ fn start_with_args() {
let mut cmd = CmdRunner::default()
.args(&["start", "acceptance", "test"])
.capture_stdout()
.run()
.unwrap();
.run();

cmd.stdout().expect_line("Hello, acceptance test!");
cmd.wait().unwrap().expect_success();
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Abscissa: an application microframework
//! ![Abscissa](https://www.iqlusion.io/img/github/iqlusioninc/abscissa/abscissa.svg)
//!
//! Abscissa is a microframework for building Rust applications (either CLI tools
//! or network services), aiming to provide a large number of features with a
Expand All @@ -23,8 +23,7 @@
//!
//! # Creating a new Abscissa application
//!
//! If you already have Rust installed, the following commands will generate an
//! Abscissa application skeleton:
//! The following commands will generate an Abscissa application skeleton:
//!
//! ```text
//! $ cargo install abscissa
Expand Down
34 changes: 22 additions & 12 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl CargoRunner {
}

/// Run the given subcommand
pub fn run(&self) -> Result<Process, FrameworkError> {
pub fn run(&self) -> Process {
self.print_command().unwrap();

let stdout = if self.capture_stdout {
Expand All @@ -119,14 +119,19 @@ impl CargoRunner {
.stdin(Stdio::piped())
.stdout(stdout)
.stderr(stderr)
.spawn()?;
.spawn()
.unwrap_or_else(|e| {
panic!("error running command: {}", e);
});

Ok(Process::new(child, self.timeout))
Process::new(child, self.timeout)
}

/// Get the exit status for the given subcommand
pub fn status(&self) -> Result<ExitStatus, FrameworkError> {
self.run()?.wait()
pub fn status(&self) -> ExitStatus {
self.run().wait().unwrap_or_else(|e| {
panic!("error waiting for subprocess to terminate: {}", e);
})
}

/// Print the command we're about to run
Expand Down Expand Up @@ -225,7 +230,17 @@ impl CmdRunner {
}

/// Invoke `cargo run` with the given arguments
pub fn run(&self) -> Result<Process, FrameworkError> {
pub fn run(&self) -> Process {
self.runner().run()
}

/// Get the exit status after invoking the given command
pub fn status(&self) -> ExitStatus {
self.runner().status()
}

/// Construct a `CargoRunner` based on the configuration
fn runner(&self) -> CargoRunner {
let mut runner = CargoRunner::default();

// Invoke `cargo run`.
Expand All @@ -250,12 +265,7 @@ impl CmdRunner {
runner.capture_stderr();
}

runner.run()
}

/// Get the exit status after invoking the given command
pub fn status(&self) -> Result<ExitStatus, FrameworkError> {
self.run()?.wait()
runner
}
}

Expand Down
2 changes: 0 additions & 2 deletions tests/app/exit_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ fn no_args() {
CmdRunner::default()
.capture_stdout()
.status()
.unwrap()
.expect_success();
}

Expand All @@ -17,6 +16,5 @@ fn invalid_args() {
.arg("foobar") // invalid arg
.capture_stdout()
.status()
.unwrap()
.expect_code(1);
}
2 changes: 0 additions & 2 deletions tests/generate_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ fn test_generated_app() {
for test_command in TEST_COMMANDS {
CargoRunner::new(test_command.split(" "))
.status()
.unwrap()
.expect_success();
}
}
Expand All @@ -48,7 +47,6 @@ fn generate_app(path: &Path) {
&abscissa_crate_patch,
])
.status()
.unwrap()
.expect_success();

let app_test_dir = path.join("tests");
Expand Down

0 comments on commit 56ab3cc

Please sign in to comment.