Skip to content

Commit

Permalink
Added unit testing support
Browse files Browse the repository at this point in the history
  • Loading branch information
svelterust committed Jan 23, 2024
1 parent a95a274 commit 0a65ebc
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.commit_hash.outputs.commit_hash }}
release_name: Release ${{ steps.commit_hash.outputs.commit_hash }}
release_name: ${{ steps.commit_hash.outputs.commit_hash }}
draft: false
prerelease: false

Expand Down
57 changes: 56 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ name = "knarkos"
version = "0.1.0"
edition = "2021"

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
spin = "0.9.8"
x86_64 = "0.14.2"
volatile = "0.2.6"
uart_16550 = "0.2.0"
bootloader = "0.9.23"
lazy_static = { version = "1.0", features = ["spin_no_std"] }

[package.metadata.bootimage]
test-success-exit-code = 33
test-args = [
"-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
"-display", "none"
]
59 changes: 58 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,74 @@
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]

mod vga;
mod serial;

#[no_mangle]
pub extern "C" fn _start() -> ! {
println!("Hello, world!");
#[cfg(test)]
test_main();

loop {}
}

/// This function is called on panic.
pub trait Testable {
fn run(&self) -> ();
}

impl<T> Testable for T
where
T: Fn(),
{
fn run(&self) {
serial_print!("{}...\t", core::any::type_name::<T>());
self();
serial_println!("[ok]");
}
}

#[cfg(not(test))] // new attribute
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{}", info);
loop {}
}

// our panic handler in test mode
#[cfg(test)]
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
serial_println!("[failed]\n");
serial_println!("Error: {}\n", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}

#[cfg(test)]
pub fn test_runner(tests: &[&dyn Testable]) {
serial_println!("Running {} tests", tests.len());
for test in tests {
test.run();
}
exit_qemu(QemuExitCode::Success);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum QemuExitCode {
Success = 0x10,
Failed = 0x11,
}

pub fn exit_qemu(exit_code: QemuExitCode) {
use x86_64::instructions::port::Port;

unsafe {
let mut port = Port::new(0xf4);
port.write(exit_code as u32);
}
}
32 changes: 32 additions & 0 deletions src/serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use spin::Mutex;
use uart_16550::SerialPort;
use lazy_static::lazy_static;

lazy_static! {
pub static ref SERIAL1: Mutex<SerialPort> = {
let mut serial_port = unsafe { SerialPort::new(0x3F8) };
serial_port.init();
Mutex::new(serial_port)
};
}

#[macro_export]
macro_rules! serial_print {
($($arg:tt)*) => {
$crate::serial::_print(format_args!($($arg)*));
};
}

#[macro_export]
macro_rules! serial_println {
() => ($crate::serial_print!("\n"));
($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => ($crate::serial_print!(
concat!($fmt, "\n"), $($arg)*));
}

#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write;
SERIAL1.lock().write_fmt(args).expect("Printing to serial failed");
}

0 comments on commit 0a65ebc

Please sign in to comment.