Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rksm committed Aug 8, 2022
1 parent 3e1a9f9 commit 4ceab4d
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 6 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
on: [push]

name: CI

jobs:
build_and_test:
name: hot-lib-reloader tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- uses: actions-rs/cargo@v1
with:
command: test
args: -- --no-capture
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Cargo.lock
DEV.org
*.org
examples/bevy-extended/
/.idea/
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ thiserror = "1.0.32"

[dev-dependencies]
env_logger = "^0.9"
recursive_lib = { path = "tests/recursive_lib" }

[workspace]
resolver = "2"
Expand Down
4 changes: 1 addition & 3 deletions examples/minimal/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use hot_lib_reloader::hot_module;

#[hot_module(dylib = "lib")]
#[hot_lib_reloader::hot_module(dylib = "lib")]
mod hot_lib {
hot_functions_from_file!("../lib/src/lib.rs");
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ For a detailed discussion see https://robert.kra.hn/posts/hot-reloading-rust/.
Assuming you use a workspace with the following layout:
```
```ignore
├── Cargo.toml
└── src
│ └── main.rs
Expand Down Expand Up @@ -87,7 +87,7 @@ lib = { path = "lib" }
You can then define and use the lib reloader like so (`./src/main.rs`):
```no_run
```ignore
hot_lib_reloader::define_lib_reloader! {
unsafe MyLibLoader {
// Will look for "liblib.so" (Linux), "lib.dll" (Windows), ...
Expand Down
5 changes: 4 additions & 1 deletion src/lib_reloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ impl LibReloader {
///
/// Users of this API must specify the correct type of the function or variable loaded.
pub unsafe fn get_symbol<T>(&self, name: &[u8]) -> Result<Symbol<T>, HotReloaderError> {
Ok(self.lib.as_ref().unwrap().get(name)?)
match &self.lib {
None => Err(HotReloaderError::LibraryNotLoaded),
Some(lib) => Ok(lib.get(name)?),
}
}
}

Expand Down
26 changes: 26 additions & 0 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub fn recompile(dir: impl AsRef<std::path::Path>) {
std::process::Command::new("cargo")
.arg("build")
.current_dir(dir)
.spawn()
.and_then(|proc| proc.wait_with_output())
.expect("cargo build failed");
}

pub fn modify_file_and_do(
file: impl AsRef<std::path::Path>,
modify_file_fn: impl FnOnce(&str) -> String,
do_fn: impl FnOnce() + std::panic::UnwindSafe,
) {
let file = file.as_ref().canonicalize().expect("cannot find lib file");

let content = std::fs::read_to_string(&file).expect("cannot read file");
let new_content = modify_file_fn(content.as_str());
std::fs::write(&file, new_content).expect("cannot write lib file");

let res = std::panic::catch_unwind(do_fn);

std::fs::write(&file, content).expect("cannot restore file");

res.expect("modify_file_and_do: do_fn panicked");
}
29 changes: 29 additions & 0 deletions tests/recursive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mod common;

#[hot_lib_reloader::hot_module(dylib = "recursive_lib")]
mod hot_lib {
hot_functions_from_file!("./recursive_lib/src/lib.rs");
}

#[test]
fn test() {
let n = hot_lib::do_more_stuff(Box::new(hot_lib::do_stuff));
assert_eq!(n, 5);

common::modify_file_and_do(
"tests/recursive_lib/src/lib.rs",
|content| {
content.replace(
"pub fn do_stuff() -> i32 { 3 }",
"pub fn do_stuff() -> i32 { 5 }",
)
},
|| {
common::recompile("tests/recursive_lib");
std::thread::sleep(std::time::Duration::from_secs(1));

let n = hot_lib::do_more_stuff(Box::new(hot_lib::do_stuff));
assert_eq!(n, 7);
},
);
}
9 changes: 9 additions & 0 deletions tests/recursive_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "recursive_lib"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["rlib", "dylib"]

[dependencies]
9 changes: 9 additions & 0 deletions tests/recursive_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[rustfmt::skip]
#[no_mangle]
pub fn do_stuff() -> i32 { 3 }

#[no_mangle]
pub fn do_more_stuff(callback: Box<dyn Fn() -> i32>) -> i32 {
let n = callback();
n + 2
}

0 comments on commit 4ceab4d

Please sign in to comment.