Browse Source

Add test-runner as a dependency and fix build errs

Luckily, Cargo doesn't mind circular dev-dependencies and we can patch
to ensure it's using the same version. We can also leave this as a
`git = "..."` dependency if we want, since it's a dev dependency.
pull/135/head
Ian Chamberlain 1 year ago
parent
commit
ff12bd0818
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 4
      Cargo.toml
  2. 8
      ctru-rs/Cargo.toml
  3. 8
      ctru-rs/src/lib.rs
  4. 73
      ctru-rs/src/test_runner.rs
  5. 3
      ctru-sys/build.rs
  6. 8
      ctru-sys/src/lib.rs

4
Cargo.toml

@ -4,5 +4,7 @@ default-members = ["ctru-rs", "ctru-sys"] @@ -4,5 +4,7 @@ default-members = ["ctru-rs", "ctru-sys"]
resolver = "2"
[patch.'https://github.com/rust3ds/ctru-rs']
# Make sure all dependencies use the local ctru-sys package
# Make sure all dependencies use the local packages. This is needed for things
# like pthread-3ds that rely on ctru-sys, and test-runner which relies on ctru-rs
ctru-rs = { path = "ctru-rs" }
ctru-sys = { path = "ctru-sys" }

8
ctru-rs/Cargo.toml

@ -29,13 +29,15 @@ widestring = "0.2.2" @@ -29,13 +29,15 @@ widestring = "0.2.2"
toml = "0.5"
[dev-dependencies]
bytemuck = "1.12.3"
cfg-if = "1.0.0"
ferris-says = "0.2.1"
futures = "0.3"
lewton = "0.10.2"
# TODO: switch to rust3ds org once migrated there. Also, rename?
test-runner = { git = "https://github.com/ian-h-chamberlain/test-runner-3ds" }
time = "0.3.7"
tokio = { version = "1.16", features = ["rt", "time", "sync", "macros"] }
cfg-if = "1.0.0"
bytemuck = "1.12.3"
lewton = "0.10.2"
[features]
default = ["romfs", "big-stack"]

8
ctru-rs/src/lib.rs

@ -22,7 +22,7 @@ @@ -22,7 +22,7 @@
#![feature(custom_test_frameworks)]
#![feature(try_trait_v2)]
#![feature(allocator_api)]
#![test_runner(test_runner::run)]
#![test_runner(test_runner::run_gdb)] // TODO: does this make sense to have configurable?
#![doc(
html_favicon_url = "https://user-images.githubusercontent.com/11131775/225929072-2fa1741c-93ae-4b47-9bdf-af70f3d59910.png"
)]
@ -73,9 +73,10 @@ pub fn use_panic_handler() { @@ -73,9 +73,10 @@ pub fn use_panic_handler() {
/// When `test` is enabled, this function will be ignored.
#[cfg(not(test))]
fn panic_hook_setup() {
use crate::services::hid::{Hid, KeyPad};
use std::panic::PanicInfo;
use crate::services::hid::{Hid, KeyPad};
let main_thread = std::thread::current().id();
// Panic Hook setup
@ -110,7 +111,4 @@ pub mod mii; @@ -110,7 +111,4 @@ pub mod mii;
pub mod prelude;
pub mod services;
#[cfg(test)]
mod test_runner;
pub use crate::error::{Error, Result};

73
ctru-rs/src/test_runner.rs

@ -1,73 +0,0 @@ @@ -1,73 +0,0 @@
//! Custom test runner for building/running unit tests on the 3DS.
extern crate test;
use std::io;
use test::{ColorConfig, OutputFormat, TestDescAndFn, TestFn, TestOpts};
use crate::prelude::*;
/// 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 run(tests: &[&TestDescAndFn]) {
let gfx = Gfx::new().unwrap();
let mut hid = Hid::new().unwrap();
let apt = Apt::new().unwrap();
let mut top_screen = gfx.top_screen.borrow_mut();
top_screen.set_wide_mode(true);
let _console = Console::new(top_screen);
let opts = TestOpts {
force_run_in_process: true,
run_tests: true,
// TODO: color doesn't work because of TERM/TERMINFO.
// With RomFS we might be able to fake this out nicely...
color: ColorConfig::AutoColor,
format: OutputFormat::Pretty,
// Hopefully this interface is more stable vs specifying individual options,
// and parsing the empty list of args should always work, I think.
// TODO Ideally we could pass actual std::env::args() here too
..test::test::parse_opts(&[]).unwrap().unwrap()
};
// 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.");
while apt.main_loop() {
gfx.wait_for_vblank();
hid.scan_input();
if hid.keys_down().contains(KeyPad::START) {
break;
}
}
}
/// Adapted from [`test::test_main_static`] and [`test::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"),
}
}

3
ctru-sys/build.rs

@ -68,7 +68,8 @@ fn check_libctru_version() -> Result<(String, String, String), Box<dyn Error>> { @@ -68,7 +68,8 @@ fn check_libctru_version() -> Result<(String, String, String), Box<dyn Error>> {
if lib_version != crate_built_version {
return Err(format!(
"libctru version is {lib_version} but this crate was built for {crate_built_version}"
))?;
)
.into());
}
let Output { stdout, .. } = Command::new(pacman)

8
ctru-sys/src/lib.rs

@ -16,3 +16,11 @@ pub use result::*; @@ -16,3 +16,11 @@ pub use result::*;
pub unsafe fn errno() -> s32 {
*__errno()
}
// TODO: not sure if there's a better way to do this, but I have gotten myself
// with this a couple times so having the hint seems nice to have.
#[cfg(test)]
compile_error!(concat!(
"ctru-sys doesn't have tests and its lib test will fail to build at link time. ",
"Try specifying `--package ctru-rs` to build those tests.",
));

Loading…
Cancel
Save