#exported #cdylib #projects #linker #dynamically #testing #items

build test-cdylib

Library for dynamically linking to cdylib projects from test code

2 stable releases

1.1.0 Apr 29, 2020
1.0.0 Jan 24, 2020

#515 in Build Utils

Download history 1325/week @ 2024-07-27 1458/week @ 2024-08-03 1448/week @ 2024-08-10 1755/week @ 2024-08-17 1847/week @ 2024-08-24 1184/week @ 2024-08-31 1135/week @ 2024-09-07 1235/week @ 2024-09-14 1129/week @ 2024-09-21 1298/week @ 2024-09-28 1310/week @ 2024-10-05 1409/week @ 2024-10-12 1765/week @ 2024-10-19 1028/week @ 2024-10-26 1219/week @ 2024-11-02 554/week @ 2024-11-09

4,825 downloads per month
Used in 3 crates

MIT/Apache

29KB
656 lines

test-cdylib

CI Latest Version Rust Documentation Rustc Version 1.31+

test-cdylib is a library for dynamically linking to cdylib projects from test code. This allows testing for the existence of exported items.

This library is based off of dtolnay's TryBuild.

Testing a cdylib project

A cdylib project can be tested like this:

#[test]
fn api_test() {
    let dylib_path = test_cdylib::build_current_project();

    // Or load the shared library using any other method of your choice.
    let dylib = dlopen::symbor::Library::open(&dylib_path).unwrap();

    // Test the api as necessary.
}

This will build the current project, if it is not already built, and return the path to the compiled library.

Testing a cdylib building library

Libraries that are meant to help create cdylib interfaces can be tested in two ways. First is to link to an example, e.g.

#[test]
fn api_gen_test() {
    let dylib_path = test_cdylib::build_example("example");

    // Or load the shared library using any other method of your choice.
    let dylib = dlopen::symbor::Library::open(&dylib_path).unwrap();

    // Test the api as necessary.
}

This will build the example and return the path to the compiled library.

The second way is to build a file as a cdylib, e.g.

#[test]
fn api_gen_test() {
    let dylib_path = test_cdylib::build_path("tests/cdylib/api_test.rs");

    // Or load the shared library using any other method of your choice.
    let dylib = dlopen::symbor::Library::open(&dylib_path).unwrap();

    // Test the api as necessary.
}

This will build the given file as a cdylib project, and return the path to the compiled library. All dependencies and dev-dependencies are available. Note that this will cause all dependencies to be rebuilt, which can slow down testing significantly.

Multiple tests with the same library

Multiple tests can link to the same library by using once_cell to contain the path to the library, e.g.

use once_cell::sync::Lazy;
use std::path::PathBuf;
static LIB_PATH: Lazy<PathBuf> = Lazy::new(|| test_cdylib::build_current_project());

The same can be done for both build_example and build_path.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1–2MB
~42K SLoC