-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tests): unified testing infrastructure for
perseus-cli
This allows having shared utilities.
- Loading branch information
1 parent
264a308
commit 5b18efd
Showing
3 changed files
with
63 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
mod build; | ||
mod clean; | ||
mod export; | ||
mod export_error_page; | ||
mod help; | ||
mod new; | ||
mod serve; | ||
mod snoop_build; | ||
mod snoop_serve; | ||
mod snoop_wasm_build; | ||
mod tools; | ||
|
||
mod utils { | ||
use std::process::Command; | ||
|
||
/// Tests a running app by executing the given command. This will safely | ||
/// terminate any child processes in the event of an error. | ||
pub fn test_serve(cmd: &mut Command, path: &str) -> Result<(), Box<dyn std::error::Error>> { | ||
use command_group::CommandGroup; | ||
|
||
// We use a group process spawn because the child will spawn the server process, | ||
// which can't be cleaned up if SIGKILL is sent | ||
let mut child = cmd.group_spawn()?; | ||
|
||
std::thread::sleep(std::time::Duration::from_millis(5000)); | ||
|
||
// Check if the child process has failed (this is the only way to catch | ||
// things like binding errors); this will not block trying to wait | ||
let exit_status = child.try_wait()?; | ||
if let Some(status) = exit_status { | ||
panic!("server process returned non-zero exit code '{}'", status); | ||
} | ||
|
||
// We don't extensively test things here, since that's what Perseus' testing | ||
// system is for, and that tests *all* the core examples extensively in a | ||
// headless browser, which is more realistic than simple HTTP requests | ||
// anyway | ||
let body = ureq::get(path) | ||
.call() | ||
.map_err(|err| { | ||
let _ = child.kill(); | ||
err | ||
})? | ||
.into_string() | ||
.map_err(|err| { | ||
let _ = child.kill(); | ||
err | ||
})?; | ||
assert!(body.contains("Welcome to Perseus!")); | ||
|
||
let _ = child.kill(); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters