-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/remove-libc-conflicts
- Loading branch information
Showing
11 changed files
with
395 additions
and
7 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
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
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,14 @@ | ||
[package] | ||
name = "test-runner" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[features] | ||
console = [] | ||
gdb = [] | ||
socket = [] | ||
|
||
[dependencies] | ||
ctru-rs = { git = "https://github.com/rust3ds/ctru-rs" } | ||
ctru-sys = { git = "https://github.com/rust3ds/ctru-rs" } | ||
libc = "0.2.147" |
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,41 @@ | ||
# test-runner | ||
|
||
A helper crate for running automated Rust tests on a 3DS. Since the builtin | ||
Rust test framework expects a more traditional OS with subprocesses etc., | ||
it's necessary to use `custom_test_frameworks` when running tests on 3DS | ||
hardware or in an emulator to get a similar experience as a usual `cargo test`. | ||
|
||
## Usage | ||
|
||
First the test runner to your crate: | ||
|
||
```sh | ||
cargo add --dev test-runner --git https://github.com/rust3ds/ctru-rs | ||
``` | ||
|
||
In `lib.rs` and any integration test files: | ||
|
||
```rs | ||
#![feature(custom_test_frameworks)] | ||
#![test_runner(test_runner::run_gdb)] | ||
``` | ||
|
||
<!-- TODO document the different runners --> | ||
|
||
## Caveats | ||
|
||
* GDB doesn't seem to support separate output streams for `stdout` and `stderr`, | ||
so all test output to `stderr` will end up combined with `stdout` and both will be | ||
printed to the runner's `stdout`. If you know a workaround for this that doesn't | ||
require patching + building GDB itself please open an issue about it! | ||
|
||
* Doctests require a bit of extra setup to work with the runner, since they don't | ||
use the crate's `#![test_runner]`. To write doctests, add the following to the | ||
beginning of the doctest (or `fn main()` if the test defines it): | ||
|
||
```rust | ||
let _runner = test_runner::GdbRunner::default(); | ||
``` | ||
|
||
The runner must remain in scope for the duration of the test in order for | ||
the test output to be printed. |
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,54 @@ | ||
use std::process::Termination; | ||
|
||
use ctru::prelude::*; | ||
use ctru::services::gfx::{Flush, Swap}; | ||
|
||
use super::TestRunner; | ||
|
||
/// Run tests using the [`ctru::console::Console`] (print results to the 3DS screen). | ||
/// This is mostly useful for running tests manually, especially on real hardware. | ||
pub struct ConsoleRunner { | ||
gfx: Gfx, | ||
hid: Hid, | ||
apt: Apt, | ||
} | ||
|
||
impl TestRunner for ConsoleRunner { | ||
type Context<'this> = Console<'this>; | ||
|
||
fn new() -> Self { | ||
let gfx = Gfx::new().unwrap(); | ||
let hid = Hid::new().unwrap(); | ||
let apt = Apt::new().unwrap(); | ||
|
||
gfx.top_screen.borrow_mut().set_wide_mode(true); | ||
|
||
Self { gfx, hid, apt } | ||
} | ||
|
||
fn setup(&mut self) -> Self::Context<'_> { | ||
Console::new(self.gfx.top_screen.borrow_mut()) | ||
} | ||
|
||
fn cleanup<T: Termination>(mut self, result: T) -> T { | ||
// We don't actually care about the output of the test result, either | ||
// way we'll stop and show the results to the user. | ||
|
||
println!("Press START to exit."); | ||
|
||
while self.apt.main_loop() { | ||
let mut screen = self.gfx.top_screen.borrow_mut(); | ||
screen.flush_buffers(); | ||
screen.swap_buffers(); | ||
|
||
self.gfx.wait_for_vblank(); | ||
|
||
self.hid.scan_input(); | ||
if self.hid.keys_down().contains(KeyPad::START) { | ||
break; | ||
} | ||
} | ||
|
||
result | ||
} | ||
} |
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,77 @@ | ||
use std::process::Termination; | ||
|
||
use ctru::error::ResultCode; | ||
|
||
use super::TestRunner; | ||
|
||
// We use a little trick with cfg(doctest) to make code fences appear in | ||
// rustdoc output, but compile without them when doctesting. This raises warnings | ||
// for invalid code, though, so silence that lint here. | ||
#[cfg_attr(not(doctest), allow(rustdoc::invalid_rust_codeblocks))] | ||
/// Show test output in GDB, using the [File I/O Protocol] (called HIO in some 3DS | ||
/// homebrew resources). Both stdout and stderr will be printed to the GDB console. | ||
/// | ||
/// Creating this runner at the beginning of a doctest enables output from failing | ||
/// tests. Without `GdbRunner`, tests will still fail on panic, but they won't display | ||
/// anything written to `stdout` or `stderr`. | ||
/// | ||
/// The runner should remain in scope for the remainder of the test. | ||
/// | ||
/// [File I/O Protocol]: https://sourceware.org/gdb/onlinedocs/gdb/File_002dI_002fO-Overview.html#File_002dI_002fO-Overview | ||
/// | ||
/// # Examples | ||
/// | ||
#[cfg_attr(not(doctest), doc = "````")] | ||
/// ``` | ||
/// let _runner = test_runner::GdbRunner::default(); | ||
/// assert_eq!(2 + 2, 4); | ||
/// ``` | ||
#[cfg_attr(not(doctest), doc = "````")] | ||
/// | ||
#[cfg_attr(not(doctest), doc = "````")] | ||
/// ```should_panic | ||
/// let _runner = test_runner::GdbRunner::default(); | ||
/// assert_eq!(2 + 2, 5); | ||
/// ``` | ||
#[cfg_attr(not(doctest), doc = "````")] | ||
pub struct GdbRunner(()); | ||
|
||
impl Default for GdbRunner { | ||
fn default() -> Self { | ||
|| -> ctru::Result<()> { | ||
// TODO: `ctru` expose safe API to do this and call that instead | ||
unsafe { | ||
ResultCode(ctru_sys::gdbHioDevInit())?; | ||
// TODO: should we actually redirect stdin or nah? | ||
ResultCode(ctru_sys::gdbHioDevRedirectStdStreams(true, true, true))?; | ||
} | ||
Ok(()) | ||
}() | ||
.expect("failed to redirect I/O streams to GDB"); | ||
|
||
Self(()) | ||
} | ||
} | ||
|
||
impl Drop for GdbRunner { | ||
fn drop(&mut self) { | ||
unsafe { ctru_sys::gdbHioDevExit() } | ||
} | ||
} | ||
|
||
impl TestRunner for GdbRunner { | ||
type Context<'this> = (); | ||
|
||
fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
fn setup(&mut self) -> Self::Context<'_> {} | ||
|
||
fn cleanup<T: Termination>(self, test_result: T) -> T { | ||
// GDB actually has the opportunity to inspect the exit code, | ||
// unlike other runners, so let's follow the default behavior of the | ||
// stdlib test runner. | ||
test_result.report().exit_process() | ||
} | ||
} |
Oops, something went wrong.