-
Notifications
You must be signed in to change notification settings - Fork 26
Use custom test framework to cargo test
!
#41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Meziu
merged 5 commits into
rust3ds:master
from
ian-h-chamberlain:testing/custom-test-framework
Feb 8, 2022
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ea3ab4
First pass of using custom test runner
ian-h-chamberlain 75727fd
Fix build and tweak some options
ian-h-chamberlain b839fc3
Merge remote-tracking branch 'upstream/master' into testing/custom-te…
ian-h-chamberlain 6beed43
Address review comments
ian-h-chamberlain 91c2c58
Remove unnecessary escape characters
ian-h-chamberlain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,135 @@ | ||
//! Custom test runner for building/running unit tests on the 3DS. | ||
|
||
extern crate test; | ||
ian-h-chamberlain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use std::io; | ||
|
||
use test::{ColorConfig, Options, OutputFormat, RunIgnored, TestDescAndFn, TestFn, TestOpts}; | ||
|
||
use crate::console::Console; | ||
use crate::gfx::Gfx; | ||
use crate::services::hid::{Hid, KeyPad}; | ||
|
||
/// A custom runner to be used with `#[test_runner]`. This simple implementation | ||
/// runs all tests in series, "failing" on the first one to panic (really, the | ||
/// panic is just treated the same as any normal application panic). | ||
pub(crate) fn test_runner(tests: &[&TestDescAndFn]) { | ||
crate::init(); | ||
|
||
let gfx = Gfx::default(); | ||
let hid = Hid::init().unwrap(); | ||
|
||
let mut top_screen = gfx.top_screen.borrow_mut(); | ||
top_screen.set_wide_mode(true); | ||
let _console = Console::init(top_screen); | ||
|
||
// Start printing from the top left | ||
print!("\x1b[1;1H"); | ||
ian-h-chamberlain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// TODO: it would be nice to have a way of specifying argv to make these | ||
// configurable at runtime, but I can't figure out how to do it easily, | ||
// so for now, just hardcode everything. | ||
let opts = TestOpts { | ||
list: false, | ||
filters: Vec::new(), | ||
filter_exact: false, | ||
// Forking is not supported | ||
force_run_in_process: true, | ||
exclude_should_panic: false, | ||
run_ignored: RunIgnored::No, | ||
run_tests: true, | ||
// Benchmarks are not supported | ||
AzureMarker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bench_benchmarks: false, | ||
logfile: None, | ||
nocapture: false, | ||
// TODO: color doesn't work because of TERM/TERMINFO. | ||
// With RomFS we might be able to fake this out nicely... | ||
color: ColorConfig::AlwaysColor, | ||
ian-h-chamberlain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
format: OutputFormat::Pretty, | ||
shuffle: false, | ||
shuffle_seed: None, | ||
// tweak values? This seems to work out of the box | ||
test_threads: Some(3), | ||
ian-h-chamberlain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
skip: Vec::new(), | ||
time_options: None, | ||
options: Options::new(), | ||
}; | ||
|
||
// Use the default test implementation with our hardcoded options | ||
let _success = run_static_tests(&opts, tests).unwrap(); | ||
|
||
// Make sure the user can actually see the results before we exit | ||
println!("Press START to exit."); | ||
|
||
gfx.flush_buffers(); | ||
gfx.swap_buffers(); | ||
|
||
loop { | ||
hid.scan_input(); | ||
|
||
if hid.keys_down().contains(KeyPad::KEY_START) { | ||
break; | ||
} | ||
ian-h-chamberlain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
/// Adapted from [`test::test_main_static`], along with [`make_owned_test`]. | ||
fn run_static_tests(opts: &TestOpts, tests: &[&TestDescAndFn]) -> io::Result<bool> { | ||
let tests = tests.iter().map(make_owned_test).collect(); | ||
test::run_tests_console(opts, tests) | ||
} | ||
|
||
/// Clones static values for putting into a dynamic vector, which test_main() | ||
/// needs to hand out ownership of tests to parallel test runners. | ||
/// | ||
/// This will panic when fed any dynamic tests, because they cannot be cloned. | ||
fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn { | ||
match test.testfn { | ||
TestFn::StaticTestFn(f) => TestDescAndFn { | ||
testfn: TestFn::StaticTestFn(f), | ||
desc: test.desc.clone(), | ||
}, | ||
TestFn::StaticBenchFn(f) => TestDescAndFn { | ||
testfn: TestFn::StaticBenchFn(f), | ||
desc: test.desc.clone(), | ||
}, | ||
_ => panic!("non-static tests passed to test::test_main_static"), | ||
} | ||
} | ||
|
||
/// The following functions are stubs needed to link the test library, | ||
/// but do nothing because we don't actually need them for it to work (hopefully). | ||
// TODO: move to linker-fix-3ds ? | ||
ian-h-chamberlain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mod link_fix { | ||
#[no_mangle] | ||
extern "C" fn execvp( | ||
_argc: *const libc::c_char, | ||
_argv: *mut *const libc::c_char, | ||
) -> libc::c_int { | ||
-1 | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn pipe(_fildes: *mut libc::c_int) -> libc::c_int { | ||
-1 | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn pthread_sigmask( | ||
_how: ::libc::c_int, | ||
_set: *const libc::sigset_t, | ||
_oldset: *mut libc::sigset_t, | ||
) -> ::libc::c_int { | ||
-1 | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn sigemptyset(_arg1: *mut libc::sigset_t) -> ::libc::c_int { | ||
-1 | ||
} | ||
|
||
#[no_mangle] | ||
extern "C" fn sysconf(_name: libc::c_int) -> libc::c_long { | ||
-1 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.