error[E0463]: can't find crate for `test`

Hello! I've the following setup (assuming we're the src directory):

main.rs:

#![no_std]
#![no_main]
#![feature(const_ptr_offset)]

use os_clone2;

#[no_mangle]
pub extern "C" fn os_clone2_entry() -> ! {
    loop {}
}

lib.rs

#![no_std]
#![no_main]
#![feature(const_ptr_offset)]
#![feature(custom_test_frameworks)]
#![test_runner(test_runner)]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

fn test_runner(_test: &[&i32]) {
    loop {}
}

.cargo/config.toml

[build]
target = "target.json"

[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]

target.json

{
    "llvm-target": "x86_64-unknown-none",
    "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "none",
    "executables": true,
    "linker-flavor": "ld.lld",
    "linker": "rust-lld",
    "panic-strategy": "abort",
    "disable-redzone": true,
    "features": "-mmx,-sse,+soft-float"
}

Cargo.toml

[package]
name = "os_clone2"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

(I've configered rustup to use the nightly channel) If I run cargo clean && cargo build -Zbuild-std && cargo test then I'm getting the following error message:

error[E0463]: can't find crate for `test`

But shouldn't it work according to this page? What am I doing wrong?

Edit

I created a test repository where you can get the exact same project to test with. Just run cargo test with the nightly version with rustup.

Libtest doesn't run on baremetal targets as it needs support for threads and terminal output. You may want to look at the custom_test_frameworks feature. See Testing | Writing an OS in Rust for an example on how to use it to test a kernel. My bad. Looks like you are already using them.

Yes and I'm wondering why I'm still getting this error message.

It looks like you need #![reexport_test_harness_main = "test_main"] and then call the test_main function to run the tests. (you can use any name instead of test_main. just be consistent.)

I only changed the main.rs now to:

#![no_std]
#![no_main]
#![feature(const_ptr_offset)]
#![reexport_test_harness_main = "test_main"]

use os_clone2;

#[no_mangle]
pub extern "C" fn os_clone2_entry() -> ! {
    #[cfg(test)]
    test_main();

    loop {}
}

But I'm still getting the same error.

I'm afraid I can't help any further.

Ok, but still thank you for your answers! :slight_smile:

I added a link to a repo which contains my test code if it can help you.

Ok, I got it running. All you need to do is adding the following lines to your main.rs:

#![reexport_test_harness_main = "test_main"]
#![feature(custom_test_frameworks)]
#![test_runner(test_runner)]

lib.rs

#![feature(custom_test_frameworks)]
#![test_runner(test_runner)]

And most importantly: You can run it only on your given target machine, otherwise you'll get a segmentation fault!

I also updated the code in my test repository.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.