phil-opp / blog_os

Writing an OS in Rust

Home Page:http://os.phil-opp.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

post-04 - Conditional compilation does not seem to work

AlexandreMarcq opened this issue · comments

Hello,

I'm following along the Testing part of the blog.
Unfortunately, cargo run and cargo test yield the same result in QEMU.

I have added a #[cfg(test)] before a println! and it does not appear on the screen.

Here is the code I have :

#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]

use core::panic::PanicInfo;

mod vga_buffer;

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    println!("{}", info);
    loop {}
}

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

    #[cfg(test)]
    test_main();

    loop {}
}

#[cfg(test)]
fn test_runner(tests: &[&dyn Fn()]) {
    println!("Running {} tests", tests.len());
}

And here is the output :
image

Is there something I'm missing ?

I don't see any issues with your code. Do you have your full code online somewhere so that I can take a look?

Could you try cloning the post-04 branch of this repo? When I put the code you posted above into the src/main.rs I get different output for cargo test --bin blog_os and cargo run:

  • cargo test --bin blog_os
    image
  • No output for cargo run

I've uploaded my code in my repo.
I tried with the post-04 branch. I got the expected output with cargo run but cargo test seems to hang :
image